Tuesday, 9 April, 2019 UTC


Summary

Javascript Null, Undefined and,  NaN Tutorial With Example is today’s topic. The value null represents the intentional absence of any object value. The undefined property indicates that the variable has not been assigned a value, or not declared at all. The NaN property represents a “Not-a-Number” value. The NaN property indicates that a value is not a legitimate number.
Javascript null, undefined and NaN
The null is one of JavaScript’s primitive values. The NaN is a property of the global object. The global undefined property represents the primitive value undefined. It is one of JavaScript’s primitive types. Let’s see one by one.
Javascript null
The value null is written with a literal: null. The null is not an identifier for a property of the global object, like undefined can be. Instead, null expresses a lack of identification, indicating that a variable points to no object. In APIs, null is often retrieved in a place where an object can be expected, but no object is relevant. In short, In JavaScript, the data type of null is an object.
Let’s see some examples of null.
// app.js

const marvel = null
console.log(marvel)
The output is null.
Now, let’s remove the first line from the above code and see the output.
// app.js

console.log(marvel)
Now, run the file in the node environment and see the output.
 
See, it throws an error. So when we assign the null value, it gives the null in return.
Let’s check the typeof null. See the following code.
// app.js

let marvel = null
console.log(typeof(marvel))
The output is following.
 
So, null is an object in Javascript.

Javascript null basic math operations

Check out the basic math operations on null. Write the following code.
// app.js

console.log(null / 7)
console.log(null % 7)
console.log(null - 7)
console.log(null + 7)
console.log(null * 7)
The output is following.
Javascript undefined
The undefined is the property of a global object; i.e., it is the variable in the global scope. An initial value of undefined is the primitive value undefined. A variable that has not been assigned a value is of type undefined. The method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. See the following example.
// app.js

let marvel
console.log(marvel == undefined)
So, if the marvel is undefined, then it will return a true otherwise false. We have not assigned the value to the marvel yet, that is why it returns undefined. The output is following,
 

Typeof operator and undefined

See the following code.
// app.js

let marvel
if (typeof marvel === 'undefined') {
   console.log('Marvel is undefined')
}
else {
  console.log('It is not undefined')
}
In the above code, we have compared the undefined with the unassigned variable marvel. So, if the condition is true, it will print the console message. The output is following.
 
Difference between null and undefined in Javascript
The undefined and null are equal in value but different in type.
See the following code.
// app.js

console.log(typeof undefined)
console.log(typeof null)
console.log(null === undefined )
console.log(null == undefined)
The output is following.
 
See, if you compare the value, then they both are same, but if you compare its datatype, then they are different that is why we get false.
Javascript NaN
NaN is a property of the global object. I think the definition is clear enough. JavaScript returns this value when number we’re supposed to get isn’t a number. For example, when you’re trying to subtract an “appdividend” from 21 or divide 12 by “KDL”.
See the following example.
// app.js

console.log('Appdividend' - 30)
The output is following.
 
The NaN property is a same as the Number.Nan property. We can use the isNaN() global function to check if a value is a NaN value when you’re adding something to the string. If JavaScript sees a + sign and a string, it automatically converts the second element of addition into the string as well.
Now, let’s check the typeof NaN.
// app.js

console.log(typeof NaN)
Run the file and see the output.
 
So, the datatype of NaN is number. Not an object or undefined or null.
 If NaN compared to itself returns false, then no matter what we’re going to compare it to, it will always give false. See the following code.
// app.js

console.log(NaN === NaN)
console.log(NaN == NaN)
The output is following.
 
Finally, Javascript Null, Undefined and NaN Tutorial With Example is over.
The post Javascript Null, Undefined and NaN Tutorial With Example appeared first on AppDividend.