#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JavaScript Fundamentals

Arithmetic

Arithmetic operators are the symbols JavaScript uses to do maths: addition, subtraction, multiplication, division, remainder and powers. You use them for everything from totalling a shopping cart to calculating a percentage or working out whether a number is even.

The basic operators

OperatorNameExampleResult
+Addition7 + 310
-Subtraction7 - 34
*Multiplication7 * 321
/Division7 / 23.5
%Remainder7 % 31
**Exponent2 ** 532

Division always gives a decimal result if needed; there is no separate integer division. Use Math.floor(7 / 2) or Math.trunc when you want a whole number.

Remainder and increments in practice

The % operator returns what is left over after division. It is perfect for checking even and odd numbers (n % 2 === 0) or for wrapping an index back to the start of a list. Note that with negative numbers the result keeps the sign of the left side, so -7 % 3 is -1, not 2.

The increment ++ and decrement -- operators add or subtract one. Their position matters when the value is used in the same expression: count++ returns the old value and then increases it, while ++count increases first and returns the new value. Compound assignment operators such as +=, -=, *= and **= update a variable in place.

let price = 499;
let qty = 3;

let subtotal = price * qty;          // 1497
let discount = subtotal * 0.1;       // 149.7
let total = subtotal - discount;     // 1347.3

let items = 0;
items += qty;                        // 3
console.log(items++);                // prints 3, items is now 4
console.log(++items);                // prints 5

console.log(10 % 4);                 // 2
console.log(2 ** 10);                // 1024

The + operator and strings

The plus sign has two jobs. With numbers it adds; if either side is a string, it joins text instead. That causes the most famous arithmetic bug in JavaScript: values read from an input box or a URL are strings, so "5" + 3 gives "53", not 8.

The other operators do not join text. They try to convert both sides to numbers, so "5" - 3 is 2 and "5" * "2" is 10. That inconsistency is why you should convert explicitly with Number(value) before calculating, rather than relying on automatic conversion.

Special results: NaN, Infinity and decimals

  • A calculation that makes no sense, like "abc" * 2, produces NaN ("not a number"). Any further maths with NaN also gives NaN. Check with Number.isNaN(x).
  • Dividing by zero does not throw an error. 5 / 0 is Infinity, and 0 / 0 is NaN.
  • Numbers are stored in binary floating point, so some decimals are not exact: 0.1 + 0.2 is 0.30000000000000004. For money, calculate in the smallest unit (paise) as whole numbers, and format the result with toFixed(2) only for display.

Order of operations

JavaScript follows the usual maths order: ** first, then *, / and %, then + and -, working left to right within the same level. So 2 + 3 * 4 is 14. Use parentheses whenever the intent might be unclear; (2 + 3) * 4 is 20 and reads clearly to everyone.

Points to keep: convert input to numbers before adding, watch for NaN, remember that decimals can be slightly inexact, and use brackets to make the order obvious.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course