Function Scope
A function scope variable is a variable declared inside a function and cannot be accessed outside the function.
Example: In this example, the num variable is declared inside the function and cannot be accessed outside the function.
function fun() {
let num = 10;
console.log(num);
}
fun(); // Calling the function
console.log(num);
Output:
10
"ReferenceError: num is not defined
JavaScript Let
The let keyword in JavaScript is used to make variables that are scoped to the block they’re declared in. Once you’ve used let to define a variable, you cannot declare it again within the same block. It’s important to declare let variables before using them.
The let keyword was introduced in the ES6 or ES2015 version of JavaScript. It’s usually recommended to use let when you’re working with JavaScript.
Syntax:
let variable_name = value;