Sunday, 13 August 2017

Simplifying The Difference Between undefined and null

Most of us find it confusing to differentiate between undefined and null variables in the JavaScript world. Here is my attempt to simplify it.
If a variable is undefined implies that it has not been defined. This essentially means that the variable has not been given a value. For such variables that have been declared and not defined (not given a value), JavaScript assigns it a special value 'undefined'. Therefore the data type of such a variable is undefined in this case. By the way this also implies that undefined is a data type in JavaScript and belongs to the pedigree of primitive (simple) data types. Let me summarize the undefined variables by saying that 'these are the variables that are declared but not defined/initialized'.
Now then about the other guy, the null variable! Unlike in the above case, the null variable has a value, the null value. The variable was initialized and given a null value. The null value belongs to the pedigree of objects.
From the above discussion, a simple difference between undefined and null could be put up by saying "undefined variables are the declared variables that never got initialized whereas the null variables have been initialized and have the null value".
However, the similarity between the two is that they are both falsy values! Having said that undefined === null is not true! Did you know why?

3 comments:

  1. null and undefined both are datatypes in JavaScript although both are falsy values === checks datatypes as well that's why it returns false

    ReplyDelete
  2. undefined is a value assigned to the variables which are declared but not initialized.where as in case of null the variable is declared as well as initialized with value Null

    ReplyDelete
  3. undefined and null are different data types on JavaScript.there is little difference in null and undefined. Undefined is primitive data type . if user is trying to access a variable which isn't defined yet then in that case you can get undefined variable. Whereas null is a non primitive data type on JavaScript which means the variable is declared and also defined but user assign a "null" value to variable.

    undefined===null
    above statement implies that both data types are strictly compared[compared with respect to data types and value as well]both are different terms and hence the statement returns false.

    ReplyDelete

The HTML Attributes Never Change!

First things first! What is an HTML attribute? The html attributes provide additional information about or to the element . For instan...