CodesJava

Easy learning with example program codes

Javascript number object example

Javascript number object:

The JavaScript Number object represents numerical data which can be integers or floating-point numbers.

How to create 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 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 Number() constructor. It returns NaN if 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:

PropertyDescription
MAX_VALUEIt specify the largest possible value.
MIN_VALUEIt specify the smallest possible value.
NaNEqual to a value that is not a number.
NEGATIVE_INFINITYA value that is less than MIN_VALUE.
POSITIVE_INFINITYA 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 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 safe integer or not.

Number toString() method:
Use: To return the given number in the form of string.

Please follow and like us:
error








Copyright © 2019 CodesJava DMCA.com Protection Status