In this section, we will see how to use the most important JS features in React:
- let and const: The new way to declare variables in JavaScript is by using let or const. You can use let to declare variables that can change their value but in block scope. The difference between let and var is that let is a block scoped variable that cannot be global, and with var, you can declare a global variable, for example:
var name = 'Carlos Santana'; let age = 30; console.log(window.name); // Carlos Santana console.log(window.age); // undefined
- The best way to understand "block scope" is by declaring a for loop with var and let. First, let's use var and see its behavior:
for (var i = 1 ; i <= 10; i++) { console.log(i); // 1, 2, 3, ...