Create An Array If It Doesn’t Exist In JavaScript

To create an array if it doesn’t exist in JavaScript –

  1. Check if the type of the variable is undefined or if the variable is not an array.
  2. If any of the above holds true, create a new array using var variable.

Let’s discuss this in detail below.

create an array if it doesn't exists in JavaScript

Create An Array If It Doesn’t Exist In JavaScript

If you are working with arrays in JavaScript, there might be a situation when you want to create an array if it doesn’t exist.

This can be checked using typeof operator and by checking if the variable is not an array.

If any of the above holds true, we can create a new array using var variable.

Consider the following example.

if(typeof myArray === "undefined" || !Array.isArray(myArray)){

    var myArray = [];

}

console.log(myArray); // []

In the above example, we are checking if the variable myArray is undefined or it is not an array. If any of the conditions hold true, we are creating a new array using var variable.

The typeof operator doesn’t give an error if a variable is undefined. Once we have checked that the variable is not undefined, we go on to check if it is an array using Array.isArray() method.

If the variable myArray is already defined and is an array, then nothing happens as both the conditions fail.

We have used var variable to create a new array as it is hoisted in JavaScript.

This means that the var variable is moved to the top of the function or global scope before the code is executed.

Thus, even if we try to create an array using var variable inside a condition, it will be moved to the top and will be accessible throughout the code. This is why we are able to log the array even if it is declared inside a condition.

If we use let or const variable to create an array, it will give an error as they are not hoisted in JavaScript.

Thus, the above code will give an error if we replace var with let or const.

if(typeof myArray === "undefined" || !Array.isArray(myArray)){

   let myArray = [];

}

console.log(myArray); // ReferenceError: myArray is not defined

if(typeof myArray === "undefined" || !Array.isArray(myArray)){

   const myArray = [];

}

console.log(myArray); // ReferenceError: myArray is not defined

The reason behind this error is that the let or const variable is not hoisted in JavaScript. This means that it is not moved to the top of the function or global scope before the code is executed.

Thus, the variable myArray is not accessible outside the block where it is declared.

This is why we get an error when we try to log the array myArray.

If you use a let keyword to declare an array, you need not check for undefined as the variable is already defined.

let myArray;

if(!Array.isArray(myArray)){

   myArray = [];

}

console.log(myArray); // []

In the above example, we have declared an array using a let keyword. As the variable is already defined, we only need to check if it is not an array.

If it is not an array, we create a new array.

This method to create an array if it doesn’t exist fails when we use a const keyword to define an array.

If you use a const keyword to declare an array, you will get an error because you cannot reassign a constant variable in JavaScript.

const myArray;

if(!Array.isArray(myArray)){

   myArray = [];

}

console.log(myArray); // TypeError: Assignment to constant variable.

In the above example, we have declared an array using a const keyword. As the variable is already defined, we only need to check if it is not an array.

If it is not an array, we try to create a new array. This gives us an error because we cannot reassign a constant variable in JavaScript.

Conclusion – Create An Array If It Doesn’t Exist

When you want to create an array if it doesn’t exist, you can use the following methods.

1. Check if the array is undefined or not using typeof operator.

2. Check if the array is not an array using Array.isArray() method.

3. Create a new array using var keyword.

4. Use a let keyword to declare an array. You need not check for undefined as the variable is already defined.

Leave a Reply