SUBMIT HOMEWORK

WIP · JavaScript · Lesson 002

Numbers, Integers, Decimals

Chapter II · Numbers, percentages, powers and the floating-point betrayal

Archive Summary

This lesson introduces numbers in JavaScript: whole-number thinking, decimals, basic math operators, percentages, powers, useful math constants, and the famous moment where 0.1 + 0.2 refuses to become exactly 0.3.

The goal is not to make math frightening. The goal is to know where JavaScript behaves exactly as expected, and where the calculator starts whispering from the basement.

“Numbers are friendly until decimals begin filing complaints.” — De la Vega’s School of Code

1. Numbers in JavaScript

JavaScript normally uses one main type for numbers: Number. That means whole numbers and decimal numbers both live in the same general category.

let songs = 43;
let frequency = 69.0;
let tax = 48.5;

console.log(songs);
console.log(frequency);
console.log(tax);

Expected output:

43
69
48.5

43 behaves like a whole number. 48.5 behaves like a decimal. JavaScript still stores both as numbers.

“In normal beginner JavaScript, do not worry about separate integer and float types yet. Learn what a Number does first.” — Lesson 002 field note

2. Basic Math Operators

JavaScript can add, subtract, multiply and divide numbers with the usual operators.

console.log(10 + 5);
console.log(10 - 5);
console.log(10 * 5);
console.log(10 / 5);

Expected output:

15
5
50
2
  • + Addition — add two values. Warning: if one value is text, + may glue instead.
  • - Subtraction — subtract one value from another. Usually less dramatic than +.
  • * Multiplication — multiply values. Useful for scores, prices, sizes and suspicious quantities.
  • / Division — divide one value by another. May produce decimals.
  • 3. Integer-ish Thinking

    Whole numbers are usually safe for counting things: songs, adverts, covers, spins, paylines, symbols and other concrete quantities.

    let songs = 43;
    let adverts = 15;
    let stationIds = 25;
    
    console.log(songs + adverts + stationIds);

    Expected output:

    83
    “Counting things is usually safe. Measuring tiny fractions of things is where the machine starts being clever in public.” — Lesson 002 field note

    4. Percentages

    JavaScript does not have a special percent type. A percentage is usually just a number divided by 100.

    const price = 100;
    const discountPercent = 25;
    
    const discount = price * 
    (discountPercent / 100);
    const finalPrice = price - discount;
    
    console.log(discount);
    console.log(finalPrice);

    Expected output:

    25
    75

    So 25% becomes 25 / 100, which is 0.25.

    “Percent means per hundred. JavaScript will not guess the percent sign for you.” — De la Vega correction note

    5. Math Constants: PI

    Some mathematical values are available through JavaScript’s built-in Math object. The most famous one is Math.PI.

    const radius = 10;
    const area = Math.PI * radius ** 2;
    
    console.log(area);

    Math.PI gives us π. The expression radius ** 2 means “radius to the power of 2”.

    6. Michael Horror Example: Bøje’s Potency Problem

    console.log(2 ** 3);
    console.log(2 ^ 3);

    Michael expected:

    8
    8

    JavaScript produced:

    8
    1

    In JavaScript, ** means power. The ^ symbol does not mean power. It means bitwise XOR, which is a different operation and not something we need for normal math yet.

    “Professor Verbum insisted the symbol looked powerful. JavaScript disagreed.” — De la Vega correction note

    7. De la Vega Correction: Powers

    console.log(2 ** 3);
    console.log(Math.pow(2, 3));

    Correct output:

    8
    8
    “Use ** for powers. Do not use ^ unless you actually mean bitwise XOR, which you probably do not yet.” — Lesson 002 field note

    8. Michael Horror Example: The Decimal Betrayal

    const tal = 0.1 + 0.2;
    
    function test(tal) {
        if (tal === 0.3) {
            return true;
        } else {
            return false;
        }
    }
    
    console.log(
        "Success for tal = 0.3",
        test(tal)
    );
    
    console.log(
        "Actual value:",
        tal
    );
    
    console.log(
        "Actual value:",
        tal
    );
    

    Michael expected:

    Success for tal = 0.3 true
    Actual value: 0.3

    JavaScript produced:

    Success for tal = 0.3 false
    Actual value: 0.30000000000000004

    JavaScript is not lying. It is approximating. Many decimal numbers cannot be stored perfectly in binary floating point, so 0.1 + 0.2 becomes very close to 0.3, but not exactly 0.3.

    9. De la Vega Correction: almostEqual()

                        
    function almostEqual(a, b, tolerance = 0.000001) {
        return Math.abs(a - b) < tolerance;
    }
    
    const tal = 0.1 + 0.2;
    
    console.log(
        "Success for tal ≈ 0.3", 
        almostEqual(tal, 0.3));
    
    

    Correct output:

    Success for tal ≈ 0.3 true
    “Do not use === for exact comparison after decimal calculations. Use tolerance, rounding or whole units.” — Lesson 002 field note

    10. Money, Credits and Scores

    Decimal problems matter most when numbers represent money, credits, balances, scores or anything where tiny differences can become visible.

    // Risky:
    let price = 19.95;
    
    // Safer:
    let priceInCents = 1995;

    For money, credits and balances, store the smallest whole unit when possible: cents, øre, credits, points or integer score units.

    “If the number is important, do not let tiny decimal ghosts manage the cash register.” — De la Vega accounting note

    11. Tiny Exercise

    Try these examples in the console. What does JavaScript print, and why?

    let balance = 0.1 + 0.2;
    
    console.log(balance);
    console.log(balance === 0.3);
    console.log(Math.round(balance * 100) / 100);
    
    console.log(3 ** 2);
    console.log(3 ^ 2);
    “If the result looks strange, do not panic. First ask what type of operation JavaScript thought you requested.” — Lesson 002 field note