JavaScript Number Object

JavaScript is a loosely typed scripting language. You don’t need to declare variables with their data type. The value assigned to a variable defines its data type. The variable initialized with value of one data type can be assigned value of another data type value. JavaScript Number Object is another way to use numeric values by treating a variable as an object of number type.

Declaring Number Object

A JavaScript Number Object can be created by Number() constructor. The syntax of declaring a Number Object is.

var object-var=new Number(value);

var and new- keywords

object-var- is the user given name to the number object

Number= Constructor of Number Class

Value- the object is initialized with this value

var objRollNo=new Number(101);

A variable of number object named objRollNo is declared and initialized with value 101.

Properties

When you need to use the number as an object, you get the advantage of working with properties of the JavaScript Number Object. Properties can be accessed by using the property name with object name joined by dot operator.

object-var.propetry-name;

The following are the properties used with number object.

MAX_VALUE-returns the largest positive number for a number object.

MIN_VALUE-returns the minimum positive number for a JavaScript number object.

NEGATIVE_INFINITY– returns the negative infinity value.

POSITIVE_INFINITY – returns the negative infinity value.

NaN -returns the Not a Number.

Example
<SCRIPT LANGUAGE="javascript">
var maxVal= Number.MAX_VALUE;
var minVal= Number.MIN_VALUE;
var niVal= Number.NEGATIVE_INFINITY;
var piVal= Number.POSITIVE_INFINITY;
var nanVal= Number.NaN;
document.write("<br> Max Value is "+maxVal);
document.write("<br> Min Value is "+minVal);
document.write("<br> Negative infinity Value is "+niVal);
document.write("<br> Positive infinity Value is "+piVal);
document.write("<br> Not a Number Value is "+nanVal);
</SCRIPT>

Methods of Number Object

With the Number object you also get methods to manipulate the object value. A method can be called by using the its name with object name joined by dot operator and an argument value(s) in parentheses. The number Object has following methods

isNAN(value)– This method returns True if the argument is not a number. It has a single mandatory argument.

isFinite(value)– This method returns True if the argument is Finite . It has a single mandatory argument.

isInteger(value)– This method returns True if the argument is Integer value . It has a single mandatory argument.

isSafeInteger(value)– This method returns True if the argument is Safe Integer value between (-)2^53-1  and  2^53-1. It has a single mandatory argument.

parseInt(string)– This method evaluates and converts an expression string value into a integer value. It has a single mandatory argument.

parseFloat(string)– This method evaluates and converts an string value into a floating point number. It has a single mandatory argument.

Example
<SCRIPT LANGUAGE="javascript">
document.write("<br> calling isNAN "+Number.isNaN(100));
document.write("<br> calling isFinite "+Number.isFinite(10));
document.write("<br> calling isInteger "+Number.isInteger(20.1));
document.write("<br> calling isSafeInteger "+Number.isSafeInteger(222));
document.write("<br> calling parseInt "+Number.parseInt(20+35));
document.write("<br> calling parseFloat "+Number.parseFloat(69+22));
</SCRIPT>