The JavaScript Number object represents numerical data which can be integers or floating-point numbers.
How to create a Javascript number object:
1. By using number literals.
2. By using Number() constructor.
By using number literals:
When we create a number literal browser automatically converts it to a Number object.
Syntax:
var num1 = value;
Example:
<script>
function stuClassName(className){
this.className=className;
}
function Student(name,rollNo){
this.name=name;
this.rollNo=rollNo;
this.stuClassName= stuClassName;
}
var student=new Student("Jai", "MCA/07/06");
student.stuClassName("MCA Final");
document.write("Name: " +student.name + "</br>");
document.write("RollNo: " + student.rollNo + "</br>");
document.write("Class: " + student.className);
</script>
By using Number() constructor:
We can create a JavaScript Number object by using the Number() constructor. It returns NaN if the value is not a number.
Syntax:
var num1 = new Number(value);
Example:
<script>
var num1=new Number(102);//Enteger value
var num2=Number(102.7);//Floating point value
var num3=Number(13e4);//Exponent value
document.write("Integer Value: "+num1 + "</br>");
document.write("Floating point Value: "+num2 + "</br>");
document.write("Exponent Value: "+num3);
</script>
JavaScript number constants/properties:
| Property | Description |
| MAX_VALUE | It specifies the largest possible value. |
| MIN_VALUE | It specifies the smallest possible value. |
| NaN | Equal to a value that is not a number. |
| NEGATIVE_INFINITY | A value that is less than MIN_VALUE. |
| POSITIVE_INFINITY | A value that is greater than MAX_VALUE |
JavaScript number methods:
Number isFinite() method:
Use: To determine whether the given value is a finite number.
Number isInteger() method:
Use: To determine whether the given value is an integer.
Number parseFloat() method:
Use: To convert the given string into a floating point number.
Number parseInt() method:
Use: To convert the given string into an integer number.
Number toExponential() method:
Use: To return the string that represents the exponential notation of the given number.
Number toFixed() method:
Use: To return the string that represents a number with exact digits after a decimal point.
Number toPrecision() method:
Use: To return the string representing a number of specified precision.
Number isSafeInteger() method:
Use: To check whether the given number is a safe integer or not.
Number toString() method:
Use: To return the given number in the form of a string.