Quick Overview
Every JavaScript number is a 64-bit float, which is why `0.1 + 0.2` comes out as `0.30000000000000004`. `%` returns a remainder that keeps the sign of the left operand, so `-7 % 3` is `-1`.
Unlike languages with separate int and float types, JavaScript uses a single primitive number type (64-bit double precision floating point) for both integers and decimal numbers.
Beyond standard addition (+), subtraction (-), multiplication (*), and division (/), the modulo operator (%) is a fundamental tool for checking number divisibility, determining even/odd states, and cycling indices in round-robin loops.
The modulo operator returns the remainder left over when one integer is divided by another:
const remainder = 10 % 3;
console.log(remainder); // 1 (10 divided by 3 is 3, with 1 left over)
// Checking if a number is even or odd
function isEven(num) {
return num % 2 === 0;
}
console.log(isEven(4)); // true
console.log(isEven(7)); // false
JavaScript provides a built-in Math object for common mathematical operations:
console.log(Math.round(4.7)); // 5 (Round to nearest integer)
console.log(Math.floor(4.9)); // 4 (Round down)
console.log(Math.ceil(4.1)); // 5 (Round up)
console.log(Math.max(10, 20, 5)); // 20
console.log(Math.min(10, 20, 5)); // 5
0.1 + 0.2): Because computers store numbers in binary (base-2), fractional decimals cannot always be represented with exact precision. In JavaScript, 0.1 + 0.2 evaluates to 0.30000000000000004. For monetary/currency calculations, always work in whole cents before converting to dollars!NaN: Operations like "hello" * 5 return NaN. NaN is the only value in JavaScript that is NOT equal to itself (NaN === NaN is false!). Use Number.isNaN(val) to check for invalid numbers.Write it yourself. The tests below run against your code.
Use % whenever you need to check divisibility or cyclical limits. Next, let's explore how boolean logic drives application decision making!
Primitives copy by value and objects copy by reference, so `b = a` duplicates a number but shares an object. That single distinction explains most surprise mutations in a codebase.
`const` prevents reassignment, not mutation: a `const` object's properties can still be changed. `let` is scoped to its block, while `var` is function-scoped and leaks out of loops.
Only backticks interpolate: `${user}` reads the variable, while the identical text in quotes prints literally. Strings are immutable, so every method returns a new one rather than editing in place.
Average 5.0 by 1 learner