Created
December 1, 2016 15:34
-
-
Save jimthedev/e73457d2d3c152e8ee3e457d59840dd7 to your computer and use it in GitHub Desktop.
When are consts actually immutable?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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