Javascript
JavaScript Fundamentals -- Variables and Scope

var, let, and const

Outline

Variables declared with var:

  • Are function or global scoped
  • Can be used before they're declared
  • Generally not recommended in modern JavaScript files

Variables declared with let:

  • Are block scoped
  • Can’t be used before declared
  • Can have their value reassigned
  • Recommended more than var, especially if you need to reassign the value

Variables declared with const:

  • Are block scoped
  • Can’t be used before declared
  • Can’t have their value reassigned
  • Recommended first choice for declaring variables

These are my personal opinions. There's no "right" or "wrong" way to declare the variables, but it's important to choose a method and then stick with it.

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

 

I finished! On to the next chapter