Arrays are variables that can store multiple values. You can access individual values in an array by using their index number. If you attempt to create an already existing array, JavaScript will throw an error. To avoid this, we’ll show you how to create an array if it doesn’t exist in JavaScript, so you don’t accidentally access an undefined array again.
Create an array if it doesn’t exist in JavaScript
Here are three ways to use when you want to create an array if it doesn’t exist in JavaScript.
Using typeof operator
The typeof operator is generally used to return a string that describes the type of an object.
To determine whether an array exists, we first use typeof to determine whether it is undefined. If it is undefined, it does not exist, and we can use its name to create an array. Otherwise, the array already exists, and we can’t create one with the same name.
// Use typeof to check if the array already exists before creating it if (typeof myArr === 'undefined') { const myArr = ['Learn', 'Share', 'IT'] console.log(myArr) } else { console.log('This array already exists') }
Output:
[ 'Learn', 'Share', 'IT' ]
Using isArray()
function
Before continuing, you can learn more about the syntax of the isArray()
function here.
isArray()
is a function that checks whether an object is an array. If the object is an array, it returns true. Otherwise, it returns false. We can use this function to create an array if it doesn’t exist.
Assume you have a variable called myArr
. You want to see if myArr
is an array to determine whether or not myArr
exists. If it is not an array, myArr
does not yet exist, and we can create an array named myArr
.
const myArr = 'My array' // String // Determine whether myArr is an array using isArray() if (Array.isArray(myArr)) { // If it is, this array already exists console.log('This array already exists') } else { // If it is not, we can create an array called myArr const myArr = ['Learn', 'Share', 'IT'] console.log(myArr) }
Output:
[ 'Learn', 'Share', 'IT' ]
Using ||
operator
We can also use the OR operator to see if an array exists. You can learn more about its syntax here.
This operator will return the first truthy value it finds. So, if the first value is false, it will return the second value.
To create an array if it doesn’t exist, we use the OR operator between the array we want to check and the value we want to create. If the array exists (truthy), the OR operator will return it; otherwise, the OR operator will return the value we want to create. Like this:
// Use the OR operator to create an array that does not exist var myArr = myArr || ['Learn', 'Share', 'IT'] console.log(myArr)
Output:
[ 'Learn', 'Share', 'IT' ]
Summary
If you want to check if an array exists in your program and create it simultaneously, we recommend using the OR operator. We hope that by the end of this article, you can easily create an array if it doesn’t exist in JavaScript.
Have a nice day!
Maybe you are interested:
- Check if Multiple Values exist in Array in JavaScript
- Add Array of Values to an Existing Set in JavaScript
- Add value to an array if that value does not exist

Hi, I’m Cora Lopez. I have a passion for teaching programming languages such as Python, Java, Php, Javascript … I’m creating the free python course online. I hope this helps you in your learning journey.
Name of the university: HCMUE
Major: IT
Programming Languages: HTML/CSS/Javascript, PHP/sql/laravel, Python, Java