- Eliminates some silent errors by changing them to errors.
- Prohibits some syntax likely to be defined in the future.
To use the strict mode, we need to add the following as the first line in the code.
WARNING: All the code presented here runs in strict mode. Some parts will work differently if you run them in non-strict mode!
'use strict'; /* Multi line comment */ // Single line comment
Add comments for complicated code, you will thank yourself later when you have to fix a bug in that code, and you forgot what that piece of code is supposed to do or why it is doing that, or how it is doing that.
To see the console output in the browser, open the Developer Tools to the right and switch to the “Console” tab. While following these lessons, you should keep the Developer Tools Console open. See the previous part of this tutorial to learn how to open the console.
If you are using a nice browser like Chrome or Firefox, the console output is displayed on the right side of the editor so you don’t have to open the Developer Tools Console.
There are many more console methods, but these are the most used.
When console methods are not enough for debugging, you can use the
debugger statement. The interpreter will stop at that line of code:
console.log("Before debugger"); debugger; console.log("After debugger");
When this code runs, you’ll see only “Before debugger” in the console and the browser’s debugging controls will be displayed.
“When the creators built the machine, they put in the processor and the memory. From these arise the two aspects of the program. The aspect of the processor is the active substance. It is called Control. The aspect of the memory is the passive substance. It is called Data.” — Master Yuan-Ma, The Book of Programming
The purpose of any program is to transform data. So any program needs data. Data can have different roles or meanings, it can be of different types. A value is a piece of data of a specific type.
Values are things that are. Types are things that could be.
Here are some examples:
Let’s see how you can make values:
'use strict'; // Need a number? Just type it and it will be summoned 7; // Want to do arithmetic? The result will be another number. // Here is how to add 7 with 3 7 + 3; // Or how to multiply 5 with 5 5 * 5; console.log( 7 + 3 ) console.log( 5 * 5 ) // Need to compare some numbers? The result will be a boolean, true or false 7 >= 3; 5 < 5; console.log( 7 >= 3 ) console.log( 5 < 5 ) // Need a string? You can create it in different ways, using " ", ' ' or ` `: "Below the surface of the machine, the program moves."; 'Without effort, it expands and contracts'; `In great harmony, electrons scatter and regroup. The forms on the monitor are but ripples on the water. The essence stays invisibly below. -- Master Yuan-Ma, The Book of Programming`;
To summon a value, you just have to type it.
Having values is not enough. We need to give them a meaning, we need to name them. For example, a
7 can have different meanings,
depending on how we use it in a piece of code, like the day of the week, the maximum of a series of numbers, the sum, the position in a list, and so on.
// what is this 7? 7
Another problem we have with values is that we can’t use a previous value because we have no way of grasping it.
// sum 7 + 3 // how to add 1 to sum ??
To solve all these issues we can create bindings. A binding is the result of binding a name to a value.
You can bind a value to a name using
After that, you can use the binding name to get the bound value.
You can assign a value to a binding using
=. The same value can be bound to several names.
NOTE: Bindings are also called variables. But calling a constant a variable is kind of confusing. So here we stick with bindings.
Think of a binding not as a box containing a value, but as a tentacle that grasps a value. The same value can be bound to several names, meaning the same value can be grasped by several tentacles not that there are several boxes with the same value.
NOTE: There is another, old way of binding a value, using “var” but nobody uses it anymore since it can cause some strange and unexpected behavior. Please use instead “const” by default, and “let” only if you need to change the binding.
Let’s see how we create some bindings:
// Previously we had to repeat the 7 + 3 operation when we printed the result, // by binding the result of 7 + 3 to 'sum', we can print it using the binding const sum = 7 + 3; console.log(sum) // Bindings that never change are declared with "const" const person = "Dr. Cham"; // Tip: If you attempt to change a const you will get an error // Bindings that change are declared with "let" let whatHeDid = "He did dynamite a retirement home"; // Let's change it whatHeDid = "He did dynamite a retirement home full of grannies"; // You can bind several values in a single line let born = 1894, missing = 1941; // Also you can just declare a "let" binding, without providing a value let died; // You need to provide a value to "const" or you'll get an error: // const wentMad; // Log a binding's value using console.log console.log("person:", person); console.log("born, missing, died :", born, missing, died);
'use strict'; let x = 100; let y = x; // What is happening here? x = 0; // What is y? // console.log(y)
Can you guess what is the value of
let x = 100;means that we summon the value 100 and create a binding for it, called
let y = x;means we create a binding called
yand bind the value that is bound to
x, which means we bind
yto 100, not x.
x = 0;means we summon value 0 and we bind
xto that value.
yis not affected.
yis still bound to 100.
There are values and bindings. A binding binds a name only to a value. To cannot bind a binding to another binding.
- ‘use strict’ runs the code in a mode that fixes some of the old mistakes
- Logging helps you see what’s going on
- Values are the data in your program
- Use bindings to reference data