What is the scope of a variable declared with `var` inside a function?
Analysis & Theory
`var` has function scope, meaning it is only accessible within the function it was declared in.
What is the result of this code?
```js
if (true) {
let x = 10;
}
console.log(x);
```
Analysis & Theory
`let` is block-scoped, so `x` is not accessible outside the `if` block.
Which keyword is block-scoped in JavaScript?
Analysis & Theory
`let` and `const` are block-scoped. `var` is function-scoped.
What kind of scope is created when you define a function?
Analysis & Theory
Functions create a new function scope.
What is lexical scope?
A
Scope defined at runtime
B
Scope based on function call order
C
Scope based on where functions are defined
Analysis & Theory
Lexical scope means inner functions access variables from their outer (parent) scope at the time of definition.
What will this code print?
```js
function test() {
var x = 5;
}
console.log(x);
```
Analysis & Theory
`x` is defined inside `test()`, so it's not accessible outside — causes `ReferenceError`.
Where is a global variable accessible?
C
Anywhere in the program
Analysis & Theory
Global variables are accessible throughout the entire code unless shadowed.
Which keyword allows you to declare a **constant** block-scoped variable?
Analysis & Theory
`const` creates block-scoped constants.
What happens when you declare a variable without `var`, `let`, or `const`?
B
It becomes a global variable
Analysis & Theory
In non-strict mode, it becomes a global variable (which is discouraged).
What is shadowing in JavaScript scope?
A
A variable declared with no value
B
A variable hiding another with the same name in a different scope
Analysis & Theory
Shadowing occurs when a variable in a local scope has the same name as a variable in an outer scope.