JavaScript Number prototype property

Definition and usage

prototype It allows you to add new properties and methods to numbers.

prototype It is a property available to all JavaScript objects.

Instance

Create a new number method that returns the half value of a number:

Number.prototype.myMethod = function() {
  return this.valueOf() / 2;
};

Use new methods for numbers:

let n = 55;
let x = n.myMethod();

Try it yourself

Syntax

Number.prototype.name = value

Warning

It is not recommended to change the prototype of objects that are not under your control.

You should not change the prototype of built-in JavaScript data types, such as:

  • Numbers
  • Strings
  • Arrays
  • Dates
  • Booleans
  • Function
  • Objects

Please only change the prototype of your own objects.

prototype property

JavaScript prototype Properties allow you to add new properties to an object:

Instance

function Person(first, last, age, eyecolor) {
  this.firstName = first;
  this.lastName = last;
  this.eyeColor = eyecolor;
}
Person.prototype.nationality = "English";

Try it yourself

Browser support

Number.prototype It is a feature of ECMAScript1 (ES1).

All browsers fully support ES1 (JavaScript 1997):

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support Support Support Support Support Support