Javascript

JavaScript Fundamentals -- Variables and Scope

Declaration is when you create a new variable using var, let, or const. By default, declared variables have an initial value of undefined. Initialization is when a variable is given a value the first time. A variable can be used before initialization.

What is scope? Scope determines where a variable in JavaScript can be used. There are three types of scope in JavaScript: global, function, and block.

Variables with global scope in JavaScript are:

  • Usable anywhere in the file where they're declared
  • Declared outside any function or block of code
  • Can cause problems if you're not careful with them

Follow along by forking this StackBlitz project.

For more information on this topic, check out this blog post.