JavaScript > JavaScript Fundamentals > Data Types > Number
JavaScript Number Type: Integers and Floats
This snippet demonstrates the usage of the Number data type in JavaScript, covering both integer and floating-point representations. We'll explore how to declare, initialize, and perform basic arithmetic operations with numbers.
Declaring and Initializing Numbers
In JavaScript, the `Number` data type is used to represent both integers (whole numbers) and floating-point numbers (numbers with decimal points). You can directly assign numeric values to variables. JavaScript automatically infers the type as `Number`. Scientific notation using `e` is a convenient way to represent large or small numbers.
// Integer
let age = 30;
// Floating-point number
let price = 19.99;
// Number with exponent
let largeNumber = 1.23e6; // Equivalent to 1.23 * 10^6
console.log(age);
console.log(price);
console.log(largeNumber);
Basic Arithmetic Operations
JavaScript supports standard arithmetic operators. The `+` operator performs addition. `-` performs subtraction. `*` performs multiplication. `/` performs division. `%` is the modulo operator, which returns the remainder of a division. `**` (introduced in ES2016) performs exponentiation (raising a number to a power).
let num1 = 10;
let num2 = 5;
let sum = num1 + num2;
let difference = num1 - num2;
let product = num1 * num2;
let quotient = num1 / num2;
let remainder = num1 % num2; // Modulo operator
let exponentiation = num1 ** num2; // ES2016 exponentiation operator
console.log("Sum: " + sum);
console.log("Difference: " + difference);
console.log("Product: " + product);
console.log("Quotient: " + quotient);
console.log("Remainder: " + remainder);
console.log("Exponentiation: " + exponentiation);
Understanding Infinity and NaN
JavaScript has special numeric values: `Infinity` (result of dividing by zero) and `NaN` (Not-a-Number, often the result of an invalid mathematical operation). Note that `typeof Infinity` and `typeof NaN` both return 'number'. Crucially, `NaN` is never equal to itself (`NaN === NaN` is always `false`). Use `Number.isFinite()` and `Number.isNaN()` to check for these values respectively.
let infinity = 1 / 0; // Infinity
let notANumber = NaN;
console.log("Infinity: " + infinity);
console.log("NaN: " + notANumber);
console.log(typeof infinity) // number
console.log(typeof NaN) // number
console.log(NaN === NaN) // false
console.log(Number.isFinite(infinity)) // false
console.log(Number.isFinite(1000)) // true
console.log(Number.isNaN(NaN)) // true
Real-Life Use Case Section
Numbers are fundamental for representing quantities, measurements, and scores. Examples include calculating prices in e-commerce applications, managing user ages and statistics in social media platforms, processing financial data, creating game scores, rendering graphics in games. Every single task which needs numerical precision and calculation.
Best Practices
Interview Tip
A common interview question involves explaining the difference between `==` and `===` in JavaScript, particularly when comparing numbers. Also, be prepared to discuss the concept of `NaN` and why `NaN === NaN` is `false`. Floating point precision is another popular interview topic.
When to use them
Use the Number datatype whenever you need to represent a numerical value. This includes quantities, measurements, indices, counters, scores, prices, etc. Always make sure input fields are validated for numbers.
Memory footprint
JavaScript numbers are typically stored as double-precision 64-bit binary format (IEEE 754). This means that each number consumes 8 bytes of memory. Understanding this helps optimize your code if memory usage is critical.
Alternatives
For very large integers (beyond the safe integer range), you can use the `BigInt` type (introduced in ES2020). Libraries like `decimal.js` offer arbitrary-precision arithmetic for situations demanding perfect accuracy with decimals.
Pros
Cons
FAQ
-
What is the maximum safe integer in JavaScript?
The maximum safe integer in JavaScript is `Number.MAX_SAFE_INTEGER`, which is 9007199254740991. -
How can I check if a value is a number?
You can use the `typeof` operator or the `Number.isFinite()` function. Note that `typeof NaN` will return 'number', so `Number.isFinite()` provides a more robust check for valid numeric values. -
Why do floating-point errors occur in JavaScript?
Floating-point numbers are represented in binary format, and some decimal numbers cannot be precisely represented in binary. This can lead to small rounding errors during calculations.