JavaScript Variables
JavaScript variables are containers for data.
Variables = Data Containers
JavaScript variables are containers for data.
JavaScript variables can be declared in 4 ways:
Modern JavaScript
- Using let
- Using const
Older JavaScript
- Using var (Not Recommended)
- Automatically (Not Recommended)
Example using let
Example Code
javascriptlet x = 5;
let y = 6;
let z = x + y;Example using const
Example Code
javascriptconst x = 5;
const y = 6;
const z = x + y;From the examples you can guess:
- x contains (or stores) the value 5
- y contains (or stores) the value 6
- z contains (or stores) the value 11
JavaScript Identifiers
Variables are identified with names called identifiers.
Names can be short like x, y, z.
Names can be descriptive like age, sum, carName.
The rules for constructing names (identifiers) are:
- Names can contain letters, digits, underscores, and dollar signs.
- Names must begin with a letter, a $ sign or an underscore (_).
- Names are case sensitive (X is different from x).
- Reserved words (JavaScript keywords) cannot be used as names.
JavaScript Underscore (_)
JavaScript treats underscore as a letter.
Identifiers containing _ are valid variable names:
Example
Example Code
javascriptlet _lastName = "Johnson";
let _x = 2;
let _100 = 5;A convention among professional programmers is to start a name with underscore for "private" variables.
JavaScript Dollar Sign $
JavaScript also treats a dollar sign as a letter.
Identifiers containing $ are valid variable names:
Example
Example Code
javascriptlet $ = "Hello World";
let $$$ = 2;
let $myMoney = 5;Using the $ is not very common in JavaScript, but professional programmers often use it as an alias for the main function in JavaScript libraries.
Declaring JavaScript Variables
Creating a variable in JavaScript is called declaring a variable.
You declare a JavaScript variable with the let keyword or the const keyword.
Declaring a Variable Using let
Example
Example Code
javascriptlet carName;After the declaration, the variable has no value (technically it is undefined).
To assign a value to the variable, use the equal sign:
Example
Example Code
javascriptcarName = "Volvo";Most often you will assign a value to the variable when you declare it:
Example
Example Code
javascriptlet carName = "Volvo";Declaring a Variable Using const
Always use const if the value should not be changed
Example
Example Code
javascriptconst carName = "Volvo";A Mixed Example
Example Code
javascriptconst price1 = 5;
const price2 = 6;
let total = price1 + price2;The two variables price1 and price2 are declared with the const keyword.
The values of price1 and price2 cannot be changed.
The variable total is declared with the let keyword.
The value of total can be changed.
Declaring a Variable Automatically
Undeclared variables are automatically declared when first used:
Example (Not Recommended)
Example Code
javascriptx = 5;
y = 6;
z = x + y;Declaring a Variable Using var
The var keyword was used in all JavaScript code before 2015.
The let and const keywords were new to JavaScript in 2015.
Using var (Not Recommended)
Example Code
javascriptvar x = 5;
var y = 6;
var z = x + y;JavaScript Data Types
JavaScript variables can hold 8 datatypes, but for now, just think of numbers and strings.
Strings are text written inside quotes.
Numbers are written without quotes.
If you put a number in quotes, it will be treated as a text string.
Example
Example Code
javascriptconst pi = 3.14;
let person = "John Doe";
let answer = 'Yes I am!';One Statement, Many Variables
You can declare many variables in one statement.
Start the statement with let or constand separate the variables by comma:
Example
Example Code
javascriptlet person = "John Doe", carName = "Volvo", price = 200;A declaration can span multiple lines:
Example
Example Code
javascriptlet person = "John Doe",
carName = "Volvo",
price = 200;The Assignment Operator
In JavaScript, the equal sign (=) is an assignment operator, not an equal to operator.
This is different from algebra. The following does not make sense in algebra:
Example
Example Code
javascriptx = x + 5In JavaScript, however, it makes perfect sense: it assigns the value of x + 5 to x.
(It calculates the value of x + 5 and puts the result into x. The value of x is incremented by 5.)
JavaScript Arithmetic
As with algebra, you can do arithmetic with JavaScript variables, using operators like = and +:
Example
Example Code
javascriptlet x = 5 + 2 + 3;You can also add strings, but strings will be concatenated:
Example
Example Code
javascriptlet x = "John" + " " + "Doe";Examples
Example Code
javascriptlet x = "5" + 2 + 3;Video: JavaScript Variables
Interactive Code Examples
Example using let
javascriptlet x = 5;
let y = 6;
let z = x + y;Example using const
javascriptconst x = 5;
const y = 6;
const z = x + y;JS Example 3
javascriptlet _lastName = "Johnson";
let _x = 2;
let _100 = 5;JS Example 4
javascriptlet $ = "Hello World";
let $$$ = 2;
let $myMoney = 5;