JavaScript Arrays

An array is a special variable, which can hold more than one value:
const countries = ["India","United States","Germany"];

Important Points

In JavaScript, arrays aren't primitives but are instead Array objects with the following core characteristics:
  • JavaScript arrays are resizable and can contain a mix of different data types. Example
  • JavaScript arrays are not associative arrays. Hence array elements cannot be accessed using arbitrary strings as indexes, but must be accessed using non-negative integers (or their respective string form) as indexes. Example
  • JavaScript arrays are zero-indexed. The first element of an array is at index 0. Example
  • JavaScript array-copy operations create shallow copies. Example
Example: Arrays aren't primitives.
const countries = [
  "India",
  "United States",
  "Germany"
];
Example: JavaScript arrays are resizable and can contain a mix of different data types.
const countries = [
  "India",
  "United States",
  "Germany"
];
console.log(countries);
//JavaScript arrays are resizable and can contain a mix of different data types
countries.push(123);
console.log(countries);
Initially array values are of string data type. Later number value is added inside an array without any error. Also array resize automatically while adding number data.
Output:
[ 'India', 'United States', 'Germany' ]
[ 'India', 'United States', 'Germany', 123 ]
Example: JavaScript arrays are not associative arrays. Hence array elements cannot be accessed using arbitrary strings as indexes, but must be accessed using non-negative integers (or their respective string form) as indexes.
const countries = [
  "India",
  "United States",
  "Germany"
];
//JavaScript arrays are not associative arrays. Hence array elements cannot be accessed using arbitrary strings as indexes, but must be accessed using non-negative integers (or their respective string form) as indexes.

//non-negative integers
console.log(countries[0]);
//non-negative integers in string form
//Note: 1 and 01 are not same. Only countries['1'] is an actual array index. countries['01'] is an arbitrary string property that will not be visited in array iteration.
console.log(countries["1"]);
//string index - undefined
console.log(countries["India"]);
//negative index - undefined
console.log(countries[-1]);
Output:
India
United States
undefined
undefined
Example: JavaScript arrays are zero-indexed. The first element of an array is at index 0.
const countries = [
  "India",
  "United States",
  "Germany"
];
//length of an array is 3
console.log(countries.length);
//try to access last value inside an array at index 3
console.log(countries[3]);
//try to access last value inside an array at index 2
console.log(countries[2]);
At index position equal to length of an array got undefined value. But we found last value at (length of an array - 1) index position.
Output:
3
undefined
Germany
Example: JavaScript array-copy operations create shallow copies.
const a = [
  "India",
  "United States",
  "Germany"
];
const b = a;
b[1] = "France";
console.log(a);
console.log(b);
There are two ways to copy objects: shallow copy and deep copy. Shallow copying creates a new object with references to the same memory locations as the original object, while deep copying creates a new object with new memory locations for all of its properties and nested objects or arrays. Above example is shallow copy.
Output:
[ 'India', 'France', 'Germany' ]
[ 'India', 'France', 'Germany' ]

Learn More