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 (=
).
Author
'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 teamRelated articles
Learn more on Codecademy
- Free course
Learn C: Variables and Operators
Learn how to store and manipulate your data with C variables and operators.Beginner Friendly2 hours - Free course
Intro to C#
Dive into C#, a scalable programming language that is easy to read and maintain.Beginner Friendly4 hours - Free course
Learn Kotlin: Introduction
Get started with Kotlin by learning everything you need to know to create your first basic program.Beginner Friendly4 hours