Codectionary / Developer documentation / JavaScript

Arithmetic Operators

Arithmetic operators perform mathematical calculations on numbers: addition (+), subtraction (-), multiplication (*), division (/), remainder/modulo (%), and exponentiation (**). The + operator also performs string concatenation when either operand is a string. Increment (++) and decrement (--) adjust a variable by one.

Syntax

a + b, a - b, a * b, a / b, a % b, a ** b

Examples

Basic Arithmetic

The core arithmetic operators in action.

console.log(10 + 5);  // 15
console.log(10 - 5);  // 5
console.log(10 * 5);  // 50
console.log(10 / 5);  // 2
console.log(10 % 3);  // 1 (remainder)
console.log(2 ** 3);  // 8 (2 to the power of 3)

Increment, Decrement, and String Concatenation

Common gotchas with + and the increment/decrement operators.

let count = 5;
count++; // count is now 6
count--; // count is now 5 again

console.log(1 + 1);     // 2 (numeric addition)
console.log("1" + 1);   // "11" (string concatenation)
console.log("5" - 1);   // 4 (- forces numeric conversion)

Best practices

  • Be careful with + when mixing strings and numbers - it concatenates rather than adds if either side is a string
  • Use Number() or parseInt()/parseFloat() to explicitly convert strings to numbers before arithmetic when the source is uncertain
  • Prefer x += 1 or x++ consistently within a codebase for readability
  • Watch for floating-point precision issues (e.g., 0.1 + 0.2 !== 0.3) when comparing decimal results directly

At a glance

Purpose
Application logic and interaction
File extension
.js ยท .mjs
Runs in
Browsers and JavaScript runtimes
Usually used with
HTML, CSS and Web APIs

Specifications & further reading

Related JavaScript documentation