Skip to content

JavaScript is null or empty

  • by

In JavaScript, “null” and “empty” are two different concepts. “Null” is a special value that represents the intentional absence of any object value, while “empty” depends on the context in which it is used. To check whether a variable is null or empty, you need to check the type of the variable and its value.

To check if a string is empty:

if(myString === "") {
  // string is empty

To check if an array is empty:

if(myArray.length === 0) {
  // array is empty

The object is empty:

if(Object.keys(myObject).length === 0) {
  // object is empty

The variable is null:

if(myVariable === null) {
  // variable is null

JavaScript is a null or empty example

Simple example code check if a variable is null or empty in JavaScript:

<!DOCTYPE html>
    let myNullVariable = null;
    let myEmptyString = "";
    let myEmptyArray = [];
    let myEmptyObject = {};

    // Check if variable is null
    if(myNullVariable === null) {
        console.log("Variable is null");

    // Check if string is empty
    if(myEmptyString === "") {
        console.log("String is empty");

    // Check if array is empty
    if(myEmptyArray.length === 0) {
        console.log("Array is empty");

    // Check if object is empty
    if(Object.keys(myEmptyObject).length === 0) {
        console.log("Object is empty");



JavaScript is null or empty

The methods used to check if a string, array, or object is empty are different. We use the length property to check if the array is empty, and we use the Object.keys() method to check if the object is empty.

Comment if you have any doubts or suggestions on this JS basic topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading