![]() |
VOOZH | about |
In JavaScript, `null` indicates the deliberate absence of any object value. It's a primitive value that denotes the absence of a value or serves as a placeholder for an object that isn't present. `null` differs from `undefined`, which signifies a variable that has been declared but hasn't been assigned a value.
Syntax:
let number = null;
console.log("Type of number is:" ,typeof number);This example describes the Null value in JavaScript.
Output:
100
nullExplanation:
Here, there is a Square class that has a constructor that takes length as the argument. The Square class has a static method named create_function() which returns a new Square object that has a specified length. Here, there are two scenarios one in which we pass an argument and another one in which we do not pass an argument.
In the first scenario, we create variableOne that creates a new object of Square, and a value of 10 is passed in the create_function() method. In the second scenario, we have created variableTwo but we do not pass anything there and therefore it returns a null as output.
Another example that will illustrate Null in JavaScript.
Output:
var1 is nullExplanation:
Here, we have declared var1 as null. As we know that var1 is a falsy value therefore the else block gets executed only.
If an object couldn’t be created, returning null is a common practice:
Output:
Square { length: 10 }
nullUse null to indicate that a value is intentionally absent:
Output:
Nullnull is a falsy value.Remember, mastering null will enhance your JavaScript skills.