Difference Between Undefined and Null Value in Javascript

What’s the difference between an undefined and null value in Javascript. If you are new to  Javascript, probably you will find difficulty in understanding the difference between undefined and null value. How to check whether a variable is undefined or null.

Javascript Array Functions

Javascript has five primitive data types.

i) String

ii) Number

iii) Boolean

iv) Undefined

v) Null

The last two types undefined and null represents no value. Let’s understand, what’s the difference between undefined and null value.

Subscribe Our Tutorials

Get Latest Updates on Facebook

Difference Between Undefined and Null Value in Javascript

In the case of undefined in Javascript, Either a variable is not declared. Or a variable is declared but no value is assigned to it. When you declare any variable in a javascript, the default value of a variable is undefined. To test this case, open a console and create any variable and press enter. It shows undefined.

As compared to undefined, Null is an assignment value, it can be assigned to a variable in case of no value or empty value.

Let’s understand this concept using an example.

Let’s create one variable in Javascript.

Now, Let’s initialize null to a variable.

We have seen through example that undefined is a type itself whereas null is a special type of object.

For reference

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/null

WebRewrite

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.
Tagged , , . Bookmark the permalink.
  • dimitrios

    nice nice

  • Good article. Just one correction, There are 5 primitive types in JS
    1. number
    2. string
    3. boolean
    4. object
    5. undefined

    null is not a primitive, its an object.
    Similarly NaN and Infinity are of type number.