Broad Network


Number Objects in ECMAScript

Number and Boolean Objects in ECMAScript Part 1

Foreword: In this part of the series I talk about number objects in ECMAScript.

By: Chrysanthus Date Published: 22 Sep 2014

Introduction

This is part 1 of my series, Number and Boolean Objects in ECMAScript. In this part of the series I talk about number objects in ECMAScript. Number objects are not exactly numbers. Number objects have properties and numbers do not have properties.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

The Advanced Course and Pre-Knowledge
The ECMAScript course is divided into two parts: the professional course and the advanced course. This tutorial is the start of the advanced course in ECMAScript. If you complete both the professional and the advanced course, you will become a master in ECMAScript.

Every computer language builds up. You need to learn something today in the language, and then use it to learn something at a higher level in the same language, tomorrow. This series is part of my volume, ECMAScript Course. At the bottom of this page, you have links to the different series you should have read before reaching here.

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 can convert 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.valueOf();

        document.write(str);

    </script>

The output is "124.59".

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. We stop here and continue in the next part.

Chrys

Related Links

ECMAScript Basics
ECMAScript Operators
Expressions and Statements in ECMAScript
Custom Object in ECMAScript
ECMAScript Global Object
The ECMAScript Array
The ECMAScript String Object
ECMAScript Regular Expressions
ECMAScript Date Object
ECMAScript Error Basics
Advanced Course
Number Objects in ECMAScript
Inheritance in ECMAScript
Advanced ECMAScript Regular Expressions
Mastering the ECMAScript (JavaScript) eval Function
More Related Links
Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course

NEXT

Comments

Become the Writer's Fan
Send the Writer a Message