Variable Scope and Hoisting in JavaScript Quiz

IndebtedJudgment avatar
IndebtedJudgment
·
·
Download

Start Quiz

Study Flashcards

Questions and Answers

Which keyword is now preferred for variable declaration in JavaScript?

let

Where can a variable declared with var be accessed?

Within the function it is declared in

What is hoisting in JavaScript?

The process of moving variables and function declarations to the top of their scope before code execution

What is the scope of a variable declared with let?

<p>Block scope</p> Signup and view all the answers

Can a let variable be re-declared within its scope?

<p>No</p> Signup and view all the answers

Study Notes

Variable Declaration in JavaScript

  • let is the preferred keyword for variable declaration in JavaScript.

Variable Scope with var

  • A variable declared with var can be accessed from anywhere in the script, including outside the block it was declared in.

Hoisting in JavaScript

  • Hoisting is a JavaScript mechanism where variable declarations are moved to the top of their scope, regardless of where they are actually declared.
  • This means that a variable can be used before it is declared, but its value will be undefined until it is actually declared.

Variable Scope with let

  • The scope of a variable declared with let is limited to the block it was declared in (e.g., if, for, while, function, etc.).

RedDeclaring let Variables

  • A let variable cannot be re-declared within its scope.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

More Quizzes Like This

Use Quizgecko on...
Browser
Browser