The difference between NULL and Undefined is as follows:
var num1 = null; var num2 = 3; console.log(num1) console.log(num2)
The output will be:
In other words, it can be said that:
- NULL is an empty non-existent value
- For any computation, it must be assigned with a value
- When a variable is assigned to null, its value is null.
- When a data type is assigned to null, the data type is an object
var a= null; var b= 3; var c= a+b; console.log(c);
var a= null; var b= 3; a=4; var c= a+b; console.log("sum is: ",c);
sum is: 7
- An object or variable is called but not defined.
- When array index out of bound is assigned a value
- When a variable is assigned to undefined, its value is undefined.
- When a variable is assigned to undefined, the data type is considered undefined
An undefined function parameter is called
var demo; console.log(demo)
The following code will show error and thus “Undefined” will be called.
One can also explicitly set a variable to be undefined as shown in the example below:
var c = undefined; console.log(c);
The above code will call “undefined”.