Difference between Null and Undefined in JavaScript

Like other languages, JavaScript also supports Null and undefined terms for variable handling. If a variable is declared but has not been assigned with any value, then it is referred to as undefined. However, if a variable is assigned with a Null value, then it means that it is empty. It is to notice that both the terms are intermingling for a novel programmer.

Therefore, this guide intends to list down the differences between Undefined and Null values in JavaScript. By the end of this guide, the following learning outcomes are expected.

Before digging into the difference, let’s elaborate both the terms individually.

Null value in JavaScript

Null represents no value and variables can be assigned with the Null keyword to represent them as a Null variable. For instance, the following code assigns a null value to a variable.

var x = null;
console.log(x);

In the above code, a variable named ‘x‘ is created and null is assigned to that variable.

Output

The output shows that the value of x (in which a null value was stored) is printed on the console.

Undefined value in JavaScript

A variable that has been declared but whose value has not been assigned is referred to as an undefined variable. Apart from this, the undefined term can be obtained by using the undefined keyword while assigning.

Let’s refer to the following few lines of code.

var y;
console.log(y);

In the above code, a variable y is declared but is not assigned with any value.

Output

The output shows that the value has returned undefined as its output states that the value of the variable does not exist.

Difference between Null and Undefined

This section lists down the notable differences and provides examples that would better clarify each difference.

“Equal” but not “identical”

The Null and undefined values look alike but the following code differentiates the Null and undefined values by using the “==” and “===” operator. Notice that, the “==” operator looks for the equality of the terms whereas the “===” operator looks for the value and type of the variables.

var x = null;
var y;
console.log(x==y);
console.log(x===y);

The above code takes two variables, the x has a null value whereas the variable y is undefined.

Output

The output shows that the “==” operator returns true but the strict equality operator has shown false. Thus, it can be concluded that the Null and Undefined terms are not identical in JavaScript.

Arithmetic Operation

While dealing with arithmetic operations, the Null and Undefined values show different behaviors. The following code practices the addition by using Undefined and Null values.

var x = null;
var y;
console.log(x + 2);
console.log(y + 1);

The above code declares one variable with a null value and the other variable is set to an undefined value. Moreover, the console.log statement intends to print the result of the addition(+) operator, and the variables “x” and “y“.

Output

The output shows that the Null value acts as a “0” while dealing with arithmetic operators. Contrary to this, when an undefined value is used with arithmetic operators, it would show NaN (Not a Number) in output.

Type

Another prominent difference is the data type of these values. The Null value is of object type whereas the Undefined values have an undefined type. The following line of code refers to showing the difference between the types of these two values.

var x = null;
var y;
console.log("Type of x is: " +typeof(x));
console.log("Type of y is: " +typeof(y));

The above code will print the type of the “x” and “y” variables.

Output

The above output represents that the type of x (which contains null value) is an object whereas the type of y (which is an undefined variable) is undefined.

Conclusion

The Null and Undefined values refer to the various notions of variables in JavaScript. Like, if the variable has not been assigned with any value, then it is referred to as undefined. The Null is a value that is assigned to any variable which states that the variable is empty. Majorly these can be differentiated by their data type, the way they operate in the presence of arithmetic operators. Moreover, one can check the level of equality of Null and Undefined by using “abstract” and “strict” equality operators in JavaScript.



from https://ift.tt/Q3Xvprx

Post a Comment

0 Comments