Assignment
suggest changeUse the assignment operator (=) to give a new value to an existing reassignable binding:
let count = 0;
count = 6;
let label;
label = 'Ready';
Prefer const when the binding will not be reassigned, and initialize it as part of its declaration:
const maximumRetries = 3;
const user = { name: 'Ada' };
const prevents reassignment of the binding, but an object or array held by that binding can still be mutated:
const settings = { theme: 'light' };
settings.theme = 'dark'; // Valid.
// settings = {}; // TypeError: assignment to a constant binding.
Assigning to an undeclared identifier is an error in strict mode and in ECMAScript modules. In legacy sloppy-mode scripts, it may create a property on the global object; never rely on that behavior.
'use strict';
undeclaredName = 5; // ReferenceError
Declare one binding per statement when it improves readability, especially when bindings have different initialization or reassignment behavior:
let x;
const y = 5;
const person = 'John Doe';
let age = 14;
const date = new Date();
A function declaration similarly introduces a binding for the function name.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents