Loading...

JavaScript Numbers

You already know that a value can be a number. This session stays on that one type: how to write numbers, do everyday math, spot odd results like NaN and Infinity, and show a tidy decimal on the page.

How do numbers work in JavaScript?

In beginner JavaScript, integers and decimals share one type: number. Write them without quotes — 42 and 3.5 are numbers; "42" is a string. You can add, subtract, multiply, divide, and take a remainder. Some operations produce special results: NaN (“Not a Number”) when a calculation does not make sense as a number, and Infinity when a value grows without a finite limit (for example dividing by zero). For display, toFixed builds a string with a chosen count of digits after the decimal point.

Number toolkit

📊 Everyday number tools beginners use first

Idea Example Result / note
Integer literal let n = 10; Whole number, type number
Decimal literal let p = 2.5; Same number type
Arithmetic 10 + 3, 10 % 3 Sum 13; remainder 1
NaN 0 / 0 or bad math “Not a Number” — still typeof "number"
Infinity 1 / 0 Grows past finite limits
toFixed (3.14159).toFixed(2) Display string "3.14"

Write numbers without quotes

Whole numbers and decimals use the same type. No separate “integer type” is required at this level.

Integer and decimal literals

javascript
let score = 42;
let price = 9.99;
console.log(score);
console.log(price);
console.log(typeof score);
console.log(typeof price);

Everyday arithmetic

Use +, -, *, /, and % (remainder). A later Operators module goes deeper; here you only need to see that numbers compute.

Basic arithmetic with numbers

javascript
let a = 10;
let b = 3;
console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);
console.log(a % b);

NaN — Not a Number

NaN means the result is not a usable number. Odd fact: typeof NaN is still "number". If a calculation looks broken, log the value and check for NaN.

NaN from invalid math

javascript
let broken = 0 / 0;
console.log(broken);
console.log(typeof broken);

Infinity

Dividing a positive number by zero yields Infinity (and a negative dividend yields -Infinity). It is a real result the engine can produce — useful to recognize when debugging.

Infinity from division by zero

javascript
let huge = 1 / 0;
console.log(huge);
console.log(typeof huge);

Format decimals with toFixed

toFixed(n) returns a string with n digits after the decimal. Handy for money-style display. Remember: the result is text, so do further math on the original number, not on the formatted string.

toFixed for display

javascript
let total = 19.999;
console.log(total.toFixed(2));
console.log(typeof total.toFixed(2));

Show a total on the page

Compute a sum, optionally format it, and put the text into the page with textContent.

Page shows a calculated total

html
<!DOCTYPE html>
<html>
<head>
<title>Numbers — total on page</title>
</head>
<body>
<p id="out">…</p>
<script>
let price = 12.5;
let tax = 1.25;
let total = price + tax;
document.getElementById("out").textContent = "Total: " + total.toFixed(2);
</script>
</body>
</html>

The paragraph shows a money-style total. Change price or tax, refresh, and confirm the line updates.

Try it yourself

  1. Create two let numbers (one integer, one decimal); log both and typeof.
  2. Log a + b, a * b, and a % b for two small integers.
  3. Log 0 / 0 and confirm you see NaN.
  4. Log 1 / 0 and confirm you see Infinity.
  5. Call toFixed(2) on a decimal and note that typeof of the result is "string".
  6. In the HTML demo, change only price or tax, refresh, and read the new total.

Common mistakes

  • Writing let n = "10"; then expecting number math — quotes make a string
  • Ignoring NaN when a result “looks empty” in the Console
  • Surprising yourself with Infinity after dividing by zero
  • Doing more math on a toFixed result (it is a string)
  • Expecting a separate integer type — beginners share one number type

Summary

  • Integers and decimals share the number type; write them without quotes
  • Use +, -, *, /, and % for everyday calculations
  • NaN and Infinity are special number results worth recognizing
  • toFixed formats decimals for display and returns a string
  • Next: Strings — working with text values in more detail

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
8
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top