What’s an array in JavaScript?
In the simplest of terms, it’s a list:
var list = ['item1', 'item2', 'item3'];
// array created using the literal notation
An array can hold any type of value:
var array = [
'string',
Number(1),
Boolean(true),
{ object: '' },
[ 'array' ],
Function,
null,
undefined
];
Arrays are also zero-indexed, which means the first element starts at 0
:
var array = ['first', 'second'];
array[0]; // 'first'
array[1]; // 'second'
Then, what’s the index of the last element?
var array = ['first', 'last'];
array.length; // 2
var lastIndex = array.length - 1;
array[lastIndex]; // 'last'
Although an array is an instance of Array
, it’s actually a type Object
:
var arr = [];
arr.constructor; // [Function: Array]
arr instanceof Array; // true
typeof arr; // 'object'
Therefore, you can think of arrays as a hash:
var array = ['apple', 'banana', 'carrot'];
var associativeArray = {
'0': 'apple',
'1': 'banana',
'2': 'carrot'
};
You can specifically set an array element to a value:
var array = [];
array[0] = 'initial';
array; // [ 'initial' ]
array[0] = 'override';
array; // [ 'override' ]
If it’s set to an index beyond the array’s length, the unset elements will be undefined
:
var array = ['one'];
var array[2] = 'three';
array; // [ 'one', undefined, 'three' ]
And array elements can also be deleted:
var array = ['one', 'two', 'three'];
delete array[2];
array; // [ 'one', 'two', undefined ]
What to learn more about array methods? Check out the post for more information.