1. What is global scope in JavaScript?
Answer:
Explanation:
Global scope refers to variables that are declared outside any function or block and are accessible from any part of the code.
2. What defines the function scope in JavaScript?
Answer:
Explanation:
Function scope is defined by variables declared within a function. These variables are accessible only within the function and not outside it.
3. How does block scope work in JavaScript?
Answer:
Explanation:
Block scope means that variables declared within a block (e.g., within curly braces of if statements, loops) are only accessible within that block.
4. Which statement about let and const in JavaScript is true?
Answer:
Explanation:
The let and const keywords in JavaScript declare variables that are block scoped, meaning they are only accessible within the block they are defined in.
5. What happens to a variable declared with var inside a function?
Answer:
Explanation:
Variables declared with var inside a function are function scoped, meaning they are accessible only within the function and not outside it.
6. Can a variable declared in a block with let be accessed outside of the block?
Answer:
Explanation:
Variables declared with let within a block cannot be accessed outside that block as they are block scoped.
7. What is a typical use case for block scope in JavaScript?
Answer:
Explanation:
Block scope is typically used to restrict access to variables within certain blocks of code like loops or conditional statements for better control and to avoid conflicts.
8. What type of scope does the var keyword create in a loop in JavaScript?
Answer:
Explanation:
The var keyword, even when used in a loop, creates a function scope if the loop is within a function, and global scope if the loop is in the global context.
9. How does global scope affect variables in JavaScript?
Answer:
Explanation:
Global scope allows variables to be accessible from any part of the code, regardless of blocks or functions.
10. What is the best practice regarding global scope in JavaScript?
Answer:
Explanation:
It is best practice to limit the use of global scope as it can lead to conflicts and difficulties in managing the code, especially in larger applications.
11. How are const and let different from var in terms of scope?
Answer:
Explanation:
const and let provide block scope, which confines variables to the block where they are declared. var, however, is function scoped.
12. What is the outcome of using a variable outside its scope?
Answer:
Explanation:
Attempting to use a variable outside its scope results in a reference error because the variable is not accessible outside its defined scope.
13. Is it possible to access a block-scoped variable before its declaration?
Answer:
Explanation:
Accessing a block-scoped variable (declared with let or const) before its declaration results in a ReferenceError due to the temporal dead zone.
14. What is the scope of a variable declared outside any function or block in JavaScript?
Answer:
Explanation:
Variables declared outside any function or block have global scope, making them accessible from anywhere in the code.
15. Can a function access a variable in the scope of its calling function?
Answer:
Explanation:
A function cannot access variables in the scope of its calling function unless those variables are passed to it as arguments or are in the global scope.