What are Variables?
Variables are containers for storing data (storing data values).
3 Ways to Declare a JavaScript Variable
- Using
var
- Using
let
- Using
const
When to Use JavaScript var?
- Always declare JavaScript variables with
var
,let
, orconst
. - The
var
keyword is used in all JavaScript code from 1995 to 2015. - The
let
andconst
keywords were added to JavaScript in 2015. - If you want your code to run in older browsers, you must use
var
When to Use JavaScript const?
- If you want a general rule: always declare variables with
const
. - If you think the value of the variable can change, use
let
.
JavaScript Identifiers ?
All JavaScript variables must be identified with unique names.
These unique names are called identifiers.
JavaScript identifiers are case-sensitive.
The general rules for constructing names for variables (unique identifiers) are:
- Names can contain letters, digits, underscores, and dollar signs.
- Names must begin with a letter.
- Names can also begin with $ and _ (but we will not use it in this tutorial).
- Names are case sensitive (y and Y are different variables).
- Reserved words (like JavaScript keywords) cannot be used as names.