Nearby lessons

4 of 40

๐Ÿ”ก JavaScript Variables

๐Ÿ“Œ What are JavaScript Variables?

๐Ÿ“Œ JavaScript variables are used to store data values. You can declare them in different ways:

  • ๐Ÿ”น Automatically
  • ๐Ÿ”น Using var
  • ๐Ÿ”น Using let
  • ๐Ÿ”น Using const

๐Ÿ“ var Keyword

  • ๐Ÿ“… The var keyword was used in all JavaScript code from 1995 to 2015.
  • โœจ The let and const keywords were added to JavaScript in 2015.
  • โš ๏ธ Use var only for older browsers.
Example02
โšกCode Cell
Loading Editor...
โœ๏ธ You can edit this code โ€” click Run to refresh the preview.
Output

๐Ÿ“ let Keyword

  • โŒ Variables defined with let cannot be redeclared.
  • โœ… Must be declared before use.
  • ๐Ÿ“ฆ Has block scope.
Example03
โšกCode Cell
Loading Editor...
โœ๏ธ You can edit this code โ€” click Run to refresh the preview.
Output

๐Ÿ“ const Keyword

  • โŒ Variables with const cannot be redeclared or reassigned.
  • ๐Ÿ“ฆ Has block scope.
Example04
โšกCode Cell
Loading Editor...
โœ๏ธ You can edit this code โ€” click Run to refresh the preview.
Output

๐Ÿ“ฆ JavaScript Variable Keywords

AttributeDescription
varโœ… Can be redeclared and updated. Has function or global scope. Use in legacy code.
letโš ๏ธ Cannot be redeclared, but can be updated. Has block scope.
const๐Ÿ”’ Cannot be redeclared or updated. Must be initialized. Has block scope.

๐Ÿง  Test Your Knowledge

3 Questions
Progress: 0 / 3