let caught =5*5;
That’s a second kind of statement. The special word (keyword) let indicates that this sentence is going to define a binding. It is followed by the name of the binding and, if we want to immediately give it a value, by an = operator and an expression.
The previous statement creates a binding called caught and uses it to grab hold of the number that is produced by multiplying 5 by 5.
After a binding has been defined, its name can be used as an expression. The value of such an expression is the value the binding currently holds. Here’s an example:
let ten = 10;
console.log(ten * ten);
// → 100
When a binding points at a value, that does not mean it is tied to that value forever. The = operator can be used at any time on existing bindings to disconnect them from their current value and have them point to a new one.
let mood = “light”;
// → light
mood = “dark”;
// → dark
You should imagine bindings as tentacles, rather than boxes. They do not contain values; they grasp them—two bindings can refer to the same value. A program can access only the values that it still has a reference to. When you need to remember something, you grow a tentacle to hold on to it or you reattach one of your existing tentacles to it.
Let’s look at another example. To remember the number of dollars that Luigi still owes you, you create a binding. And then when he pays back $35, you give this binding a new value.
let luigisDebt = 140;
luigisDebt = luigisDebt – 35;
// → 105
When you define a binding without giving it a value, the tentacle has nothing to grasp, so it ends in thin air. If you ask for the value of an empty binding, you’ll get the value undefined.
A single let statement may define multiple bindings. The definitions must be separated by commas.
let one = 1, two = 2;
console.log(one + two);
// → 3
The words var and const can also be used to create bindings, in a way similar to let.
var name = “Ayda”;
const greeting = “Hello “;
console.log(greeting + name);
// → Hello Ayda
The word const stands for constant. It defines a constant binding, which points at the same value for as long as it lives. This is useful for bindings that give a name to a value so that you can easily refer to it later.
2. Binding Names
Binding names can be any word. Digits can be part of binding names— catch22 is a valid name, for example—but the name must not start with a digit. A binding name may include dollar signs ($) or underscores (_) but no other punctuation or special characters.
break case catch class const continue debugger default
delete do else enum export extends false finally for
function if implements import interface in instanceof let
new package private protected public return static super
switch this throw true try typeof var void while with yield
Don’t worry about memorizing this list. When creating a binding produces an unexpected syntax error, see whether you’re trying to define a reserved word
No Starch Press; 3rd edition.