bradley653
1h ago • 0 views
Hey everyone! 👋 Struggling a bit with understanding how variables work in different parts of your code? Especially when jumping between Python and JavaScript? You're not alone! Variable scope can be super tricky, but it's fundamental to writing clean, bug-free code. I've put together a quick study guide and a quiz to help you really nail it down. Let's conquer scope together! 💻
💻 Computer Science & Technology
1 Answers
✅ Best Answer
jessica543
Mar 15, 2026
📚 Quick Study Guide: Variable Scope Fundamentals
- 💡 What is Scope? Scope defines the accessibility of variables, functions, and objects in some particular part of your code during runtime. It determines where a variable can be referenced or used.
- 🌍 Global Scope: Variables declared outside of any function or block. They are accessible from anywhere in the code.
- 🏠 Local (Function) Scope: Variables declared inside a function. They are accessible only within that function. In Python, this is the primary local scope.
- 🧱 Block Scope (JavaScript 'let'/'const'): Variables declared with `let` or `const` inside a `{}` block (e.g., if statements, for loops) are only accessible within that block. Python does not have block scope in the same way; variables in if/for blocks are still function-scoped if inside a function.
- ⬆️ Lexical (Static) Scope: Most modern languages (Python, JavaScript) use lexical scope, meaning the scope of a variable is determined by its position in the source code at the time of definition, not at runtime. Inner functions can access variables of their outer (enclosing) functions.
- 🔄 Variable Shadowing: Occurs when a variable in an inner scope has the same name as a variable in an outer scope. The inner variable "shadows" the outer one, making the outer one inaccessible from within the inner scope.
- 🚫 Hoisting (JavaScript Specific): `var` declarations are "hoisted" to the top of their function or global scope, but their assignments are not. `let` and `const` are also hoisted, but they enter a "Temporal Dead Zone" until their actual declaration, preventing access before definition. Python does not have hoisting.
🧠 Practice Quiz: Test Your Scope Skills
1. Consider the following Python code:
x = 10
def outer():
x = 20
def inner():
print(x)
inner()
outer()
What will be printed when the `outer()` function is called?
- A) 10
- B) 20
- C) Error
- D) None
2. What is the output of the following JavaScript code?
function greet() {
var message = "Hello";
if (true) {
var message = "Hi";
console.log(message);
}
console.log(message);
}
greet();
- A) Hi, Hello
- B) Hi, Hi
- C) Hello, Hi
- D) Hello, Hello
3. In Python, which keyword is used to explicitly modify a global variable from within a function?
- A) `local`
- B) `global`
- C) `nonlocal`
- D) `this`
4. Which of the following JavaScript variable declarations introduces block scope?
- A) `var`
- B) `let`
- C) `const`
- D) Both B and C
5. What is the output of this Python code?
def func():
y = 5
def inner_func():
nonlocal y
y = 10
inner_func()
print(y)
func()
- A) 5
- B) 10
- C) Error
- D) None
6. Regarding JavaScript's 'hoisting', which statement is true for `let` and `const`?
- A) They are hoisted and can be accessed before their declaration.
- B) They are not hoisted at all.
- C) They are hoisted but are in a "Temporal Dead Zone" until declared.
- D) They behave identically to `var` in terms of hoisting.
7. Which statement accurately describes a key difference in scope between Python and JavaScript?
- A) Both languages have identical block scope behavior.
- B) Python primarily uses function scope, while JavaScript (with `let`/`const`) has block scope.
- C) JavaScript variables are always globally scoped by default, unlike Python.
- D) Python has hoisting for all variable types, while JavaScript does not.
Click to see Answers
1. B) 20 (Python uses lexical scoping; `inner` sees `x` from `outer`'s scope first.)
2. B) Hi, Hi (`var` has function scope, so the inner `var message` re-declares and updates the same `message` variable in the `greet` function scope.)
3. B) `global`
4. D) Both B and C (`let` and `const` introduce block scope.)
5. B) 10 (`nonlocal` allows modifying a variable in an enclosing scope that is not global.)
6. C) They are hoisted but are in a "Temporal Dead Zone" until declared.
7. B) Python primarily uses function scope, while JavaScript (with `let`/`const`) has block scope.
Join the discussion
Please log in to post your answer.
Log InEarn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! 🚀