Scope
In JavaScript, scope
refers to the range
within which variables can be accessed.
The scope of var
ignores the code block enclosed by curly braces { }
.
if (true) { var name = "one"; } console.log(name); // "one"
In the code above, the name
variable declared with var
is defined within the if
block, but it can also be accessed outside the if
block.
Note: The
if
keyword is used to create conditional statements. The condition is placed inside the parentheses, and the code to be executed is placed within the curly braces{ }
. In the conditional statement above, sincetrue
is always true, the code inside theif
block is executed.
Types of scopes include global scope
, local scope
, block scope
, and function scope
.
We will explore the characteristics of each type of scope with practical examples later on.
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help
Code Editor
Execution Result