0
let selected_user = '11';
const arrayOfUsers = ['11', '12', '13'];
console.log(arrayOfUsers); // ['11', '12', '13']
let indexMainUser = arrayOfUsers.indexOf(selected_user);
arrayOfUsers.splice(indexMainUser, 1);
console.log(arrayOfUsers); //  ['12', '13']

I have the above code in JavaScript and I am wondering why does a constant array gets modified with splice method?

  • 2
    const stops the variable from being reassigned. It does not stop you modifying the contents of that variable. – Liam Dec 23 '21 at 10:49
  • 1
    [`const`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const) stops the variable from being *reassigned*. – Nina Scholz Dec 23 '21 at 10:50
  • array and object can be modified but their memory address remains the same. – Amit Kumawat Dec 23 '21 at 11:04

0 Answers0