bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/JavaScript/Working with Data
JavaScript•Working with Data

JavaScript Array 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

const cars = ["Saab", "Volvo", "BMW"];

Cannot be Reassigned

An array declared with const cannot be reassigned:

Example

const cars = ["Saab", "Volvo", "BMW"];
cars = ["Toyota", "Volvo", "Audi"];    // ERROR

Arrays 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

// 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

Meaning: An array declared with const must be initialized when it is declared.

Using const without initializing the array is a syntax error:

This will not work

const 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

cars = ["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

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"

An array declared with var 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 chapter: JavaScript Scope .

Redeclaring Arrays

Redeclaring an array declared with var is allowed anywhere in a program:

Example

var cars = ["Volvo", "BMW"]; // Allowed
var cars = ["Toyota", "BMW"]; // Allowed
cars = ["Volvo", "Saab"]; // Allowed

Redeclaring or reassigning an array to const , in the same scope, or in the same block, is not allowed:

Example

var 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

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
}

Redeclaring an array with const , in another scope, or in another block, is allowed:

Example

const cars = ["Volvo", "BMW"]; // Allowed
{
 const cars = ["Volvo", "BMW"]; // Allowed
}
{
 const cars = ["Volvo", "BMW"]; // Allowed
}

Previous

Regular Expression Assertions

Next

RegExp Quantifiers

This chapter

Overview
37

Lessons

216m

Read time

1. JavaScript Strings2. JavaScript Numbers3. JavaScript Dates4. JavaScript Arrays5. JavaScript Sets6. JavaScript Maps7. JavaScript Math Object8. JavaScript RegExp9. JavaScript String Templates10. JavaScript Number Methods11. JavaScript Date Formats12. JavaScript Array Methods13. JavaScript Set Methods14. JavaScript Map Methods15. JavaScript Random16. JavaScript RegExp Flags17. JavaScript String Methods18. JavaScript Number Properties19. JavaScript Get Date Methods20. JavaScript Array Search21. JavaScript Set Logic22. JavaScript WeakMap23. RegExp Character Classes24. JavaScript String Search25. JavaScript Bitwise Operations26. JavaScript Set Date Methods27. JavaScript Array Sort28. JavaScript WeakSet29. RegExp Meta Characters30. JavaScript BigInt31. JavaScript Array Iterations32. Regular Expression Assertions33. JavaScript Array Const34. RegExp Quantifiers35. JavaScript RegExp Patterns36. JavaScript RegExp Objects37. RegExp Methods

On this page

ECMAScript 2015 (ES6)Cannot be ReassignedArrays are Not ConstantsElements Can be ReassignedAssigned when DeclaredConst Block ScopeRedeclaring Arrays