JavaScript Number Methods & Global Functions
The Number type is the foundation of all quantitative data in JavaScript. While arithmetic operators handle basic calculations, built-in methods and global functions provide crucial tools for formatting numbers, controlling precision, and converting text data into reliable numeric values.
Number Methods
Like strings, numbers are primitive values but gain access to powerful methods via a temporary wrapper object. These methods are essential for formatting and inspecting the numeric value, and they typically return the result as a string.
Formatting Methods: These methods are used to control how a number is displayed, which is vital for presenting things like currency or scientific notation.
.toFixed(digits): Formats a number using fixed-point notation, returning a string with a specified number of digits after the decimal point. It will round the number if necessary.
const pi = 3.14159265;
const price = 45;
// Used heavily for currency (forces 2 decimal places and rounds up)
console.log(pi.toFixed(3)); // Output: 3.142
console.log(price.toFixed(2)); // Output: 45.00JavaScript.toPrecision(precision): Returns a string representing the number rounded to a specified total number of significant digits.
// Useful for scientific notation or significant figures
console.log((98.765).toPrecision(3)); // Output: 98.8
console.log((123456).toPrecision(4)); // Output: 1.235e+5
// 1.235e+5 is short for 123,500JavaScript.toString(radix?): Returns a string representing the number. Optionally accepts aradix(or base, like 2 for binary or 16 for hexadecimal).
const num = 255;
// Converting to Hexadecimal (Base 16):
// 255 consists of fifteen 16s (f) and fifteen 1s (f).
console.log(num.toString(16)); // Output: "ff"
// Converting to Binary (Base 2):
// 10 consists of one 8, zero 4s, one 2, and zero 1s (1-0-1-0).
console.log((10).toString(2)); // Output: "1010"JavaScriptGlobal Conversion Functions
Global conversion functions are methods that are part of the global JavaScript environment or window object (not attached to the number or string prototype) and are used primarily for converting strings into numbers. Note that in the browser environment the global methods are attached to is the window object but in other JavaScript enviroments such as Node.js it would be the global object or self object for “web workers” which is part of the browser. This is crucial when dealing with user input, which is always received as text. Parsing integers and floats:
parseInt(string, radix?): Parses a string and returns an integer (whole number). It reads characters until a non-digit is encountered.
const value1 = "10.99 boxes";
const value2 = "Price: 50.75";
// Stops at the decimal point or the first non-digit character.
console.log(parseInt(value1)); // Output: 10
console.log(parseInt(value2)); // Output: NaN (Starts with a non-digit)JavaScriptparseFloat(string): Parses a string and returns a floating-point number (decimal). It reads characters until the second decimal point or a non-digit.
// Reads the first decimal point, but stops at the second.
console.log(parseFloat("100.50.6")); // Output: 100.5 (drops 0)
console.log(parseFloat("19.99 shoes")); // Output: 19.99 (drops " shoes")JavaScriptNumber(value): Explicitly converts any value (string, boolean, null) into a number. This conversion is strict—it fails (returnsNaNif any non-numeric characters are present, unlikeparseInt()/parseFloat().
// Strict conversion fails because of the word " shoes"
console.log(Number("19.99 shoes")); // Output: NaN
console.log(Number("19.99")); // Output: 19.99
console.log(Number(true)); // Output: 1
console.log(Number("10")); // Output: 10JavaScriptGlobal Check Function (isNaN)
The isNaN() function is essential for validating data. It specifically checks if a value is the special numeric value NaN (Not a Number).
The value NaN is unique because it is the only value in JavaScript that is not equal to itself (NaN === NaN is false). Therefore, you must use a dedicated function to check for it.
isNaN(value): Checks if the given value isNaN. Returnstrueif the value is non-numeric, otherwisefalse.
// Using the global isNaN() function
console.log(NaN === NaN); // Output: false
console.log(isNaN(NaN)); // Output: true
console.log(isNaN("apple")); // Output: true
console.log(isNaN(123)); // Output: false
console.log(isNaN("123")); // Output: false (Since "123" can be converted to a number)JavaScriptNumber.isNaN(value): This is the preferred, stricter version. It returnstrueonly if the value is explicitlyNaNand does not attempt to coerce non-number strings.
// The modern, stricter way to confirm NaN:
console.log(NaN === NaN); // Output: false
console.log(Number.isNaN(NaN)); // Output: true (strict)
console.log(Number.isNaN(123)); // Output: false
console.log(Number.isNaN("123")); // Output: false
console.log(Number.isNaN("apple")); // Output: falseJavaScriptWhile this overview covers the most common number methods, you can refer to the Number prototype object in your console for a complete inventory of every method available for further experimentation.




