Start learning free

LESSON 16 OF 410 · CALCULATIONS WITH NUMBERS

Arithmetic Operators in JavaScript

Lesson Read the full explanation

TL ; DR:

  • We can tell computers to perform calculations for us using +, -, *, /:

    console.log(2 + 6); // Prints 8
    console.log(10 - 7); // Prints 3
    console.log(3 * 3); // Prints 9
    console.log(6 / 2); // Prints 3
    


  • We can also use variables. In these examples, JavaScript will replace variable num with its value 6 to do the calculations:

    let num = 6;
    console.log(num + 5); // Prints 11
    console.log(10 - num); // Prints 4
    console.log(3 * num); // Prints 18
    console.log(num / 2); // Prints 3
    



Full lesson:

Computers absolutely excel at performing calculations. The "compute" in their name comes from their historical association with providing answers to mathematical questions.

These calculations can be performed either directly with numbers or with variables that are assigned numeric values.


Arithmetic operators

Arithmetic operators are used with numeric values to perform common mathematical operations:

Operator Name Examples
+ Addition 3 + 5,  x + 5,  3 + y,  x + y
- Subtraction 8 - 3,  8 - y,  x - 3,  x - y
* Multiplication 3 * 5,  3 * y,  x * 5,  x * y
/ Division 8 / 4,  8 / y,  x / 8,  x / y
% Modulus 8 % 4,   8 % y,  x % 4,  x % y
** Exponentiation 3 ** 2,  3 ** y,   x ** 3,  x ** y


Note that performing arithmetic on variables does not change the variable - you can only update a variable using the = sign.

Let's go through some examples together to better understand how each operator works:


Addition

// Declare 2 variables and initialize them:
let a = 2;
let b = 3;

console.log(a + b); // Output: 5

console.log(a + 10); // Output: 12

Subtraction

// Declare 2 variables and initialize them:
let a = 2;
let b = 3;

console.log(a - b); // Output: -1

console.log(10 - b); // Output: 7

Multiplication

// Declare 2 variables and initialize them:
let a = 2;
let b = 3;

console.log(a * 5); // Output: 10

console.log(-2 * a * b); // Output: -12

Division

// Declare 2 variables and initialize them:
let a = 2;
let b = 3;

console.log(b / a); // Output: 1.5

console.log(a / 10); // Output: 0.2

Exponentiation

The ** operator performs the exponential calculation (2**3 translates to 2 * 2 * 2):

// Declare 2 variables and initialize them:
let a = 2;
let b = 3;

console.log(2 ** 3); // Output: 8

console.log(b ** a); // Output: 9

We left the modulo operator (%) for the next lesson.


Assignment
Follow the Coding Tutorial and let's practice with arithmetic operators!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    JAVASCRIPT · CALCULATIONS WITH NUMBERS

    Arithmetic Operators in JavaScript, explained

    JavaScript Arithmetic Operators: Do Math in Your Programs

    Every app does math — an e-commerce site calculates totals, a game tracks scores, a dashboard computes averages. JavaScript's arithmetic operators are how you make numbers work for you. They're simple to learn and open the door to building anything that involves numerical data.

    The 6 Arithmetic Operators in JavaScript

    OperatorNameExampleResult
    +Addition7 + 310
    -Subtraction7 - 34
    *Multiplication7 * 321
    /Division7 / 23.5
    %Modulus (remainder)7 % 31
    **Exponentiation2 ** 8256

    Using Operators With Variables

    Operators work with raw numbers or variables — JavaScript substitutes the variable's current value:

    let price = 49.99;
    let quantity = 3;
    
    let subtotal = price * quantity;
    console.log(subtotal);              // 149.97
    
    let discount = subtotal * 0.1;
    let total = subtotal - discount;
    console.log(total.toFixed(2));      // "134.97"
    

    The Modulus Operator: More Useful Than You Think

    % returns the remainder after division. It's used constantly in real code:

    // Even or odd?
    console.log(10 % 2);   // 0  → even
    console.log(7 % 2);    // 1  → odd
    
    // Distribute items into rows of 4:
    const items = 29;
    const perRow = 4;
    console.log(`Full rows: ${Math.floor(items / perRow)}`);  // 7
    console.log(`Leftover: ${items % perRow}`);               // 1
    
    // Convert seconds to minutes and seconds:
    const totalSeconds = 137;
    const mins = Math.floor(totalSeconds / 60);
    const secs = totalSeconds % 60;
    console.log(`${mins}m ${secs}s`);   // 2m 17s
    

    Order of Operations (PEMDAS)

    JavaScript follows standard math precedence. Use parentheses to be explicit:

    console.log(2 + 3 * 4);       // 14  (multiplication first)
    console.log((2 + 3) * 4);     // 20  (parentheses first)
    
    // Calculate average — parentheses matter:
    const a = 10, b = 20, c = 30;
    const wrong   = a + b + c / 3;    // 43.33 — divides only c by 3!
    const correct = (a + b + c) / 3;  // 20    — divides the sum
    

    Real-World Examples

    // Compound interest: A = P(1 + r)^t
    const principal = 1000;
    const rate = 0.05;
    const years = 10;
    const amount = principal * (1 + rate) ** years;
    console.log(amount.toFixed(2));   // 1628.89
    
    // Temperature conversion:
    const celsius = 30;
    const fahrenheit = (celsius * 9/5) + 32;
    console.log(fahrenheit);   // 86
    
    // Calculate grade percentage:
    const earned = 87;
    const possible = 100;
    const grade = (earned / possible) * 100;
    console.log(`${grade}%`);  // 87%
    

    JavaScript-Specific Gotchas

    • + with strings concatenates, not adds: "5" + 3 gives "53", not 8. Always ensure your values are numbers before doing math.
    • Floating-point precision: 0.1 + 0.2 gives 0.30000000000000004 in JavaScript (a quirk of binary floating-point). Use .toFixed(2) for display or multiply by 100 for money calculations.
    • Division never throws: 10 / 0 returns Infinity, not an error. Check your denominators.
    // String + number:
    console.log("5" + 3);     // "53"  (string concatenation!)
    console.log(Number("5") + 3);  // 8  (correct)
    console.log(+"5" + 3);    // 8  (unary + converts to number)
    
    // Float precision:
    console.log(0.1 + 0.2);              // 0.30000000000000004
    console.log((0.1 + 0.2).toFixed(2)); // "0.30"
    

    Key Takeaways

    • +, -, *, / work as expected; % gives the remainder; ** raises to a power.
    • JavaScript follows standard operator precedence — use parentheses to be explicit.
    • The + operator concatenates strings — convert to numbers first when doing math.
    • Floating-point results aren't always exact — use .toFixed() for display.

    What to Learn Next

    Now that you can compute values, explore assignment operators (+=, -=, *=) to update variables concisely, and comparison operators (===, >, <) to start making decisions with if statements.

    Keep going with JavaScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free