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);
Null in JavaScript Example:
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:
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.
Null in JavaScript Example:
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:
Here, we have declared var1 as null. As we know that var1 is a falsy value therefore the else block gets executed only.
Use Cases of Null in JavaScript
1. Object Initialization:
If an object couldn’t be created, returning null
is a common practice:
JavaScript
class Square {
constructor(length) {
this.length = length;
}
static createSquare(length) {
return length > 0 ? new Square(length) : null;
}
}
const square1 = Square.createSquare(10); // Creates a valid Square object
const square2 = Square.createSquare(); // return null
console.log(square1);
console.log(square2);
Output:
Square { length: 10 }
null
2. Checking for null:
Use null
to indicate that a value is intentionally absent:
JavaScript
const myValue = null;
if (myValue) {
console.log("Not null"); // This won't execute
} else {
console.log("Null"); // Output: "Null"
}
Output:
Null
Key Takeaways
null
is a falsy value.- It represents the intentional absence of an object value.
- Use it wisely in your code to convey specific meanings.
Remember, mastering null
will enhance your JavaScript skills.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read