Computer >> Computer tutorials >  >> Programming >> Javascript

How many values does javascript have for nothing?


JavaScript has 2 values for nothing, null and undefined. These 2 values are quite different and should be used as such.

undefined

A variable that has not been assigned a value is of type undefined. A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned.

Example

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

Output

undefined
undefined
null

null is an assignment value. It can be assigned to a variable as a representation of no value.

Example

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

Output

null
null

Note −Type of undefined is undefined while that of null is object.