Just Enough Numbers
JavaScript’s Primary Numeric Type: Number
JavaScript’s primary numeric type is Number
, which is versatile enough to handle both integers
and approximate real numbers
. This flexibility is key to many operations in JavaScript, making Number
one of the most commonly used data types. JavaScript supports numeric literals in several formats. For instance, a base-10
integer in JavaScript is simply written as a sequence of digits, such as 0
, 5
, or 50000000
.
Numeric Literals Beyond Base-10
But JavaScript doesn’t stop at base-10
; it also recognizes hexadecimal values
, which are base-16
. In this format, a digit can be anything from 0
to 9
or the letters a
(or A
) through f
(or F
), representing values from 10
to 15
. For example, 0x1A
would represent the decimal value 26
. Starting with ES6
, JavaScript introduced even more numeric formats: you can now write integers in binary
(base-2
) or octal
(base-8
) using the prefixes 0b
and 0o
respectively, such as 0b1010
for 10
in decimal or 0o12
for 10
in decimal.
Floating-Point Numbers and Exponential Notation
When dealing with real numbers
, JavaScript allows you to use floating-point literals
, which can include a decimal point. For example, 3.14
is a floating-point literal. JavaScript also supports exponential notation
for representing real numbers, where you can follow a number with e
(or E
) and an exponent, like 1.23e4
, which equates to 1.23 * 10^4
.
Enhancing Readability with Numeric Separators
To make long numeric literals more readable, you can use underscores
as separators. For instance, instead of writing 1000000
, you could write 1_000_000
to break it up into more digestible chunks.
Arithmetic Operations and Mathematical Functions
Arithmetic in JavaScript is straightforward, with the basic operators +
for addition, -
for subtraction, *
for multiplication, /
for division, and %
for modulo (which gives the remainder of a division). ES2016
added the ``operator for exponentiation, making it easier to perform power calculations like23
for2^3
. For more complex mathematical operations, JavaScript provides a range of functions and constants through the Math
object, such asMath.sqrt()
for square roots andMath.PI
for the value of π.
Special Numeric Values: Infinity and NaN
JavaScript also defines global constants like Infinity
and NaN
(Not-a-Number). Infinity
represents values that exceed the largest representable number, while NaN
signifies an invalid number. The global function isNaN()
helps identify if a value is NaN
or if it’s a non-numeric value that can’t be converted to a number, similar to Number.isNaN()
.
Working with Large Integers: BigInt
For those times when regular Number
types just won’t cut it—like when you need to represent extremely large integers—JavaScript introduced the BigInt
type in ES2020
. BigInt
allows for integers of arbitrary precision, meaning you can work with numbers far beyond the limits of Number
. You can create a BigInt
by appending an n
to the end of an integer literal, like 12345678901234567890n
. Additionally, you can use the BigInt()
function to convert regular numbers or strings to BigInt
values.
The Power and Flexibility of JavaScript Numbers
These features make JavaScript's handling of numbers both powerful and flexible, providing you with the tools you need to perform a wide range of numerical operations. Whether you're dealing with simple integers, complex floating-point numbers, or massive integers that require BigInt
, JavaScript has you covered.
Mastering JavaScript's Numeric Fundamentals
We've covered a solid foundation on numbers, giving you more than enough to get started writing applications with confidence. All the essentials are here, and they’ll serve you well in 95%
of the scenarios you’ll encounter. There’s plenty more to learn, but we’ve intentionally kept things streamlined at this stage of your journey, as you earn your blue belt in JavaScript. More advanced topics like overflow
, underflow
, division by zero
, negative zero values
, and binary floating-point rounding errors
will come later when you're ready to level up on more advanced belts. For now, focus on mastering these fundamentals—they’ll be your go-to tools as you continue to grow as a developer.
Study Style Notes
JavaScript's Numeric Types
- Primary Numeric Type:
Number
- Handles both integers and approximate real numbers.
- Versatile for many operations in JavaScript.
- Commonly used data type.
Numeric Literals in JavaScript
- Base-10 Integer Literals: Written as a sequence of digits (e.g.,
0
,5
,50000000
). - Hexadecimal Values (Base-16):
- Uses digits
0-9
and lettersA-F
(ora-f
). - Example:
0x1A
represents26
in decimal.
- Uses digits
- Binary (Base-2) and Octal (Base-8) Literals:
- Introduced in ES6.
- Use prefixes
0b
(binary) and0o
(octal). - Examples:
0b1010
(binary for10
),0o12
(octal for10
).
Real Numbers and Exponential Notation
- Floating-Point Literals: Include a decimal point (e.g.,
3.14
). - Exponential Notation: Use
e
orE
followed by an exponent (e.g.,1.23e4
equals1.23 * 10^4
).
Numeric Separators for Readability
- Underscores: Used to separate digits in long numeric literals.
- Example:
1_000_000
instead of1000000
.
- Example:
Arithmetic Operations in JavaScript
- Basic Operators:
+
(addition),-
(subtraction),*
(multiplication),/
(division),%
(modulo).
- **Exponentiation Operator (
**
):**- Introduced in ES2016 for power calculations (e.g.,
2**3
equals2^3
).
- Introduced in ES2016 for power calculations (e.g.,
- Math Object:
- Provides functions like
Math.sqrt()
for square roots. - Constants like
Math.PI
for π.
- Provides functions like
Special Numeric Values
- Infinity: Represents values beyond the largest representable number.
- NaN (Not-a-Number): Represents an invalid number.
- Functions:
isNaN()
: Checks if a value is NaN or not numeric.Number.isNaN()
: More precise for determining NaN.
- Functions:
BigInt: Handling Large Integers
- BigInt Type: Introduced in ES2020.
- Represents integers of arbitrary precision.
- Created by appending
n
to an integer literal (e.g.,12345678901234567890n
). - Can also use
BigInt()
to convert numbers or strings to BigInt.
JavaScript's Numeric Capabilities
- Flexible and Powerful: Handles a wide range of numeric operations.
- Supports:
- Simple integers.
- Complex floating-point numbers.
- Massive integers (using BigInt).
- Supports:
Mastering JavaScript Numbers
- Foundational Knowledge: Essential for developing applications confidently.
- Future Learning:
- Advanced topics: overflow, underflow, division by zero, negative zero, and binary floating-point rounding errors.
- Focus on mastering fundamentals first for effective growth as a developer.