How to Write JavaScript Number Variables

Hey - Nick here! This page is a free excerpt from my $99 course JavaScript Fundamentals.

If you want the full course, click here to sign up and create an account.

I have a 30-day satisfaction guarantee, so there's no risk (and a ton of upside!) in signing up for this course and leveling up your JavaScript developer skills today!

In this lesson, you'll learn how to work with numbers in JavaScript.

How to Write Number Variables in JavaScript

Let's start by discussing the basics of writing number variables in JavaScript.

Many programming languages have multiple data types to store numeric values.

Specifically, programming languages generally divide quantitative variables into (at least) two categories:

  • Integers, often labelled as int
  • Floating-point numbers, often labelled as float

JavaScript only has one data type for dealing with numbers. It is aptly called number.

We can create a number variable in the same way that we created string variables previously - with either the var, let, or const keywords:

var year = 2020;

let age = 20;

const carYear = 2009;

Let's move on to discussing how to perform operations on number variables in JavaScript.

How to Perform Mathematical Operations on Number Variables in JavaScript

Like other programming languages, JavaScript has the built-in ability to perform mathematical operations on quantitative data stored in variables.

Let's see a few examples. We will use the following two variables to demonstrate mathematical operations in JavaScript:

var1 = 10;

var2 = 5;

How to Perform Addition in JavaScript

JavaScript uses the + to denote addition.

Here is how we could add var1 and var2 together:

var1 + var2;

Here's what this code would return:

15

How to Perform Subtraction in JavaScript

JavaScript uses the - character to denote subtraction.

Here's how we could subtract var2 from var1:

var1 - var2;

This code would return 5.

How to Perform Multiplication in JavaScript

JavaScript uses the * character to denote multiplication.

We can multiply the var1 and var2 variables together with the following JavaScript statement:

var1 * var2;

This statement would return 50.

How to Perform Division in JavaScript

JavaScript uses the / character to denote division.

Here's how we could divide var1 by var2 in JavaScript using the / operator:

var1 / var2;

This statement would return 2.

How to Calculate Exponents in JavaScript

JavaScript uses the ** character to denote exponents. For example, here's how you would raise var1 to the power of var2:

var1 ** var2;

The output of this code is 100000, which is 10 raised to the power of 5.

How to Calculate Remainders in JavaScript

The remainder function is a lesser-known mathematical function defined as the polynomial “left over” after dividing one polynomial by another. As an example, the remainder of 18 divided by 4 is 2, because 16 divides evenly into 4 and 18 minus 16 is 2.

JavaScript uses the % character to calculate remainders. As an example, you can use the following statement to calculate the remainder after dividing var1 by var2:

var1 % var2;

The output of this code is actually 0 since var1 actually divides evenly into var2.

Let's consider another example to make sure that you have a firm understanding of this topic:

var1 % 9;

This statement returns 1.

Now that we have discussed how to perform all of the basic mathematical operations in JavaScript, let's discuss some more advanced mathematical capabilities.

Introduction to the Math Object in JavaScript

JavaScript comes with a built-in Math object that we can use to call some advanced mathematical functions.

We have not discussed objects yet in this course. They are blocks of JavaScript code that contain both data and functions. Data within a JavaScript object is called an attribute. Functions within JavaScript objects are called methods.

Attributes and methods within JavaScript objects can be accessed with the . character, which is called the dot operator.

Throughout the rest of this tutorial, we will see how to access attributes and methods from within the Math object using the dot operator.

How to Round Numbers in JavaScript

JavaScript's Math object contains a built-in method called round that allows you to easily round numbers to the nearest integer.

Here are a few examples:

Math.round(2.1)

//Returns 2

Math.round(2.9)

//Returns 3

Math.round(2.5)

//Returns 3

There are certain cases in which you will want to force a number to round up or round down when the Math.round method will normally cause the opposite behavior. We will learn how to handle these two situations in the next two sections of this tutorial.

How to Round Numbers Down in JavaScript

The JavaScript Math object contains a method called Math.floor that allows you to round numbers down.

Here are a few examples of the Math.floor method in action:

Math.floor(2.1)

//Returns 2

Math.floor(2.9)

//Returns 2

How to Round Numbers Up in JavaScript

The JavaScript Math object contains a method called Math.ceil that allows you to round numbers up.

Here are a few examples of the Math.ceil method in action:

Math.ceil(2.9)

//Returns 3

Math.ceil(2.1)

//Returns 3

How to Generate Random Numbers in JavaScript

There are a number of situations in which you will want to generate random numbers using JavaScript.

The Math object solves this problem with the Math.random method, which does not require any parameters to be passed into it and returns a random number from 0 to 1.

You can also multiply to Math.random method by an integer to get a random number that ranges from 0 to that integer.

Here are a few examples of the Math.random method in action:

//Generate a random number from 0 to 1

Math.random()

//Generate a random number from -1 to 0

-Math.random()

//Generate a random number from 0 to 10

Math.random() * 10

Final Thoughts

In this lesson, you learned how to write number variables in JavaScript. You also learned the most fundamental mathematical operations and how to implement them in JavaScript.

Here is a summary of the topics discussed in this lesson:

  • How to write number variables in JavaScript
  • How to perform addition, subtraction, multiplication, and division
  • The exponent and remainder operations in JavaScript
  • How to round numbers in JavaScript (including forced rounding with Math.floor and Math.ceil)
  • How to generate random numbers in JavaScript