Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
JavaScript uneval() Function
Next article icon

Null in JavaScript

Last Updated : 05 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.



Next Article
JavaScript uneval() Function

S

sayanc170
Improve
Article Tags :
  • JavaScript
  • Technical Scripter
  • Web Technologies
  • JavaScript-Questions
  • Technical Scripter 2022

Similar Reads

  • Undefined Vs Null in JavaScript
    In JavaScript, both undefined and null represent the absence of a meaningful value, but they have different purposes and are used in distinct contexts. Knowing when and how to use each can help you write clearer, more predictable code. Let's see the differences between undefined and null in JavaScri
    4 min read
  • JavaScript Quiz
    These JavaScript quiz questions are designed to help you test and enhance your knowledge of JavaScript, ranging from basic concepts to advanced features. These topic-specific quizzes provide a comprehensive way to practice and assess your understanding of JavaScript concepts. The JavaScript quizzes
    2 min read
  • JavaScript uneval() Function
    The uneval() is an inbuilt function in JavaScript that is used to create a string representation of the source code of an Object. Syntax: uneval(object) Note: This function has been DEPRECATED and is no longer recommended. Parameters: It accepts an object which may be a JavaScript expression or stat
    2 min read
  • Undefined in JavaScript
    Undefined is a type of Data type in JavaScript. It is a primitive value undefined, when a variable is declared and not initialized or not assigned with any value. By default, the variable was stored with an Undefined value. Undefined is a global read-only variable that represents the value Undefined
    2 min read
  • What is Vanilla JavaScript ?
    Vanilla JavaScript, synonymous with pure or plain JavaScript, eschews external tools like libraries or frameworks. It empowers developers to craft interactive websites solely with JavaScript. While it excels in frontend development, Node.js, a JavaScript framework, is preferred for backend operation
    4 min read
  • What is JavaScript?
    JavaScript is a powerful and flexible programming language for the web that is widely used to make websites interactive and dynamic. JavaScript can also able to change or update HTML and CSS dynamically. JavaScript can also run on servers using tools like Node.js, allowing developers to build entire
    6 min read
  • JavaScript Basic Quiz
    These JavaScript basic quiz questions are designed to help you test and enhance your foundational knowledge of JavaScript. Covering essential concepts, this quiz offers a great way to assess your understanding of the core features of JavaScript. The JavaScript quizzes are perfect for beginners and p
    2 min read
  • JavaScript Basics
    JavaScript is a versatile, lightweight scripting language widely used in web development. It can be utilized for both client-side and server-side development, making it essential for modern web applications. Known as the scripting language for web pages, JavaScript supports variables, data types, op
    6 min read
  • JavaScript Output
    JavaScript provides different methods to display output, such as console.log(), alert(), document.write(), and manipulating HTML elements directly. Each method has its specific use cases, whether for debugging, user notifications, or dynamically updating web content. Here we will explore various Jav
    4 min read
  • JavaScript Syntax
    JavaScript syntax refers to the rules and conventions dictating how code is structured and arranged within the JavaScript programming language. This includes statements, expressions, variables, functions, operators, and control flow constructs. Syntax console.log("Basic Print method in JavaScript");
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences