Numbers

Grade 2, page 5 of 13. A one-minute read.

The number type holds, well, numbers. Unlike some other languages, JavaScript doesn't care whether a number is whole or has a decimal point. They're all just "numbers."

JavaScript
const wholeNumber = 42;
const decimal = 3.14;
const negative = -10;
const big = 1_000_000;

console.log(wholeNumber, decimal, negative, big);

It prints

42 3.14 -10 1000000

See that 1_000_000? You're allowed to put underscores inside numbers to make them easier to read. JavaScript ignores the underscores completely. (They're like the commas we use when writing big numbers on paper.)

Watch outNumbers in quotes aren't numbers

42 is a number. "42" is a string that happens to contain the characters 4 and 2. They look similar, but JavaScript treats them very differently, as you'll see at the end of this grade. When you mean a number, leave off the quotes!

Reading is step one

CurlyScript turns these notes into a game: short questions, an explanation after every answer, and a test at the end of each grade. Free, with nothing to sign up for.

Start learning