Declaring variables in ES6
August 13, 2017 Leave a comment
We don’t use the var keyword in ES6 anymore to declare variables:
var name = 'John'
Instead we have the “let” and “const” keywords for this purpose. With “let” we declare variables whose value can change over time:
let age = 30
…whereas const indicates a variable whose value is not expected to change:
const name = 'John'
We cannot reassign the name variable after this point:
name = 'Bill'
…will result in an error:
TypeError: Assignment to constant variable.
Constant variables must also be given a value immediately. The following is invalid:
const name name = 'John'
View all posts related to JavaScript here.