Skip to content

JavaScript Number

  • by

In JavaScript, Number is a data type used to represent numeric value. It can be used to store integers, floating-point numbers, and other numeric values.

Here is the syntax for creating a Number in JavaScript:

let num = 42; // an integer value
let num2 = 3.14; // a floating-point value

JavaScript provides various methods and properties to work with numbers, such as:-

Method/PropertyDescription
Number.parseInt()Parses a string and returns an integer.
Number.parseFloat()Parses a string and returns a floating-point number.
Number.MAX_VALUEReturns the largest possible number in JavaScript.
Number.MIN_VALUEReturns the smallest possible number in JavaScript.
Number.NaNRepresents a value that is “Not-a-Number“.
Number.isNaN()Returns true if the value passed as an argument is NaN, and false otherwise.
Number.isFinite()Returns true if the value passed as an argument is a finite number, and false otherwise.

JavaScript Number Example

Simple example code.

<!DOCTYPE html>
<html>
<body>
    <script>
        // Storing integer values
        let age = 27;
        let quantity = 3;

        // Storing floating-point values
        let price = 4.99;
        let temperature = -10.5;

        // Using some methods and properties
        console.log(Number.parseInt("10")); 
        console.log(Number.parseFloat("3.14")); 
        console.log(Number.MAX_VALUE); 
        console.log(Number.MIN_VALUE); 
        console.log(Number.NaN); 
        console.log(Number.isNaN(5)); 
        console.log(Number.isFinite(1/0));

    </script>
</body>
</html>

Output:

JavaScript Number

JavaScript Number() Function

In JavaScript, the Number() function is used to convert a value to a Number data type. It can be called with or without the new keyword. If called without the new keyword, it returns a primitive number value. If called with the new keyword, it returns a Number object.

let num1 = Number("42"); 
let num2 = Number("3.14");
let num3 = Number("Hello"); 
let numObj = new Number("42")

console.log(typeof num1); // Output: "number"
console.log(typeof numObj); // Output: "object"
console.log(num3); // Output: NaN

Do comment if you have any doubts or suggestions on this JS number topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *