JavaScript Array Const
JavaScript Basics

JavaScript Array Const

ECMAScript 2015 (ES6)

In 2015, JavaScript introduced the const keyword, which has become a common practice for declaring arrays.

Example

const fruits = ["Apple", "Banana", "Cherry"];

Cannot be Reassigned

An array declared with const cannot be reassigned to a different array.

Example
const fruits = ["Apple", "Banana", "Cherry"];fruits = ["Orange", "Mango", "Pineapple"]; // ERROR

Arrays are Not Constants

The const keyword defines a constant reference to an array, not a constant array itself. This means you can change the elements of the array, but you cannot reassign the array variable to a new array.

Elements Can be Reassigned

You can change the elements of a constant array.

Example
// Creating a constant arrayconst fruits = ["Apple", "Banana", "Cherry"];
// Changing an elementfruits[0] = "Orange";‍// Adding a new elementfruits.push("Pineapple");

Browser Support

The const keyword is not supported in Internet Explorer 10 or earlier.

The following table shows the first browser versions with full support for the const keyword:

Browser

Version

Chrome

49 (Mar 2016)

IE / Edge

11 (Oct 2013)

Firefox

36 (Feb 2015)

Safari

10 (Sep 2016)

Opera

36 (Mar 2016)

Assigned when Declared

JavaScript const variables must be assigned a value when they are declared. This means an array declared with const must be initialized during declaration.

Example

This will not work:

const fruits;fruits = 
["Apple", "Banana", "Cherry"]; // ERROR

Arrays declared with var can be initialized at any time and even used before they are declared.

Example

This is OK:

fruits = ["Apple", "Banana", "Cherry"];var fruits;

Const Block Scope

An array declared with const has block scope. This means an array declared inside a block is different from an array declared outside the block.

Example
const fruits = ["Apple", "Banana", "Cherry"];// Here fruits[0] is "Apple"{ const fruits = ["Orange", "Mango", "Pineapple"]; // Here fruits[0] is "Orange"}// Here fruits[0] is "Apple"

An array declared with var does not have block scope.

Example
var fruits = ["Apple", "Banana", "Cherry"];// Here fruits[0] is "Apple"{ var fruits = ["Orange", "Mango", "Pineapple"]; // Here fruits[0] is "Orange"}// Here fruits[0] is "Orange"

Redeclaring Arrays

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

Example
var fruits = ["Banana", "Cherry"]; // Allowedvar fruits = ["Orange", "Mango"]; // Allowedfruits = ["Apple", "Pineapple"]; // Allowed

Redeclaring or reassigning an array declared with const in the same scope or block is not allowed.

Example
var fruits = ["Banana", "Cherry"]; // Allowedconst fruits = ["Orange", "Mango"]; // Not allowed{ var fruits = ["Apple", "Pineapple"]; // Allowed const fruits = ["Peach", "Grape"]; // Not allowed}

Redeclaring or reassigning an existing const array in the same scope or block is not allowed.

Example
const fruits = ["Banana", "Cherry"]; // Allowedconst fruits = ["Orange", "Mango"]; // Not allowedvar fruits = ["Apple", "Pineapple"]; 

// Not allowedfruits = ["Peach", "Grape"]; // Not allowed‍{ const fruits = ["Banana", "Cherry"]; // Allowed const fruits = ["Orange", "Mango"]; // Not allowed var fruits = ["Apple", "Pineapple"]; // Not allowed fruits = ["Peach", "Grape"]; // Not allowed}

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

Example
javascriptCopy codeconst fruits = ["Banana", "Cherry"]; 
// Allowed{ const fruits = ["Orange", "Mango"]; // Allowed}{ const fruits = ["Apple", "Pineapple"]; // Allowed}

Take a look into your desired course