Codecademy Team
Variables, Data Types, and Mathematical Operators
Use this article as a reference sheet for JavaScript variables, data types, and mathematical operators.Use this article as a reference sheet for JavaScript variables, data types, and mathematical operators.
- Variables — The
var
keyword indicates the creation of a variable. - Data Types —
'New York City'
is a string,40.7
is a number, andtrue
is a boolean. - Mathematical Operators — The addition (
+
), subtraction (-
), multiplication (*
), and division (/
) characters function as mathematical operators.
var location = 'New York City';var latitude = 40.7;var inNorthernHemisphere = true;
Each line in the example above begins with the keyword var
. The var
keyword is used to create a variable. The word that immediately follows var
is the name of the variable. The variable’s name should describe the value that it stores (e.g. location
).
Variables can store any data type, including:
- String — Any grouping of keyboard characters (letters, spaces, numbers, or symbols) surrounded by single quotes (
'Hello'
) or double quotes ("World!"
). In the example above, the string'New York City'
is saved to the variablelocation
. - Number — Any number, including numbers with decimals:
4
,1516
,.002
,23.42
. In the example above, the number40.7
is saved to the variablelatitude
. - Boolean — Either
true
orfalse
, with no quotations. In the example above, the variableinNorthernHemisphere
is set totrue
. The sample code below contains examples of mathematical expressions:
var num1 = 4;var num2 = 9;var addNumbers = num1 + num2; // addNumbers equals 13var subtractNumbers = num2 - num1; // subtractNumbers equals 5var multiplyNumbers = num1 * num2; // multiplyNumbers equals 36var divideNumbers = num2 / num1; // divideNumbers equals 2.25
In the example above, the values saved to the variables were calculated using the addition (+
), subtraction (-
), multiplication (*
), and division (/
) operators.
Variables always store the final value of the expression on the right side of the equals sign (=
).