JavaScript Tutorials - Herong's Tutorial Examples - 2.33, by Herong Yang
The "Number" Object Type - Not Equal to Number Primitive Type
This section provides a quick description and a tutorial example script on the 'Number' built-in object type, which is used to create 'Number' objects.
The "Number" object type is a special built-in object type to be used to create a "Number" object, which is different than a Number value of the Number primitive type. It has the following main features:
Here is a tutorial example script showing some of those features:
// Number_Object_Type.js // Copyright (c) 2013 HerongYang.com. All Rights Reserved. // Creating a new Number object var myObject = new Number(3.14159); // Checking this object println("\nAbout this \"myObject\":"); println(" Type = "+(typeof myObject)); println(" Instance Of Object: "+(myObject instanceof Object)); println(" Instance Of Number: "+(myObject instanceof Number)); // Using Number's inherited methods var v = myObject.valueOf(); var s = myObject.toString(); var f = myObject.toFixed(2); println("\nExecution result:"); println(" typeof myObject = "+(typeof myObject)); println(" typeof myObject.valueOf() = "+(typeof v)); println(" typeof myObject.toString() = "+(typeof s)); println(" typeof myObject.toFixed() = "+(typeof f)); println(" value of myObject.valueOf() = "+v); println(" value of myObject.toString() = "+s); println(" value of myObject.toFixed() = "+f); println("\nConverting to Number objects on the fly:"); println(" (2/3).toFixed() = "+((2/3).toFixed(2))); println("\nUsing properties of the Number constructor:"); println(" Number.MAX_VALUE = "+Number.MAX_VALUE); println(" Number.MIN_VALUE = "+Number.MIN_VALUE);
If you run this script with "jrunscript", you will get:
About this "myObject": Type = object Instance Of Object: true Instance Of Number: true Execution result: typeof myObject = object typeof myObject.valueOf() = number typeof myObject.toString() = string typeof myObject.toFixed() = string value of myObject.valueOf() = 3.14159 value of myObject.toString() = 3.14159 value of myObject.toFixed() = 3.14 Converting to Number objects on the fly: (2/3).toFixed() = 0.67 Using properties of the Number constructor: Number.MAX_VALUE = 1.7976931348623157e+308 Number.MIN_VALUE = 5e-324
Now we know how to create "Number" objects with the "Number()" constructor.
Read other "String" related chapters to see more tutorial examples.
Table of Contents
ECMAScript Language Specification and JavaScript Dialects
Data Types, Variables and Expressions
Creating, Accessing, and Manipulating Arrays
Defining and Calling Functions
Web Browser Supporting JavaScript
Server-Side and Client-Side Web Scripting
Defining Your Own Object Types
Inheritance of Properties and Methods through the Prototype Object Chain
'jrunscript' - JavaScript Shell Command from JDK
Using Functions as "Function" Objects
►Introduction to Built-in Object Types
Overview of Built-in Object Types
The "Object" Object Type - The Root Object Type
The "Global" Object Type - The Invisible Global Container
Global Properties and Functions Defined in ECMAScript
Global Properties and Functions Provided by "jrunscript"
The "Function" Object Type - Functions Are Objects
The "Array" Object Type - Arrays Are Objects
The "JSON" Object Type - parse() and stringify()
The "String" Object Type - Not Equal to String Primitive Type
The "Boolean" Object Type - Wrapping Boolean Values into Objects
►The "Number" Object Type - Not Equal to Number Primitive Type
The "Date" Object Type - Managing Dates and Times
The "RegExp" Object Type - Regular Expression Patterns
The "Error" Object Type - Runtime Exceptions
The "Math" Object Type - The Math Container
W3C's Document Object Model (DOM) Specifications