Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • JavaScript - Get Started
  • What is JavaScript
  • Setup JavaScript Dev Environment
  • HTML <script> Tag
  • JavaScript - Syntax
  • JavaScript - Popup Message
  • JavaScript - Variables
  • JavaScript - Operators
  • JavaScript - Data Types
  • JavaScript - String
  • JavaScript - Numbers
  • JavaScript - Boolean
  • JavaScript - Object
  • JavaScript - Date
  • JavaScript - Date Methods
  • JavaScript - Array
  • JavaScript - Array Methods
  • JavaScript - null and undefined
  • JavaScript - Function
  • JavaScript - if condition
  • JavaScript - switch
  • JavaScript - for loop
  • JavaScript - while loop
  • JavaScript - Scope
  • JavaScript - eval
  • JavaScript - Error Handling
  • JavaScript - strict mode
  • JavaScript - Hoisting
  • Define JS Class
  • JS Object In Depth
  • this Keyword
  • new Keyword
  • Prototype
  • Inheritance
  • Closure
  • IIFE
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Difference between null and undefined in JavaScript

Here you will learn what is null and undefined in JavaScript and what is the difference between them.

What is a null?

A null means the absence of a value. You assign a null to a variable with the intention that currently this variable does not have any value but it will have later on. It is like a placeholder for a value. The type of null is the object.

Example: null
let num = null;

console.log(num); // null
console.log(typeof num); // "object"

Sometimes, null variables are the result of erroneous code. For example, if you try to find an HTML element using document.getElelementByID() with the wrong id, then it will return null. So it is recommended to check for null before doing something with that element.

Example: null
var saveButton = document.getElementById("save");

if (saveButton !== null)
    saveButton.submit();

What is undefined?

A variable is undefined when you haven't assigned any value yet, not even a null.

Example: undefined Variable
let num;
console.log(num);//"undefined"

Generally, variables are undefined when you forgot to assign values or change existing code. For example, consider the following Greet() function that returns a string.

function Greet(){
    return "Hi";
}

let str = Greet();//"Hi"`}
                </CodeExample>
    <p>Now, suppose somebody changes the function as below. So now, <code>str</code> will be undefined.</p>
    <div className="card code-panel-without-title">
        <div className="panel-body">
            <pre className="language-js"><code>function Greet(){
    alert("Hi");
}
let str = Greet();//undefined

Thus, undefined variables are the result of some code problems.

Difference between null and undefined

You must explicitly assign a null to a variable. A variable has undefined when no value assigned to it.

Example: null and undefined Variables
let num1 = null;
let num2;

console.log(num1);//null
console.log(num2); //undefined

The '' is not the same as null or undefined.

let str = '';

console.log(typeof str);//string
console.log(str === null); //false
console.log(str === undefined); //false

The type of null variable is object whereas the type of undefined variable is "undefined".

Example: Types
let num1 = null;
let num2;

console.log(typeof num1);//"object"
console.log(typeof num2); //"undefined"

Use the === operator to check whether a variable is null or undefined. The == operator gives the wrong result.

Example: Comparison using === and ==
let num1 = null;
let num2;

console.log(num1 == null); //true
console.log(num2 == undefined);//true

console.log(num1 == undefined);//true (incorrect)
console.log(num2 == null);//true (incorrect)

console.log(num1 === null); //true
console.log(num2 === undefined);//true

console.log(num1 === undefined);//false
console.log(num2 === null);//false

console.log(num1 == num2);//true (incorrect)
console.log(num1 === num2);//false

The null and undefined variables are falsy to if-statements and ternary operators.

Example: Null and undefined with if-statements
let num1 = null;
let num2;

if(num1)
{
    console.log(num1);
}
else
{
    console.log("num1 is null");
}

if(num2)
{
    console.log(num2);
}
else
{
    console.log("num2 is undefined");
}

A null variable treated as 0 in an numeric expression whereas undefined variable will be NaN.

Example: With Numeric Expresion
let num1 = null;
let num2;

console.log(num1 + 10);//10
console.log(num2 + 10); //NaN

It will give wrong result when concatenated with string.

Example: With String Values
let num1 = null;
let num2;

console.log(num1 + " Hello");//"null Hello"
console.log(num2 + " Hello"); //"undefined Hello"

Note: The null and undefined variables are one of the main reasons for runtime errors in JavaScript. The best practice is to check variables for null or undefined before using them.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.