JavaScript Array Const
- Previous Page JS Array Iteration
- Next Page JS Date
ECMAScript 2015 (ES6)
In 2015, JavaScript introduced an important new keyword:const
.
Using const
Declaring an array has become a common practice:
Example
const cars = ["Saab", "Volvo", "BMW"];
Cannot be reassigned
Using const
The declared array cannot be reassigned:
Example
const cars = ["Saab", "Volvo", "BMW"]; cars = ["Toyota", "Volvo", "Audi"]; // ERROR
Arrays are not constants
Keyword const
It is somewhat misleading.
It does not define a constant array. It defines a constant reference to the array.
Therefore, we can still change the elements of the constant array.
Elements can be reassigned
You can change elements of constant arrays:
Example
// You can create constant arrays: const cars = ["Saab", "Volvo", "BMW"]; // You can change elements: cars[0] = "Toyota"; // You can add elements: cars.push("Audi");
Browser support
Internet Explorer 10 or earlier versions do not support const
Keyword.
The following table indicates the first browser version that fully supports the const keyword:
Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome 49 | IE 11 / Edge | Firefox 36 | Safari 10 | Opera 36 |
March 2016 | October 2013 | February 2015 | September 2016 | March 2016 |
Assignment at declaration
JavaScript const
Variables must be assigned a value at the time of declaration:
Means: use const
The declared array must be initialized at the time of declaration.
Using const
And not initializing the array is a syntax error:
Example
Does not work:
const cars; cars = ["Saab", "Volvo", "BMW"];
Using var
The declared array can be initialized at any time.
You can even use the array before declaration:
Example
No problem:
cars = ["Saab", "Volvo", "BMW"]; var cars;
const block scope
Using const
The declared array hasBlock scope.
The array declared in the block is different from the array declared outside the block:
Example
const cars = ["Saab", "Volvo", "BMW"]; // Here cars[0] is "Saab" { const cars = ["Toyota", "Volvo", "BMW"]; // Here cars[0] is "Toyota" } // Here cars[0] is "Saab"
Using var
The declared array does not have block scope:
Example
var cars = ["Saab", "Volvo", "BMW"]; // Here cars[0] is "Saab" { var cars = ["Toyota", "Volvo", "BMW"]; // Here cars[0] is "Toyota" } // Here cars[0] is "Toyota"
You can learn more about block scope in the following sections:JavaScript Scope.
in the declaration of an array
You can use redeclare an array at any position in the program var
Redeclare the array:
Example
var cars = ["Volvo", "BMW"]; // Allowed var cars = ["Toyota", "BMW"]; // Allowed cars = ["Volvo", "Saab"]; // Allowed
Not allowed to redeclare or reassign an array to in the same scope or block const
:
Example
var cars = ["Volvo", "BMW"]; // Allowed const cars = ["Volvo", "BMW"]; // Not allowed { var cars = ["Volvo", "BMW"]; // Allowed const cars = ["Volvo", "BMW"]; // Not allowed }
Not allowed to redeclare or reassign an existing array in the same scope or block const
Array:
Example
const cars = ["Volvo", "BMW"]; // Allowed const cars = ["Volvo", "BMW"]; // Not allowed var cars = ["Volvo", "BMW"]; // Not Allowed cars = ["Volvo", "BMW"]; // Not Allowed { const cars = ["Volvo", "BMW"]; // Allowed const cars = ["Volvo", "BMW"]; // Not Allowed var cars = ["Volvo", "BMW"]; // Not Allowed cars = ["Volvo", "BMW"]; // Not Allowed }
Allowed to be used in another scope or another block const
Redeclare the array:
Example
const cars = ["Volvo", "BMW"]; // Allowed { const cars = ["Volvo", "BMW"]; // Allowed } { const cars = ["Volvo", "BMW"]; // Allowed }
Complete Array Reference Manual
For a complete reference, please visit our complete JavaScript Array Reference Manual.
This manual includes descriptions and examples of all array properties and methods.
- Previous Page JS Array Iteration
- Next Page JS Date