CodeBlanks Logo Icon CodeBlanks

Integers and decimals

JavaScript course · Beginner · Lesson 19/133

Learn integer and decimal numbers and that JavaScript has a single number type.

One type: number

In JavaScript all numbers live under a single type: number. There is no separate integer and float type; both belong to the same type.

For example 7 is an integer and 3.14 is a decimal number. The part left of the dot is the whole part, the part right of it is the decimal part. Negative numbers belong to the same type: -5 or -0.5.

When you add two numbers the result is still a number. The typeof operator returns "number" for both.

Careful with decimals

Decimal numbers are stored in binary on computers. Because of this some values cannot be shown exactly and small rounding errors appear.

For example 0.1 + 0.2 does not give 0.3; it gives 0.30000000000000004. This is not specific to JavaScript; all languages behave this way.

When working with money most people keep the amount as an integer in cents or use rounding functions to avoid these errors.

Exercise Declare quantity and price

Declare an integer const variable named quantity with value 3. Declare a decimal const variable named price with value 19.99. Then log both with console.log.

Show example solution
const quantity = 3;
const price = 19.99;
console.log(quantity);
console.log(price);

Test yourself

  1. Which one is a decimal (float) number?

    • 42
    • 3.14
    • -8
    • 1000
    Show answer

    3.14 — Correct. 3.14 is a decimal number; values with a dot count as floats.

  2. Decimal price

    const price = ____;
    Show answer
    const price = 19.99;
  3. Match each term to its meaning

    integer
    Whole number (7, -3)
    float
    Decimal number (3.14)
    number
    JS's single number type
    typeof
    Returns the type of a value
  4. What does console.log(typeof 42); print?

    console.log(typeof 42);
    • "number"
    • "integer"
    • "42"
    • "int"
    Show answer

    "number" — Correct. JavaScript keeps integers and decimals in the same type; typeof returns "number" for both.

All lessons in Beginner