What is Null?
We have heard the word "null" many times in English. Its literal meaning is having no value. The same meaning is analogous to the meaning of null in programming.
Null is basically an assignment value given to a variable. The variable which has been assigned as null contains no value.
Here we have assigned the value null to variable x.
What is Undefined?
There is a subtle difference between null and undefined, but as a programmer, it is important that we understand it clearly.
As the name suggests, undefined means "not defined". So we declare a variable but do not assign a value to it, the variable becomes undefined.
Type of Null and Undefined
As discussed above, the type of null is an object.
Here we have assigned the null value to variable x and then checked its type.
As discussed above, the type of undefined is undefined itself. So basically, it is a data type.
Here we have declared variable x and then checked its type. During run time, undefined gets assigned to the variable x.
Why is Null an Object?
In APIs, null is often used or returned in a place where an object can be expected as the output but no object is relevant. This shows the absence of an object as null is returned.
Differentiating Using isNaN()
While performing various arithmetic operations, null is converted to 0, and then the operation is performed, while undefined is converted to NaN. This is so because while performing arithmetic operations, the function toNumber() gets called on it, which converts null to 0 and undefined to NaN.
Undefined vs Null
|Definition||Undefined means the variable has been declared, but its value has not been assigned.||Null means an empty value or a blank value.|
|The typeof operator||The typeof() operator returns undefined for an undefined variable.||The typeof() operator returns the type as an object for a variable whose value is assigned as null.|
|On performing arithmetic operations||It returns NaN on performing arithmetic operations.||converts to 0 then perform the operation|
|Is it an assignment value?||No, as there is no value assigned to the variable, it becomes undefined.||Yes, as we assign null to a variable, it is an assignment value.|
As we know, when we initialize a variable as null, and perform arithmetic operations on it, then it gets converted to 0. We have calculated the exponent of the base, and here the exponent is 0 so the value becomes 1.
As discussed above, we haven't passed any argument for 'h' so it takes the default value as undefined. And we have performed arithmetic operations on variables out of which one is undefined, so the result is NaN.
- Null is basically an assignment value. The variable which has been assigned as null contains no value and is empty.
- Undefined is when we declare a variable but do not assign a value to it(define it), the variable becomes undefined.
- Null is a type of object.
- The data type of undefined is undefined.
- When we perform arithmetic operations on null, the value gets converted to 0, and then the further operations get performed. When we perform arithmetic operations on undefined, it prints NaN as the value is undefined and not a number.