JavaScript Variables
A variable is a "container" used to store information.
In JavaScript, variables are used to store data and can be dynamically changed during program execution.
In JavaScript, variables can store various types of data, such as numbers, strings, objects, functions, and so on.
A variable name is an identifier used to reference the data stored in the variable.
In JavaScript, you can declare variables using the var, let, and const keywords.
-
var: a variable declaration introduced in ES5, with function scope. -
let: a variable declaration introduced in ES6, with block scope. -
const: a constant declaration introduced in ES6, with block scope and an immutable value.
Example
var
x
=
5
;
var
y
=
6
;
var
z
=
x
y
;
Try it »
Just like in algebra
x=5
y=6
z=x+y
In algebra, we use letters (such as x) to hold values (such as 5).
Through the expression z=x+y above, we can calculate that the value of z is 11.
In JavaScript, these letters are called variables.
![]() |
You can think of a variable as a container for storing data. |
|---|
JavaScript Variables
Like algebra, JavaScript variables can be used to hold values (such as x = 5) and expressions (such as z = x + y).
Variables can use short names (such as x and y) or more descriptive names (such as age, sum, totalvolume).
- Variable names must start with a letter
- Variable names can also start with $ and _ symbols (though we do not recommend this)
- Variable names are case-sensitive (y and Y are different variables)
![]() |
Both JavaScript statements and JavaScript variables are case-sensitive. |
|---|
JavaScript Data Types
JavaScript variables can also hold other data types, such as text values (name="Bill Gates").
In JavaScript, a piece of text like "Bill Gates" is called a string.
JavaScript variables have many types, but for now we only focus on numbers and strings.
When you assign a text value to a variable, you should surround the value with double or single quotes.
When the value you assign to a variable is numeric, do not use quotes. If you surround the numeric value with quotes, it will be treated as text.
Example
var
pi
=
3.14
;
//
If you are familiar with ES6, pi can use the const keyword, representing a constant
//
const pi = 3.14;
var
person
=
"
John Doe
"
;
var
answer
=
'
Yes I am!
'
;
Try it »
Declaring (creating) JavaScript variables
Creating a variable in JavaScript is usually called "declaring" a variable.
We use the var keyword to declare a variable:
var carname;
After a variable is declared, it is empty (it has no value).
To assign a value to a variable, use the equals sign:
carname="Volvo";
However, you can also assign a value to a variable when declaring it:
var carname="Volvo";
In the following example, we create a variable named carname, assign it the value "Volvo", and then place it in an HTML paragraph with id="demo":
Example
var
carname
=
"
Volvo
"
;
document
.
getElementById
(
"
demo
"
)
.
innerHTML
=
carname
;
Try it »
** Features of var declaration:**
- A variable can be declared repeatedly (overwriting the original variable).
- When a variable is not assigned a value, its default value is undefined.
- Variables declared with var are hoisted, but not initialized.
![]() |
A good programming habit is to declare all needed variables at the beginning of the code. |
|---|
Multiple variables in one statement
You can declare many variables in one statement. The statement starts with var and separates the variables with commas:
var lastname="Doe", age=30, job="carpenter";
A declaration can also span multiple lines:
var lastname="Doe",
age=30,
job="carpenter";
Multiple variables declared in one statement cannot be assigned the same value at the same time:
var x, y, z = 1;
x and y are undefined, z is 1.
Value = undefined
In computer programs, it is common to declare variables without values. A variable declared without a value actually has the value undefined.
After executing the following statement, the value of the variable carname will be undefined:
var carname;
Redeclaring JavaScript variables
If you redeclare a JavaScript variable, the variable's value is not lost:
After the following two statements are executed, the value of the variable carname is still "Volvo":
var
carname="Volvo";
var carname;
JavaScript arithmetic
You can do arithmetic with JavaScript variables, using operators such as = and +:
Example
y
=
5
;
x
=
y
2
;
Try it »
Using let and const (ES6)
Before 2015, we used the var keyword to declare JavaScript variables.
JavaScript versions after 2015 (ES6) allow us to use the const keyword to define a constant, and the let keyword to define a block-scoped variable.
let
let is a new variable declaration method introduced in ES6 and is recommended.
let syntax:
let variableName = value;
Example
let city
=
"Beijing"
;
let age
=
30
;
console.
log
(
city
,
age
)
;
// output: Beijing 30
const
const is used to define a constant, that is, once assigned, the variable's value cannot be modified.
const syntax:
const variableName = value;
Example
const
z
=
10
;
// z = 20; // error, a constant cannot be reassigned
if
(
true
)
{
const
z
=
20
;
// different constants
console.
log
(
z
)
;
// output 20
}
console.
log
(
z
)
;
// Output 10
For more on const and let, see:JavaScript let and const.
Safari 10 and Edge 14 were the first browsers to support all ES6 features:
| Chrome 58 | Edge 14 | Firefox 54 | Safari 10 | Opera 55 |
| Jan 2017 | Aug 2016 | Mar 2017 | Jul 2016 | Aug 2018 |
You will learn more about JavaScript operators in later chapters of this tutorial.
