Skip to content

Instantly share code, notes, and snippets.

@StevenJL
Created September 10, 2017 03:36
Show Gist options
  • Save StevenJL/47127144a14100528b4e9fb5f2ae5bbf to your computer and use it in GitHub Desktop.
Save StevenJL/47127144a14100528b4e9fb5f2ae5bbf to your computer and use it in GitHub Desktop.
const
// cannot re-assign a const variable
const x = "written in stone";
x = "changed my mind"; // raises TypeError: Assignment to constant variable
// but you can still mutate a const variable
const person = {};
person.name = "Steve";
/*
`const` is great for assigning to a module that is required into code,
so as to prevent the variable holding the module from being accidentally reassigned:
*/
// note 'require' is provided by node.js
const important_library = require("important_library");
important_library = "I totally forgot this was const";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment