Yogesh Chauhan's Blog

Number Properties in JavaScript

in JavaScript on July 19, 2020

constructor

Returns the constructor function for an object.

The return value is a reference to the function, not the name of the function:

=> For JavaScript numbers –> returns function Number() { [native code] }

=> For JavaScript strings –> returns function String() { [native code] }

=> For JavaScript booleans –> returns function Boolean() { [native code] }

Example


number = 1;
console.log(number.constructor);

//[Function: Number] 
//in console.log you don't get { [native code] } part

MAX_VALUE

Returns the largest number possible in JavaScript.

This static property has a value of 1.7976931348623157e+308.

Numbers larger than MAX_VALUE are represented as infinity.

Example


console.log(Number.MAX_VALUE);
// 1.7976931348623157e+308

MAX_VALUE is a static property of the JavaScript Number object. We can only use it as Number.MAX_VALUE.

Using x.MAX_VALUE, where x is a number or a Number object, will return undefined:


number = 1;
console.log(number.MAX_VALUE);

//undefined

MIN_VALUE

Returns the smallest positive number possible in JavaScript.

This static property has a value of 5e-324.

Numbers smaller than this are converted to 0.


console.log(Number.MIN_VALUE);

//5e-324

MIN_VALUE is the value closest to 0. The most negative number is the negative MAX_NUMBER.

MIN_VALUE is a static property of the JavaScript Number object. You can only use it as Number.MIN_VALUE.

Using x.MIN_VALUE, where x is a number or a Number object, will return undefined:


number = 1;
console.log(number.MIN_VALUE);

//undefined

NEGATIVE_INFINITY

The NEGATIVE_INFINITY property represents negative infinity, means something that is lower than any other number.

Example


console.log(Number.NEGATIVE_INFINITY)

//-Infinity

NEGATIVE_INFINITY is a static property of the JavaScript Number object.

You can only use it as Number.NEGATIVE_INFINITY.

Using x.NEGATIVE_INFINITY, where x is a number or a Number object, will return undefined:


number = 1;
console.log(number.NEGATIVE_INFINITY);

//undefined

NaN

The NaN property represents “Not-a-Number” value. This property indicates that a value is not a legal number.

The NaN property is the same as the Number.Nan property.

Checkout this related post: The Difference Between IsNaN() Method And IsNaN() Function In JavaScript

POSITIVE_INFINITY

The POSITIVE_INFINITY property represents positive infinity which is something that is higher than any other number.

Example


console.log(Number.POSITIVE_INFINITY)

//Infinity

POSITIVE_INFINITY a static property of the JavaScript Number object.

You can only use it as Number.POSITIVE_INFINITY.

Using x.POSITIVE_INFINITY, where x is a number or a Number object, will return undefined:


number = 1;
console.log(number.POSITIVE_INFINITY);

//undefined

prototype

The prototype constructor allows you to add new properties and methods to JavaScript numbers.

When constructing a property, ALL numbers will be given the property, and its value, as default.

When constructing a method, ALL numbers will have this method available.

Example


Number.prototype.methodTemp = function() {
  return this.valueOf() * 2;
};

function tempFunction() {
  var number = 1;
  document.getElementById("any").innerHTML = n.methodTemp();
}


//assuming we have HTML element with an id = any
//it will print 2

Number.prototype does not refer to a single number object, but to the Number() object itself.

Prototype is a global object constructor which is available for all JavaScript objects.

Number Properties Cannot be Used on Variables

Number properties belongs to the JavaScript’s number object wrapper called Number.

These properties can only be accessed as Number.MAX_VALUE.

Using myNumber.MAX_VALUE, where myNumber is a variable, expression, or value, will return undefined:


var a = 1;
var b = a.MAX_VALUE;     

console.log(b);

//undefined

Credit: w3schools


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to select multiple values in React dropdown?ReactWordPress: How to print ACF array field values?WordPressHow to deploy Angular App on Dreamhost or Amazon S3 or firebase?Angular2 Ways We Can Write Multiple Line Commands in PHPPHPHow to progressively load images and add a blurry placeholder?CSSWhat is a component helper function in React?React