04 Numbers

A number is used to store numeric values, such as age, price, temperature, or distance.

Unlike strings, numbers are not written inside quotation marks.

Valid JSON Numbers

Here are some examples of valid numbers:

{
    "students": 35,
    "price": 49.99,
    "temperature": -8.5,
    "discount": 0.15,
    "distanceToMoon": 3.84e8
}

In this example:

  • 35 is a whole number.
  • 49.99 is a decimal number.
  • -8.5 is a negative number.
  • 0.15 is a decimal smaller than 1.
  • 3.84e8 is scientific notation, which means 3.84 × 10⁸ (384,000,000).

Rules for JSON Numbers

Rule 1: Numbers Do Not Use Quotes

Numbers should be written without double quotes.

✅ Correct

{
    "score": 92
}

❌ Wrong

{
    "score": "92"
}

Why is it wrong? "92" is treated as text (a string), not a number.

Rule 2: Use Only Digits (0–9)

JSON numbers use only the digits 0 to 9.

✅ Correct

{
    "year": 2026
}

❌ Wrong

{
    "value": 0x20
}

Why is it wrong? 0x20 is a hexadecimal number. JSON does not allow hexadecimal numbers.

Rule 3: Do Not Start a Number with Extra Zeros

A number should not have unnecessary zeros at the beginning.

✅ Correct

{
    "day": 7
}

❌ Wrong

{
    "day": 007
}

Why is it wrong? Leading zeros are not allowed in JSON numbers.

Rule 4: Decimal Numbers Are Allowed

Use a decimal point (.) when a number has a fractional part.

✅ Correct

{
    "weight": 65.8
}

❌ Wrong

{
    "weight": 65,8
}

Why is it wrong? JSON uses a period (.) as the decimal separator, not a comma.

Rule 5: Scientific Notation Is Allowed

Very large or very small numbers can be written using e or E.

✅ Correct

{
    "stars": 2.5e9
}

This means: 2.5 × 10⁹ = 2,500,000,000

Another valid example:

{
    "tinyValue": 4.2E-3
}

This means: 4.2 × 10⁻³ = 0.0042

Rule 6: NaN and Infinity Are Not Allowed

JSON does not support the special values NaN or Infinity.

✅ Correct

{
    "result": null
}

❌ Wrong

{
    "result": NaN
}

❌ Wrong

{
    "result": Infinity
}

Why is it wrong? NaN and Infinity are allowed in JavaScript, but they are not valid JSON values.

Summary

Number TypeExample
Whole Number45
Decimal Number19.75
Negative Number-12
Decimal Less Than 10.8
Scientific Notation5.6e7

Remember

  • ✅ Do not use quotes around numbers.
  • ✅ Do not add extra zeros at the beginning.
  • ✅ Use a period (.) for decimal numbers.
  • ✅ Scientific notation (e or E) is allowed.
  • ❌ Do not use hexadecimal (0x), octal, NaN, or Infinity.