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 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
Clean Form Input With These PHP Functions Before Inserting into DatabasePHPWhat’s a Closure in JavaScript?JavaScriptEssential SQL Commands We Need to KnowSQL/MySQLWordPress: How to set WP_SITEURL?WordPressSolution for Xcode 11 Command PhaseScriptExecution failed with a nonzero exit code errorMiscellaneousSelector Lists and Combinators in SCSS (Sass)SCSS