Coding/Learn/javascript/operators and math
JavaScript Foundational Course
01.Introduction to JavaScript & V8 Engine
02.Console Output & Program Flow
03.Variables & Data Types (let, const, typeof)
04.Template Literals & String Interpolation
05.Type Coercion & Strict Equality (=== vs ==)
06.Arithmetic, Math Object & Modulo
07.Conditionals & If / Else Branching
08.Logical Operators & Nullish Coalescing (??)
09.Ternary Operator & Switch Statements
10.While & Do-While Loops
11.Standard For Loops & Iteration
12.Loop Control (Break & Continue)
13.Functions, Arrow Syntax & Return Values
14.Default Parameters, Rest & Spread Syntax
15.Arrays, Indexing & Mutating Methods
16.Array Iterators (map, filter, reduce)
17.Objects, Key-Value Pairs & Destructuring
18.Scope, Lexical Environment & Closures
19.Asynchronous JavaScript (Promises & Async/Await)
20.Capstone: Interactive E-Commerce Cart & Ledger Engine
Course Certificate0%

Complete all lessons to earn

LearnArithmetic, Math Object & Modulo
6 / 20 0%
Lesson 06 of 20
5 mins read

Arithmetic, Math Object & Modulo

PreviousNext Lesson
In Plain English

JavaScript supports arithmetic operators (+, -, *, /, %, **), compound assignments (+=, *=), and the built-in Math object for advanced operations like Math.round(), Math.floor(), Math.sqrt(), and Math.random().

The Mental Model

"The Math object is like a pre-loaded scientific calculator bundled directly inside the JavaScript runtime."

Deep Dive: How It Works

Modulo (%): Returns the remainder of division (e.g., 10 % 3 = 1). Useful for checking even/odd numbers.

Exponentiation (**): 2 ** 3 = 8 (replaces Math.pow(2, 3)).

Rounding Methods: Math.floor() rounds down, Math.ceil() rounds up, Math.round() rounds to nearest integer.

Syntax Blueprint

Math.round(4.7) ──▶ 5 | 17 % 5 ──▶ 2 | 2 ** 8 ──▶ 256

Use arithmetic operators for basic calculations and the Math object for rounding and powers.

PointCore Rules to Remember

Point star
Math Object: Provides static constants (Math.PI) and calculation methods (Math.max, Math.min).
Point star
Modulo Operator: number % 2 === 0 checks if a number is even.
Point star
Compound Assignments: count += 5 is shorthand for count = count + 5.

Common Beginner Traps & How to Fix Them

Mistake:Floating-point precision in 0.1 + 0.2.

Why it happens: Binary floating-point arithmetic causes 0.1 + 0.2 to evaluate to 0.30000000000000004.

How to fix: Use Math.round() or .toFixed(2) for financial and currency formatting.

Pro Tip: Use Math.floor(Math.random() * (max - min + 1)) + min to generate random integers in a range.

Live Interactive Example

Hit Run Code to see it live
Arithmetic and Math Methods
JavaScript ES2024
1
2
3
4
5
6
7
8
9
10
11
12
Output Console
Click "Run Code" to view the rendered output.
How it works: Demonstrates division, remainder modulo, powers, and rounding.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Calculate circle area with Math.PI

  • PointGiven const radius = 5;
  • PointCalculate area = Math.PI * (radius ** 2);
  • PointRound area to the nearest integer using Math.round(area).
  • PointPrint "Rounded Area: 79".
1
2
3
4
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates