Working With Numbers in JavaScript I
JavaScript·3 min read·Mar 13, 2025
In JavaScript, the Number type represents numeric data, such as integers (e.g., 42, -2) and floating-point numbers (e.g, 3.14, -0.1), both positive and negative, and special values like NaN, Infinity, or -Infinity.
It is used for performing arithmetic operations, calculations, and also for storing user-facing values representing quantities or measurements.
Writing number literals
In JavaScript, numbers can be written in 4 different forms called bases including: decimal, hexadecimal, binary, and octal.
Decimal numbers (base 10)
Decimal numbers use digits 0-9.
They are the most common number system as they are used in everyday arithmetic and most general-purpose programming tasks, such as counters, prices, measurements, and so on.
For example:
// Integerconst age = 34;// Floating-pointconst pi = 3.14;Hexadecimal numbers (base 16)
Hexadecimal numbers use digits 0-9 and letters 'A-F' (or 'a-f') to represent values 0-15, and are prefixed with 0x.
They are commonly used in computing to represent data in a shorter and more readable format, such as binary data, memory addresses, color codes, and more.
For example:
// 255 in base 10Unlock the Build CLI Apps in JavaScript & Node.js module
Learn how to build, integrate, and safeguard scalable CLI tools, scripts, and apps with JavaScript, Node.js, npm, and Git.
You get immediate access to:
- 45 focused lessons across JavaScript, Node.js, npm, and Git
- 17 real-world projects with commented solutions
- Ongoing updates to this bundle
- Lifetime access to this bundle