Skip to content

Instantly share code, notes, and snippets.

@jimthedev
Created December 1, 2016 15:34
Show Gist options
  • Select an option

  • Save jimthedev/e73457d2d3c152e8ee3e457d59840dd7 to your computer and use it in GitHub Desktop.

Select an option

Save jimthedev/e73457d2d3c152e8ee3e457d59840dd7 to your computer and use it in GitHub Desktop.
When are consts actually immutable?
// TURNS OUT NOT ALWAYS
const name = "Jim";
name = "James"; // error cannot mutate constants
const age = 1;
age = 2; // Error
const active = true;
active = false // error
const names = ['Jim', 'James'];
names[2] = 'Bob'; // No error, const is shallow
const restaurant = {
name: 'Bobs Burgers'
};
restaurant.name: 'Jims Burgers'; // No error, const is shallow
const firstNames = ['Bob', 'Jim', 'Jon'];
const lastNames = ['Smith', 'Johnson', 'Anderson'];
firstNames = lastNames; // Error! You tried to change a reference!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment