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, and true 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 variable location.
  • Number — Any number, including numbers with decimals: 4, 1516, .002, 23.42. In the example above, the number 40.7 is saved to the variable latitude.
  • Boolean — Either true or false, with no quotations. In the example above, the variable inNorthernHemisphere is set to true. The sample code below contains examples of mathematical expressions:
var num1 = 4;
var num2 = 9;
var addNumbers = num1 + num2; // addNumbers equals 13
var subtractNumbers = num2 - num1; // subtractNumbers equals 5
var multiplyNumbers = num1 * num2; // multiplyNumbers equals 36
var 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 (=).

Author

Codecademy Team

'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'

Meet the full team