JavaScript Array Const
In 2015, JavaScript introduced an important new keyword: const.
ECMAScript 2015 (ES6)
In 2015, JavaScript introduced an important new keyword: const.
It has become a common practice to declare arrays using const:
Example
Example Code
javascriptconst cars = ["Saab", "Volvo", "BMW"];Cannot be Reassigned
An array declared with const cannot be reassigned:
Example
Example Code
javascriptconst cars = ["Saab", "Volvo", "BMW"];
cars = ["Toyota", "Volvo", "Audi"]; // ERRORArrays are Not Constants
The keyword const is a little misleading.
It does NOT define a constant array. It defines a constant reference to an array.
Because of this, we can still change the elements of a constant array.
Elements Can be Reassigned
You can change the elements of a constant array:
Example
Example Code
javascript// You can create a constant array:
const cars = ["Saab", "Volvo", "BMW"];
// You can change an element:
cars[0] = "Toyota";
// You can add an element:
cars.push("Audi");Assigned when Declared
JavaScript const variables must be assigned a value when they are declared: Meaning: An array declared with const must be initialized when it is declared.
Using const without initializing the array is a syntax error:
Example
Example Code
javascriptconst cars;
cars = ["Saab", "Volvo", "BMW"];Arrays declared with var can be initialized at any time.
You can even use the array before it is declared:
Example
Example Code
javascriptcars = ["Saab", "Volvo", "BMW"];
var cars;Const Block Scope
An array declared with const has Block Scope.
An array declared in a block is not the same as an array declared outside the block:
Example
Example Code
javascriptconst cars = ["Saab", "Volvo", "BMW"];
// Here cars[0] is "Saab"
{
const cars = ["Toyota", "Volvo", "BMW"];
// Here cars[0] is "Toyota"
}
// Here cars[0] is "Saab"An array declared with var does not have block scope:
Example
Example Code
javascriptvar 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 chapter: JavaScript Scope.
Redeclaring Arrays
Redeclaring an array declared with var is allowed anywhere in a program:
Example
Example Code
javascriptvar cars = ["Volvo", "BMW"]; // Allowed
var cars = ["Toyota", "BMW"]; // Allowed
cars = ["Volvo", "Saab"]; // AllowedRedeclaring or reassigning an array to const, in the same scope, or in the same block, is not allowed:
Example
Example Code
javascriptvar cars = ["Volvo", "BMW"]; // Allowed
const cars = ["Volvo", "BMW"]; // Not allowed
{
var cars = ["Volvo", "BMW"]; // Allowed
const cars = ["Volvo", "BMW"]; // Not allowed
}Redeclaring or reassigning an existing const array, in the same scope, or in the same block, is not allowed:
Example
Example Code
javascriptconst 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
}Redeclaring an array with const, in another scope, or in another block, is allowed:
Example
Example Code
javascriptconst cars = ["Volvo", "BMW"]; // Allowed
{
const cars = ["Volvo", "BMW"]; // Allowed
}
{
const cars = ["Volvo", "BMW"]; // Allowed
}Interactive Code Examples
JS Example 1
javascriptconst cars = ["Saab", "Volvo", "BMW"];JS Example 2
javascriptconst cars = ["Saab", "Volvo", "BMW"];
cars = ["Toyota", "Volvo", "Audi"]; // ERRORJS Example 3
javascript// You can create a constant array:
const cars = ["Saab", "Volvo", "BMW"];
// You can change an element:
cars[0] = "Toyota";
// You can add an element:
cars.push("Audi");JS Example 4
javascriptconst cars;
cars = ["Saab", "Volvo", "BMW"];JS Example 5
javascriptcars = ["Saab", "Volvo", "BMW"];
var cars;JS Example 6
javascriptconst cars = ["Saab", "Volvo", "BMW"];
// Here cars[0] is "Saab"
{
const cars = ["Toyota", "Volvo", "BMW"];
// Here cars[0] is "Toyota"
}
// Here cars[0] is "Saab"