JS Arithmetic

JavaScript Arithmetic Operators

JavaScript arithmetic operators are used to perform mathematical calculations on values called operands. You will use them often when adding totals, updating counters, calculating discounts, or building logic for forms, games, and apps.

Most arithmetic operators are binary operators, which means they work with two operands. A few are unary operators, which means they work with only one operand.

What Are Operands in JavaScript?

Operands can be:

Operands: Literals, Variables, and Expressions

let result1 = 3 + 5;   // 3 and 5 are literal values

const x = 3; const y = 5; let result2 = x + y; // x and y are variables

let result3 = 3 + 2 * x; // 2 * x is part of an expression

In general, arithmetic operators are used for math, but some operators can do more. For example, the addition operator + can also join strings.


JavaScript Arithmetic Operators List

Here are the most common arithmetic operators in JavaScript:

Operator Name Description
+ Addition Adds two operands
- Subtraction Subtracts the second operand from the first
* Multiplication Multiplies two operands
/ Division Divides the left operand by the right operand
% Modulus Returns the remainder after division
++ Increment Increases a value by 1
-- Decrement Decreases a value by 1

Arithmetic Operators Overview

let a = 10;
let b = 3;

console.log(a + b); console.log(a - b); console.log(a * b); console.log(a / b); console.log(a % b);


JavaScript Addition (+) Operator

The addition operator + adds two numeric values.

Addition Syntax

let x = 5;
let y = 10;
let sum = x + y; // 15

let value1 = '10' + 3; // "103" let value2 = '10' + '3'; // "103"

The same `+` operator is also used for **string concatenation**.

How JavaScript Handles +

Addition and String Concatenation

const x = 3;
const y = 5;

console.log(x + y); console.log('10' + 3); console.log('10' + '3');


JavaScript Subtraction (-) Operator

The subtraction operator - subtracts the right operand from the left operand.

20 - 10;      // 10
'20' - 10;    // 10
'20' - '10';  // 10
'20ee' - 10;  // NaN

Important Points About Subtraction

Subtraction with Numbers and Strings

console.log(20 - 10);
console.log('20' - '10');
console.log('20ee' - 10);

JavaScript Multiplication (*) Operator

The multiplication operator * multiplies two operands and returns their product.

20 * 10;      // 200
'20' * '10';  // 200
'20ee' * 10;  // NaN

If a string contains only a numeric value, JavaScript converts it to a number before multiplying. If the string is not numeric, the result becomes NaN.

Multiplication Examples

Check the browser console (F12) to see the output.

console.log(20 * 10);
console.log('20' * '10');
console.log('20ee' * 10);

JavaScript Division (/) Operator

The division operator / divides the left operand by the right operand and returns the quotient.

20 / 10; // 2
20 / -10; // -2
100 / 0; // Infinity
0 / 0;   // NaN

This operator is useful for averages, ratios, percentages, and splitting values into equal parts.

Division Examples

Check the browser console (F12) to see the output.

console.log(20 / 10);
console.log('20' / '10');
console.log(100 / 0);
console.log(0 / 0);

JavaScript Modulus (%) Operator

The modulus operator % returns the remainder after division.

5 % 3;  // 2
20 % 9; // 2
20 % 10; // 0

This operator is very useful when you want to:

Modulus Examples

console.log(20 % 9);
console.log(-20 % 9);
console.log(20 % 10);

JavaScript Increment (++) Operator

The increment operator ++ increases the value of a variable by 1. It is a unary operator, so it works on only one operand.

There are two forms:

Prefix Increment

In prefix form, the value is increased first and then used.

let x = 10;
let y = ++x; // x = 11, y = 11

Postfix Increment

In postfix form, the current value is used first and then increased.

let a = 10;
let b = a++; // b = 10, a = 11

Prefix vs Postfix Increment

let x = 10;
let y = ++x;

let a = 10; let b = a++;


JavaScript Decrement (--) Operator

The decrement operator -- decreases the value of a variable by 1. Like increment, it is also a unary operator.

Prefix Decrement

let x = 10;
let y = --x; // x = 9, y = 9

Postfix Decrement

let a = 10;
let b = a--; // b = 10, a = 9

Prefix vs Postfix Decrement

let x = 10;
let y = --x;

let a = 10; let b = a--;


Quick Summary

JavaScript arithmetic operators help you perform calculations on numbers and variables.

Once you understand arithmetic operators well, writing calculations in JavaScript becomes much easier.


Exercise

?

What is the result of the expression 10 % 3 in JavaScript?