Numbers in javascript
In JavaScript, numbers are a fundamental data type, and understanding how to work with them is crucial. This lesson will introduce you to the various ways JavaScript handles numbers, as well as methods and properties you can use to manipulate them.
How JavaScript Handles Numbers
In JavaScript, all numbers are stored as double-precision floating-point values, which means that whether you’re working with integers or decimal numbers, they are treated the same way under the hood.
What is Syntactic Sugar?
You might have heard the term "syntactic sugar" in computer science. While this isn't specific to JavaScript, it refers to making code easier to read or write without changing its functionality. For example:
console.log(1000000);
This is fine, but we can use a more readable format with underscores to separate the digits:
console.log(1_000_000); // Output: 1000000
The underscores help separate the digits without affecting the value. The JavaScript engine simply ignores them.
Other Ways to Represent Numbers
You can also represent numbers in JavaScript using exponential notation or the exponentiation operator.
Exponential notation:
console.log(1e6); // Output: 1000000
Here, e means "raise to the power of," so 1e6 is the same as 1 * 10^6.
Exponentiation operator:
console.log(10 ** 6); // Output: 1000000
Both methods give us the same result but in different formats.
BigInt: Working with Larger Numbers
JavaScript now supports BigInt, which allows you to work with numbers larger than the regular number limits:
console.log(9007199254740991n); // BigInt Example
This is especially useful when dealing with extremely large numbers beyond the limits of standard numbers in JavaScript.
Converting Strings to Numbers
Sometimes, you may need to convert strings into numbers. JavaScript provides several methods for this:
Using the `Number()` function:
console.log(Number("100")); // Output: 100
The Number()
function converts a string to a number. If the string is not a valid number, it will return NaN
(Not-a-Number).
Using the unary `+` operator:
console.log(+"100"); // Output: 100
Important Number Properties
JavaScript provides several useful properties for numbers:
Number.MAX_SAFE_INTEGER
: The largest safe integer in JavaScript.
console.log(Number.MAX_SAFE_INTEGER); // Output: 9007199254740991
Number.MAX_VALUE
: The largest positive number that JavaScript can represent.
console.log(Number.MAX_VALUE); // Output: 1.7976931348623157e+308
Number Methods
JavaScript offers a variety of methods to manipulate numbers. Here are a few:
`toString()`: Converting Numbers to Strings
The toString()
method converts a number to a string:
console.log((100).toString()); // Output: "100"
Notice the syntax: you can also use double dots like this:
console.log(100..toString()); // Output: "100"
This is just a quirky syntax where the second dot is used to avoid confusing the parser with floating-point notation.
`toFixed()`: Controlling Decimal Places
The toFixed()
method rounds a number to a specified number of decimal places and returns a string:
console.log(320.888888.toFixed(2)); // Output: "320.89"
If you set the parameter to 4 decimal places:
console.log(320.884888.toFixed(4)); // Output: "320.8849"
If rounding occurs (e.g., 320.888888 becomes 320.89), toFixed()
will handle it automatically.
parseInt()
and parseFloat()
: Converting Strings to Numbers
parseInt() converts a string to an integer (ignoring decimal points):
parseInt()
converts a string to an integer (ignoring decimal points):
console.log(parseInt("100.5")); // Output: 100
parseFloat()
converts a string to a floating-point number:
console.log(parseFloat("100.5")); // Output: 100.5
Checking Numbers with isInteger() and isNaN()
You can check if a value is an integer or if it's "Not-a-Number" using these built-in methods:
Number.isInteger()
checks if the value is an integer:
console.log(Number.isInteger(100)); // Output: true
console.log(Number.isInteger(100.5)); // Output: false
Number.isNaN()
checks if a value is NaN:
console.log(Number.isNaN("test" / 20)); // Output: true
Summary
In this lesson, we explored:
- How JavaScript handles numbers (both integers and floating-point numbers).
- Various ways to represent and format numbers.
- Methods like
toString()
,toFixed()
,parseInt()
, andparseFloat()
to work with numbers. - Properties and functions to manage number limits and conversions.
JavaScript makes working with numbers easy, but understanding these properties and methods will help you write cleaner, more efficient code.