Numbers are everywhere in JavaScript — prices, scores, percentages, age, ratings, and calculations. To work with numbers effectively, JavaScript provides many useful number methods.
Number methods are built-in functions that help you:
-
Format numbers
-
Round numbers
-
Convert numbers to strings
-
Control decimal points
-
Validate numeric values
They make your calculations cleaner, more accurate, and easier to manage.
1. toString() – Convert Number to String1 let num = 100;
Explanation:
Converts a number into a string.
Output:
"100"
String
Useful when displaying numbers as text.
2. toFixed() – Control Decimal Place
Output:
99.75
Explanation:
Rounds the number and keeps only the specified decimal places.
Commonly used in:
-
Price formatting
-
Currency calculations
3. toPrecision() – Control Total Digits
Output:
123.5
Explanation:
Limits the total number of digits, not just decimals.
4. Number() – Convert Other Types to Number
Explanation:
Converts strings and booleans into numbers.
5. parseInt() – Convert String to Integer
Output:
100
100
Explanation:
Removes decimal part and keeps only the integer.
6. parseFloat() – Convert String to Decimal Number
Output:
10.75
7. isNaN() – Check if Value is Not a Number
Explanation:
Helps validate user input in forms.
8. Number.isInteger() – Check for Integer1 console.log(Number.isInteger(10)); // true
Useful when validating whole number input.
Real-World Example: Price Formatter1
This is a real-world use of JavaScript number methods in e-commerce applications.
Common Mistakes Beginners Make
Confusing strings with numbers
Correct way
Why Number Methods Are Important in JavaScript
Learning JavaScript number methods helps you:
-
Format currency values
-
Handle user input properly
-
Avoid calculation bugs
-
Perform well in interviews
-
Build real-world applications
They are essential in:
-
Finance apps
-
Dashboards
-
Analytics systems
-
E-commerce platforms
toFixed(), parseInt(), Number(), and isNaN(), you can confidently handle numbers in any project.
Practice these methods with small progra