# null vs undefined in JavaScript: the difference

> Understand the difference between null and undefined in JavaScript, two primitive types, how to check for each, and why typeof null returns 'object'.

Author: Flavio Copes | Published: 2020-06-11 | Canonical: https://flaviocopes.com/javascript-difference-null-undefined/

Let's talk about the similarities first.

`null` and `undefined` are [JavaScript](https://flaviocopes.com/javascript/) primitive types.

The meaning of `undefined` is to say that a variable has declared, but it has no value assigned.

```js
let age //age is undefined
```

```js
let age = null //age is null
```

> Note: accessing a variable that's not been declared will raise a `ReferenceError: <variable> is not defined` error, but this does not mean it's `undefined`.

How do you check if a variable is null? Use the comparison operator, for example `age === null`

Same for undefined: `age === undefined`

In both cases, you can check for:

```js
if (!age) {

}
```

and this will be matching both `null` and `undefined`.

You can also use the `typeof` operator:

```js
let age
typeof age //'undefined'
```

although `null` is evaluated as an object, even though it is a primitive type:

```js
let age = null
typeof age //'object'
```
