top of page

Doing Arithmetic in JavaScript

Let's start with numbers. We will keep it simple, really simple. There are three types of numbers you need to know about.

  • Integers are numbers without a fraction. They can either be positive or negative. Ex: 5, 50, or -5.

  • Floating point numbers (floats) have decimal points and decimal places. Ex: 8.5, and 124.86390.

  • Doubles are a specific type of floating point number that have greater precision than standard floating point numbers.

You only need to concentrate about integers and floats as you start to learn JavaScript. Doubles will be explained later for those who are interested.

The arithmetic operators commonly used in JavaScript are listed in the table below.  

Operator
Operation
+
Adding of numbers. Concatenation of strings.
-
Subtraction
*
Multiplication
/
Division
%
Modulus
++
Increment
--
Decrement

Note that the + operator performs two kinds of operation. When dealing with numbers, the + operator adds the numbers and produce a sum. Whereas dealing with strings, the + operator concatenate to produce  a single joined string.

​

Let's look at some code.

The code above will run the arithmetic we have written in the <script> block and show the result in the <div> with the id output. This is done with the document.getElementById("output").inneHTML = outputString

©2022 - 2024 by JavaScript Schools.

bottom of page