JavaScript null
JavaScript null is “used to represent the intentional absence of any object value”. Null is distinct from other primitive data types such as undefined, boolean, number, and string in that it is explicitly set by a programmer to indicate a non-value state. In contrast, other primitive data types represent specific values.
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 the variable points to no object.
const marvel = null
console.log(marvel)
The output is null.
Let’s remove the first line from the above code and see the output.
console.log(marvel)
Run the file in the node environment and see the output.
See, it throws an error. So when we assign the null value, it returns the null.
Proof that a null is an object in Javascript
What if we want to check the type of null? We have a function called typeof() that checks the type of any object or value. So, let’s check the typeof() null.
let marvel = null
console.log(typeof(marvel))
See the output.
So, null is an object in Javascript.
The null is falsy
In Javascript, null is the falsy value (i.e., “it evaluates to false if coerced to a boolean)”. Thus, the easiest way to check for null is to know that null evaluates to false in conditionals or if coerced to the boolean value.
const notSureifNull = null
if (notSureifNull) {
console.log("Not null")
}
else {
console.log("It could be null")
} // Could be null
for (let i = 0; null; i++) {
console.log("It won't run")
}
notSureifNull ? console.log("truthy value") : console.log("Falsy value")
Output
Could be null
Falsy value
When you run the above file, you will get this output, which does not comprehend null from the other false values.
Declaring null in JavaScript
We can assign a null object to any variable during the declaration. Javascript variables come to life as the assignment operator, which helps us assign a particular value to the variable.
let data = null
console.log(data)
Output
null
In some programming languages, they don’t have to have an initial value assigned to them.
For example, the initial value for some variables might be Null.
JavaScript undefined
JavaScript undefined is a “primitive data type representing a value that is not defined or initialized”. It is often used to indicate the absence of a value or that a variable or object property has not been assigned a value.
The undefined is the property of a global object, i.e., 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.
let marvel
console.log(marvel == undefined)
So, if the marvel is undefined, it will return a true or otherwise false. This is because we have not assigned the value to the marvel yet; that is why it returns undefined.
The output is the following,
If you define a function that has a return statement or returns no value, then it returns undefined.
function js_undefined() {
}
console.log(js_null())
Output
undefined
When you call the js_undefined() function, it returns undefined because there is no return statement.
In the console, printing shows undefined, showing that we explicitly force print the output.
How to declare an undefined in Javascript
In some programming languages, you don’t have to have an initial value assigned to them. For example, the initial value for some variables might be Null, or in the Javascript case, it is undefined.
So, if you declare a variable in Javascript and do not assign the initial value, then the value of that variable is automatically undefined.
let data
console.log(data)
Output
undefined
If we don’t define a variable and direct print its value in the console, then we will get ReferenceError.
console.log(data)
Output
console.log(data)
^
ReferenceError: data is not defined
All variables in Javascript come into life through the assignment operator.
A variable can only start life as undefined if you don’t assign any value.
If you don’t define a variable and directly try to print in the console, you will get ReferenceError.
JavaScript NaN
JavaScript NaN is a global property value that represents Not-A-Number. JavaScript returns this value when the Number we are supposed to get isn’t a number.
For example, when trying to subtract an “appdividend” from 21 or divide 12 by “KDL”.
console.log('Appdividend' - 30)
See the output.
The NaN property is the same as the Number.Nan property.
When adding something to the string, we can use the isNaN() global function to check if a value is a NaN value. If JavaScript sees a + sign and a string, it automatically converts the second additional element into the string.
Let’s check the typeof NaN.
console.log(typeof NaN)
Run the file and see the output.
The data type of NaN is a Number. Not an object or undefined or null.
If NaN compared to itself returns false, no matter what we’re going to compare it to, it will always be false. See the following code.
console.log(NaN === NaN)
console.log(NaN == NaN)
See the output.
Declaring NaN in Javascript
You can assign a NaN value to any variable using an assignment operator.
let data = NaN
console.log(data)
Output
NaN
Using NaN as part of a comparison
NaN compares unequal (via ==, !=, ===, and !==) to any other value, including another NaN value. Use Number.isNaN() to check whether a value is NaN. Or perform a self-comparison: NaN, and only NaN, will compare unequal to itself.
console.log(NaN === NaN);
console.log(Number.NaN === NaN)
console.log(isNaN(NaN))
console.log(isNaN(Number.NaN))
Output
false
false
true
true
We have compared NaN values with itself and other values using triple equality or strict comparison operator. From the output, we can see that NaN is not strictly equal to NaN. For example, even the Number object’s NaN property is not similar to NaN.
Then we have to test NaN and Number.NaN values with the built-in JavSscript isNaN() function, which returns true.
That’s it.

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.