Variables

Variables: Storing Information

Variables are like labeled boxes where you can store information. You can put something in the box, change what’s inside, or look at what’s stored there.

Three Ways to Declare Variables

Copy to Clipboard

var vs let vs const

  • var: Old way, has some weird behaviors (avoid it)
  • let: Modern way, can be changed, block-scoped
  • const: Cannot be changed after creation, block-scoped

Examples

Copy to Clipboard

Variable Naming Rules

  • Can contain letters, digits, underscore, and dollar sign
  • Must start with a letter, underscore, or dollar sign
  • Cannot use reserved words like let, const, function
  • Are case sensitive

Tip: Use descriptive names! userAge is better than a.