ES6 Number Methods 🎯

beginner
7 min

ES6 Number Methods 🎯

Welcome to our deep dive into ES6 Number Methods! In this tutorial, we'll explore various methods available for working with numbers in JavaScript. We'll cover practical examples and real-world applications, making the concepts easy to understand and apply.

Understanding Number Methods 📝

Number methods are built-in functions that allow us to perform specific operations on numbers. ES6 introduced several new methods for numbers, making it easier to manipulate and work with them in our code.

Basic Number Methods 💡

Number.isFinite() and Number.isNaN()

These methods help determine if a value is finite or not a number (NaN).

javascript
// Example 1: Check if a number is finite console.log(Number.isFinite(123)); // Output: true // Example 2: Check if a value is NaN console.log(Number.isNaN('abc')); // Output: true

Number.parseFloat() and Number.parseInt()

These functions convert a string into a floating-point number or an integer, respectively.

javascript
// Example: Convert a string to a number using parseFloat() let str = '123.45'; let num = Number.parseFloat(str); console.log(num); // Output: 123.45 // Example: Convert a string to an integer using parseInt() let strInt = '123456'; let intNum = Number.parseInt(strInt); console.log(intNum); // Output: 123456

Number.max() and Number.min()

These methods return the largest or smallest number among a group of numbers.

javascript
// Example: Find the maximum number using Number.max() let numbers = [5, 10, 15, 20, 25]; let max = Math.max(...numbers); console.log(max); // Output: 25 // Example: Find the minimum number using Number.min() let min = Math.min(...numbers); console.log(min); // Output: 5

Quiz

Quick Quiz
Question 1 of 1

What does the `Number.isNaN()` method return?

Advanced Number Methods 💡

Number.toFixed() and Number.toPrecision()

These methods are used to format numbers with a specific number of decimal places or precision.

javascript
// Example: Format a number with two decimal places using toFixed() let numFixed = 123.45678.toFixed(2); console.log(numFixed); // Output: 123.46 // Example: Format a number with a specific precision using toPrecision() let numPrecision = 123.456789012345.toPrecision(6); console.log(numPrecision); // Output: 1.23457e+6

Number.toString()

This method converts a number to a string.

javascript
// Example: Convert a number to a string let num = 123456; let strNum = num.toString(); console.log(strNum); // Output: 123456

Quiz

Quick Quiz
Question 1 of 1

What does the `Number.toFixed()` method do?

That's it for this tutorial on ES6 Number Methods! In the next lesson, we'll dive deeper into more advanced topics. Stay tuned! 🎯