Modulo operator: Scope of let and const: What is a block?
Modulo operator:
Modulo operator (%) is used to find the remainder when two numbers are divided.
Example:
- Scope of let and const:
- One of the major differences between var and let/const is the scope. However, one might wonder what scope exactly is. The scope of a variable is the segment of the code where the variable can be accessed.
- Variables declared using var have the global scope whereas let and const are blocks scoped. Here, global scope means the value of a variable is accessible globally within the code. On the other hand, block scope means the value of the variable is accessible only within the block where it is declared.
- What is a block?
- A block is nothing but a set of statements enclosed inside curly braces. It is also called a Compound Statement. It is used whenever we want to execute several lines whereas JS allows only one.
- For example, we can use a block after an if condition to execute the block code conditionally. Example of scope for let and const:
In the above picture, it can be observed that variables ‘b’ and ‘c’ cannot be accessed outside the scope where they are declared.
Comments
Post a Comment