Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do we mean by defining a const array in js? [duplicate]

when we define an array as constant in javascript does it mean that array cannot shrink or enlarge and have a constant size, or, does it mean that all the elements in an array are constant and you cannot change their value.

handleClick(i) {
     const squares = this.state.squares.slice();
     squares[i] = 'X';
     this.setState({squares: squares});
}

in the above code.

like image 238
Aryan Choudhary Avatar asked Jan 25 '26 01:01

Aryan Choudhary


1 Answers

Declaring a variable as const only means that you cannot assign a new value to that variable once a value has been assigned:

const array = [];

array = []; // Not allowed: assignment to constant variable

Declaring an array as const has no bearing on what you can do with the contents of the actual array:

const array = [];

array.push("something"); // Allowed: add value to array
array[0] = "or other";   // Allowed: replace value in array
array.length = 0;        // Allowed: change array size
like image 53
Robby Cornelissen Avatar answered Jan 26 '26 16:01

Robby Cornelissen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!