How many values does javascript have for nothing?

JavaScript has two values for nothing i.e., null and undefined. These two are also the primitive types in JavaScript.


Out of the two values, Undefined in JavaScript means if a variable is declared and no value is assigned to the variable, then this variable is said to be undefined. An object can also be null. It is said to be null when there is no value to it.

Example 1

This example of the undefined value in JavaScript.

var str console.log('The value of given variable is:',str)

In the above example 1, a variable named ‘str’ is declared. So, as it is just declared and no value is assigned and by default it takes the value as ‘undefined’. When the value of the undefined variable is printed, it displays as “undefined” only.

Example 2

Following is another example of this value

var myVar if(myVar === void 0) { console.log("myVar has the value:",typeof(myVar)); }

Example 3

let a; console.log(a); function b() {} console.log(b())


Null in JavaScript is of type ‘Object’. The name itself refers that the value of the variable is nothing or it can be said that the value of the variable is absent (or zero). JavaScript cannot assign the value ‘null’ to any of the variables or objects unlike to that of undefined. The programmer or the user have to assign a variable or an object to null.

Example 1

This example demonstrates about the ‘null’ value in JavaScript −

var a = null; console.log("This value of given variable is:",a) console.log("The type of null value is:",typeof null)

In the above example, the variable is assigned a value null directly. When the value is printed in then it displays as ‘null’. And it also displays the type of null as ‘Object’.

As null means zero arithmetic operations can be performed when a variable is null and returns the result. But if a variable is undefined then no arithmetic operations can be performed and returns NaN (Not a Number).

Example 2

This example shows the operations that can be done for ‘undefined’ and ‘null’ values −

var a console.log("The variable is:",a,"Type of is:",typeof a) console.log("Value of undefined when addition is done:",a+1) var b = null console.log("The variable is:",b,"Type of is:",typeof b) console.log("Value of null when addition is done:",b+1)

Example 3

let a = null; function b() { return null } console.log(a); console.log(b())