Broad Network


Number Object in ECMAScript

ECMAScript Number Part 2

ECMAScript 6

Foreword: In this tutorial I talk about the ECMAScript number object.

By: Chrysanthus Date Published: 15 Jul 2016

Introduction

This is part 2 of my series, ECMAScript Number. In this tutorial I talk about the ECMAScript number object. Number objects are not exactly numbers. Number objects have properties and numbers do not have properties.

The Global and Number Objects
In ECMAScript an object has properties. If a property is a function, it is called a method. The non-function properties are called, data properties. The global object is an object that is already created by the browser. You do not see its code, but it is in every script you write, and is executed before your script is executed. One of the properties of the global object is Number(). This is actually a method and it is the number constructor. You can use it to create a number object.

The Number Constructor
The number constructor can be called as a function or it can be used in the new expression. The syntax to call it as a function is:

    Number ( [ value ] )

When called as a function, it does type conversion, meaning it converts a string value for example, to a number. The return value is a number; it is not an object. Read and try the following code:

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>

    <script type="text/ECMAScript">

        num = Number("15");
  
        answer = num + 2;

        alert(answer);

    </script>

</body>
</html>

The extra HTML code is to produce a web page. The constructor converts the string, "15" to the number, 15 and assigns the number to the identifier, num. The number, 15 is added to 2. The answer of 17 is displayed.

When the constructor is used in the new expression, it returns an object, a number object with properties. The syntax is:

    new Number ( [ value ] )

The argument, value, a number becomes like a property in the number object. Read and try the following code:

    <script type="text/ECMAScript">

        numObj = new Number(26.4);
  
        alert(numObj);

    </script>

Here, the numObj identifier is a number object.

Number Prototype Properties
An ECMAScript object has a property called the prototype. This property is responsible for inheritance (creating new objects). This property is also an object with its own properties. Remember, you do not type the prototype property directly; you type but the created object.

The toString Method
This method converts the number object to a string. In simple terms, the syntax is:

    numberObject.toString()

Read and try the following code:

    <script type="text/ECMAScript">

        numObj = new Number(124.59);
  
        str = numObj.toString();

        document.write(str);

    </script>

The output is "124.59".

While it is a string, you can use it in regular expression. Read and try the following code:

    <script type="text/ECMAScript">

        numObj = new Number(124.59);
  
        subject = numObj.toString();

        if (subject.match(/^-?(?:\d+\.?|\.\d)\d*$/))
            alert("It is a real number.");

    </script>

The output is, "It is a real number."

The valueOf Method
This method returns the number value of the number object. The syntax is:

    numberObject.valueOf()

Read and try the following code:

    <script type="text/ECMAScript">

        numObj = new Number(124.59);
  
        num = numObj.valueOf();

        document.write(num);

    </script>

The number displayed is, 124.59 .

The toFixed Method
This method rounds a number to a number of decimal places. The syntax is:

    numberObject.toFixed (fractionDigits)

where fractionDigits is the number of decimal places you want. Try the following code:

    <script type="text/ECMAScript">

        numObj = new Number(124.2658);
  
        num = numObj.toFixed(2);

        document.write(num);

    </script>

The output is, 124.27 .

The toExponential Method
This method expresses a number in exponential form, using e with + or sign for base 10. The syntax is:

    numberObject.toExponential (fractionDigits)

where fractionDigits is the number of decimal places wanted. The method may do some approximation and rounding. Try the following code:

    <script type="text/ECMAScript">

        numObj = new Number(524.4658);
  
        num = numObj.toExponential(3);

        document.write(num);

    </script>

The output is, 5.245e+2 .

The toPrecision Method
This method converts the number into the requested number of significant figures. The syntax is:

    numberObject.toPrecision (precision)

where precision is the number of significant figures requested. Try the following code:

    <script type="text/ECMAScript">

        numObj = new Number(535.4658);
  
        num = numObj.toPrecision(2);

        document.write(num);

    </script>

The output is, 5.4e+2 . To the left of e you have the number of significant figures.

That is it for this part of the series.

Chrys

Related Links

ECMAScript Basics
ECMAScript Operators
Expressions in ECMAScript
Statements in ECMAScript
Custom Objects in ECMAScript
Functions in ECMAScript
ECMAScript Date Object
The ECMAScript String Object
ECMAScript String Regular Expressions
ECMAScript Template Literal
The ECMAScript Array
ECMAScript Sets and Maps
ECMAScript Number
Scopes in ECMAScript
Mastering the ECMAScript (JavaScript) eval Function
Sending Email with ECMAScript
ECMAScript Insecurities and Prevention
Advanced Course
Advanced ECMAScript Regular Expressions
Promise in ECMAScript 2015
Generator in ECMAScript 2015
ECMAScript Module
More Related Links
Node Mailsend
EMySQL API
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module
ECMAScript Course

BACK

Comments

Become the Writer's Follower
Send the Writer a Message