javascript
JavaScript Fundamentals: Variables and Identifiers
Table Of Content
Let's do a quick breakdown of Variables and Identifiers, which are essential for writing JavaScript code.
JavaScript is a Dynamic Type language: variables have no types, and can change typing. Additionally, variables are checked during run-time (the execution stage).
Variables
Variables are basically containers for storing data, and can be declared in 4 ways:
- Automatically
- Using
let
- Using
const
- Using
var
These variables carry rules that a JavaScript writer should remember:
- Always declare variables
- Always use
const
if the VALUE should not be changed - Always use
const
if the TYPE should not be changed (for Arrays and Objects) - Only use
let
if you cannot useconst
- Only use
var
if you need to support older browsers
Identifiers
All JavaScript variables must be identified with unique names.
These unique names are called Identifiers.
Identifiers can be short names (like x and y) or more descriptive names (age, distance, totalWeight).
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 _
- Names are case sensitive (a and A are different variables).
- Reserved words (such as JavaScript keywords) cannot be used as names.