close
The Wayback Machine - https://web.archive.org/web/20240213201513/https://www.geeksforgeeks.org/null-in-javascript/
Open In App
Related Articles

Null in JavaScript

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The null keyword in JavaScript is a falsy data type that denotes that the data type is null. Falsy type refers to the coercion of null to false during conditional statement executions.

Null is an object in JavaScript and represents primitive data types. A null value in JavaScript is used for referring absence of any object value and if any function or variable returns null, then we can infer that the object could not be created. If we pass null as the default parameter to any function type it would take the ‘null’ as a value passed to it.

Syntax:

let number = null;
console.log("Type of number is:" ,typeof number);

Example 1: This example describes the Null value in JavaScript.

Javascript

class Square {
    constructor(length) {
        this.length = length;
    }
    get area_of_square() {
        return Math.pow(this.length, 2);
    }
 
    // Static function that returns the length
    static create_function(length) {
        return length > 0 ? new Square(length) : null;
    }
}
 
let variableOne = Square.create_function(10);
console.log(variableOne.area_of_square);
 
let variableTwo = Square.create_function();
console.log(variableTwo); // null

                    

Output
100
null

Explanation: In this example, 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.

Example 2: Another example that will illustrate Null in JavaScript.

Javascript

const var1 = null;
if (var1) {
    console.log('var1 is not null');
} else {
    console.log('var1 is null');
}

                    

Output
var1 is null

Explanation: In this example, we have declared var1 as null. As we know that var1 is a falsy value therefore the else block gets executed only.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!


Last Updated : 22 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials