DONATE  
MENU
HTML
AJAX
JSON
JAVASCRIPT
CSS
PHP
MYSQL

JavaScript Tutorials

INTRODUCTION

» Introduction to JavaScript

» Practice IT Editor

» JavaScript Example

» JavaScript Placement

» JavaScript Syntax

» JavaScript Statements

» JavaScript Input

» JavaScript Output

» JavaScript Comments

» JavaScript Variables

» Variables' Scope

» JavaScript Operators

» JS Expressions & Concatenation

» JavaScript Data Type

» JavaScript Numbers

» Number Object

» JavaScript Strings

» String Object

» JavaScript Booleans

» Boolean Object

» JavaScript Functions

» JavaScript Arrays

» Array Object - Properties

» Array Object - Methods

» JavaScript Objects

» JavaScript Undefined

» JavaScript Nulls

CONDITIONAL TESTING

» If Statement

» If Else Statement

» JavaScript Ternary Operator

» Switch Statement

LOOPS

» For Loops

» While Loops

» Break & Continue

» For...In Loops

» do...while Loop

FUNCTIONS

» JavaScript Functions

» Creating Functions

» Functions' Parameters

» Functions' Arguments

» Function Invocation

» The Return Keyword

» The Arguments Object

» Callback Functions

» Arrow Functions

ARRAYS

» JavaScript Arrays

» Creating Arrays

» Accessing Arrays

» Looping Arrays

» Multi - dimensional Arrays

OBJECTS

» JavaScript Objects

» Creating Objects

» Prototype Definition

» Accessing Objects

» The this Keyword

AJAX

» Introduction to AJAX

JSON

» Introduction to JSON

JAVASCRIPT DATES

» The Date Object

» Date Methods

» Timing (SetInterval & SetTimeout)

MATH OBJECT

» Math Object

» Math Methods

HTML / JS DOM

» DOM Introduction

» Accessing HTML Elements in DOM

» Get And Change Elements' Contents & Values

» Change Styling - DOM CSS

» DOM Attributes

» JavaScript Event Listeners

» DOM Event Listeners

» JS Events

MISCELLANEOUS

» JS Strict Mode

» JavaScript Hoisting

» JavaScript let keyword

» Reserved Keywords

» JS Best Practices

» JavaScript vs ECMAScript

JAVASCRIPT EXAMPLES

» JavaScript Calculator

.. » JavaScript » Introduction » Number Object
Share Tweet WhatsApp

Number Object

JavaScript numbers though primitive have an object that can help in manipulating them. Numbers can also be created directly from the object,
var num = new Number(25);
Nevertheless, It is best to create numbers using the explicit method e.g var num = 25;. The number object have properties and methods that can be used in manipulating all JavaScript numbers whether created from the numbers' object or using the explicit method of creating numbers. In this page, we shall get familiarized with some of these properties and methods.
KeyNote: You can proceed with the tutorial and get back later on to get familiarized with the properties and methods.
Properties 1. MAX_SAFE_INTEGER The MAX_SAFE_INTEGER of the Number object returns the maximum number value that can be correctly represented and interpreted by JavaScript. Any addition to this won't be correctly interpreted. It's value is 9007199254740991.
<script>
var num1 = Number.MAX_SAFE_INTEGER;
var num2 = Number.MAX_SAFE_INTEGER;
num1 += 1; //adds 1
num2 += 2; //adds 2
    
if (num1 == num2){
alert("Yes"); //it will be alerted because JavaScript can no longer interpret the difference between num1 and num2
}
</script>

Copy PRACTICE IT

2. MAX_VALUE The MAX_VALUE property returns the highest positive number in JavaScript. Any addition to it results to positive infinity. It's value is 1.7976931348623157e+308
Number.MAX_VALUE;
3. MIN_SAFE_INTEGER The MIN_SAFE_INTEGER returns the minimum value that can be correctly represented and interpreted by JavaScript. Any subtraction from this won't be correctly interpreted. It's value is -9007199254740991.
<script>
var num1 = Number.MIN_SAFE_INTEGER;
var num2 = Number.MIN_SAFE_INTEGER;
num1 -= 1; //subtracts 1
num2 -= 2; //subtracts 2
    
if (num1 == num2){
alert("Yes"); //it will be alerted because JavaScript can no longer interpret the difference between num1 and num2
}
</script>

Copy PRACTICE IT

4. MIN_VALUE The MIN_VALUE property returns the lowest positive number in JavaScript. Any subtraction from it results to zero(0) or a negative number. It's value is 5e-324 5. NEGATIVE_INFINITY It's value represents the negative infinity value which is -infinity. 6. POSITIVE_INFINITY It's value represents the positive infinity value which is infinity. The above properties of the the Number object are capitalized because they are constants and their values cannot be change. They are also static because you don't need to create an object of the Number() prototype or create a number value before using them. Methods 1. isFinite() The isFinite() is a static method that can be used to determine if a number is countable or less than positive infinity and greater than negative infinity.
<script>
var num = 80;
alert(Number.isFinite(num)); //true

num = Number.POSITIVE_INFINITY;
alert(Number.isFinite(num)); //false
</script>

Copy PRACTICE IT

2. isInteger() To check if a number is an integer. That is, no decimal points, we use the static method, isInteger().
<script>
var num = 80;
alert(Number.isInteger(num)); //true
num = 80.1;
alert(Number.isInteger(num)); //false
</script>

Copy PRACTICE IT

3. isNaN() To check if a value is "not a number", use the static method, isNaN().
<script>
var num = 2 * "two";
alert(Number.isNaN(num)); //true
</script>

Copy PRACTICE IT

4. isSafeInteger() To check if a value is a safe positive/negative integer or not, use the static method, isSafeInteger(). 5. toExponential() Converts a number to exponential notation.
<script>
var myNum = 240;
alert(myNum.toExponential());
</script>

Copy PRACTICE IT

6. toFixed() The toFixed() method formats the digits after a number's decimal point to a specified length. For example, if a number is 23.456 and you use the toFixed() method to format it to 2, the result will be 23.46. Here, the length of the digits after the decimal point (456) was reduced to 2 as specified thereby producing (46).
<script>
var num = 23.456;
document.write(num.toFixed(2)); //23.46
</script>

Copy PRACTICE IT

7. toPrecision() The toPrecision() method is almost like the toFixed() method but here, the length of the whole number including the number before the decimal point is reduced to a specified length. For example, using the toPrecision() method to reduce 23.456 to 2 will produce 23.
<script>
var num = 23.456;
document.write(num.toPrecision(2)); //23
</script>

Copy PRACTICE IT

8. toString() Converts a number to a string. Can also be used in converting numbers from one base to another when you assign an argument.
<script>
var num = 50;
alert(typeof(num.toString())); //string
num = 63;
alert(num.toString(2)); //converts to base 2
</script>

Copy PRACTICE IT

By converting a number to a string, you can be able to use the length method of the string object to know its length and thereafter carry out whatever operations.
<script>
var num = 10003;
var len = num.toString().length; //the length will be 5
document.write("The length of the number, "+num+" is "+len);
</script>

Copy PRACTICE IT

9. valueOf() The valueOf() method returns the primitive value of a number.
<script>
var x = 0/0;
alert(x.valueOf());
</script>

Copy PRACTICE IT

Using the valueOf() method in your code is meaningless. It is used internally by JS and it is what is used so that whenever you use or output a number(an object), it returns the number instead of [object Number]. SUMMARY
  1. All JavaScript numbers possess an object.
  2. The object contains mostly constant properties that enable you get some distinct number values.
  3. The object contains methods that enable you manipulate numbers and determine certain features of a number.
Take a short quiz on Number Object.
Ask a question or tell us if Number Object is easy or not.
cent?

Join our 1,090 subscribers to receive tutorials' updates!

  We won't spam your mail box. We hate it.





StackBeginner 2021 © Joseph Omamode