Learn JavaScript
Lesson 1 - Introduction To JavaScript
Lesson 2 - Basic JavaScript Syntax
Lesson 3 - Control Flow
Lesson 5 - Basic Data Structures
Lesson 6 - Basic String Operations
Lesson 7 - Basic Array Operations
Lesson 8 - Exception Handling
Lesson 9 - Packages
Lesson 10 - User Input
Variable scope determines from where the variable is accessible. You have two types of variable scope available in JavaScript:
Let's understand them through a practical example:
let globalVar = "I'm a global variable"; function example() { let localVar = "I'm a local variable"; console.log(globalVar); // Accessing global variable console.log(localVar); // Accessing local variable } example(); console.log(globalVar); // Accessing global variable outside the function console.log(localVar); // This will cause an error because localVar is not accessible here
Here, the global variable globalVar
can be accessed from the function example()
, whereas the local variable localVar
is only accessible inside the function example()
.