Arrays are variables that can store ordered collection of values in them which can be of different data types, with each having a numeric position known as an index.
Different types of value that we can store in the array is:
- Number(Integers, floating points)
- Other arrays
So, let’s see how to do it.
For that, we have two options: using square brackets  or, the ‘new’ keyword.
//Example var myArray = ; //Method 1 var animals = new Array(); //Method 2
For Method 1:
var myArray = ["john", 23]; //Method 1
For Method 2:
var animals = new Array("cat", "tiger", "lion"); //Method 2
Assign Value to Array
For this example, let us take the array list so we want to assign a new value to our array list
We can do it in two ways.
We can use indexes to assign values to the arrays.
animals = "kiwi"; console.log(animals); //Prints: "cat", "tiger", "lion", undefined, undefined, "kiwi"
Here, we can see that there are some undefined values are present in our array because when we work with indexes we are creating space regardless if it is the last position of our array, so we use push() method as mentioned below.
Using push() method:
The push() method allows us to add elements at the end of the arrays but unlike using index values, it does create any ‘undefined’ values in our array “animals”.
animals.push("kiwi", "panther", "leopard"); console.log(list); // Prints: "cat", "tiger", "lion", "kiwi", "panther", "leopard"