bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/JavaScript/JavaScript Foundations
JavaScript•JavaScript Foundations

JavaScript Arithmetic

JavaScript Arithmetic Operators

Arithmetic operators perform arithmetic on numbers (literals or variables).

OperatorDescription
+Addition
-Subtraction
*Multiplication
**Exponentiation ( ES2016 )
/Division
%Modulus (Remainder)
++Increment
--Decrement

Arithmetic Operations

A typical arithmetic operation operates on two numbers.

Example

let x = 100 + 50;

The two numbers can be literals

or variables:

Example

let x = a + b;

or expressions:

Example

let x = (100 + 50) * a;

Operators and Operands

The numbers (in an arithmetic operation) are called operands .

The operation (to be performed between the two operands) is defined by an operator .

OperandOperatorOperand
100+50

Adding

The addition operator ( + ) adds numbers:

Example

let x = 5;
let y = 2;
let z = x + y;

Subtracting

The subtraction operator ( - ) subtracts numbers.

Example

let x = 5;
let y = 2;
let z = x - y;

Multiplying

The multiplication operator ( * ) multiplies numbers.

Example

let x = 5;
let y = 2;
let z = x * y;

Dividing

The division operator ( / ) divides numbers.

Example

let x = 5;
let y = 2;
let z = x / y;

Remainder

The modulus operator ( % ) returns the division remainder.

Example

let x = 5;
let y = 2;
let z = x % y;

In arithmetic, the division of two integers produces a quotient and a remainder .

In mathematics, the result of a modulo operation is the remainder of an arithmetic division.

Incrementing

The increment operator ( ++ ) increments numbers.

Example

let x = 5;
x++;
let z = x;

Decrementing

The decrement operator ( -- ) decrements numbers.

Example

let x = 5;
x--;
let z = x;

Exponentiation

The exponentiation operator ( ** ) raises the first operand to the power of the second operand.

Example

let x = 5;
let z = x ** 2;

x ** y produces the same result as Math.pow(x,y) :

Example

let x = 5;
let z = Math.pow(x,2);

Operator Precedence

Operator precedence describes the order in which operations are performed in an arithmetic expression.

Example

let x = 100 + 50 * 3;

Is the result of example above the same as 150 * 3, or is it the same as 100 + 150?

Is the addition or the multiplication done first?

As in traditional school mathematics, the multiplication is done first.

Multiplication ( * ) and division ( / ) have higher precedence than addition ( + ) and subtraction ( - ).

And (as in school mathematics) the precedence can be changed by using parentheses.

When using parentheses, the operations inside the parentheses are computed first:

Example

let x = (100 + 50) * 3;

When many operations have the same precedence (like addition and subtraction or multiplication and division), they are computed from left to right:

Examples

let x = 100 + 50 - 3;

For a full list of operator precedence values go to:

JavaScript Operator Precedence Values .

Previous

JavaScript Statements

Next

JavaScript else

This chapter

Overview
51

Lessons

194m

Read time

1. JavaScript Tutorial2. JavaScript Syntax3. JavaScript Operators4. JavaScript if5. JavaScript Loops6. JavaScript Functions7. JavaScript Scope8. JavaScript Data Types9. JavaScript Style Guide10. JavaScript Introduction11. JavaScript Statements12. JavaScript Arithmetic13. JavaScript else14. JavaScript For Loop15. JavaScript Functions16. JavaScript Code Blocks17. JavaScript Primitives18. JavaScript Best Practices19. JavaScript Where To20. JavaScript Comments21. JavaScript Assignment22. The JavaScript Ternary Operator23. JavaScript While Loops24. Invoking JavaScript Functions25. JavaScript Hoisting26. JavaScript Built-In Objects27. JavaScript Common Mistakes28. JavaScript Output29. JavaScript Variables30. JavaScript Comparison31. JavaScript Switch Statement32. JavaScript Break33. JavaScript Function Parameters34. JavaScript Use Strict35. JavaScript typeof36. JavaScript Performance37. JavaScript Let38. JavaScript Conditionals39. JavaScript Booleans40. JavaScript Continue41. JavaScript Function Return42. JavaScript toString()43. JavaScript Const44. JavaScript Logical Operators45. JavaScript Control Flow46. JavaScript Function Arguments47. JavaScript Type Conversion48. JavaScript Datatypes49. JavaScript Function Expressions50. JavaScript Destructuring51. JavaScript Arrow Functions

On this page

JavaScript Arithmetic OperatorsArithmetic OperationsOperators and OperandsAddingSubtractingMultiplyingDividingRemainderIncrementingDecrementingExponentiationOperator Precedence