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.
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.
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.
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.
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.
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.
<!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
- Create two
letnumbers (one integer, one decimal); log both andtypeof. - Log
a + b,a * b, anda % bfor two small integers. - Log
0 / 0and confirm you seeNaN. - Log
1 / 0and confirm you seeInfinity. - Call
toFixed(2)on a decimal and note thattypeofof the result is"string". - In the HTML demo, change only
priceortax, refresh, and read the new total.
Common mistakes
- Writing
let n = "10";then expecting number math — quotes make a string - Ignoring
NaNwhen a result “looks empty” in the Console - Surprising yourself with
Infinityafter dividing by zero - Doing more math on a
toFixedresult (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
toFixedformats decimals for display and returns a string- Next: Strings — working with text values in more detail
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 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