Tutorial: Javascript Number Methods


By Emmanuel Chinonso

Web Developer

JavaScript Number Methods

You can use number methods to work with numbers.

Contrast Bootstrap UI Kit

Number Methods and Properties

Property and method cannot be assigned to primitive values (such as 3.14 or 2014). (because they are not objects). However, primitive values can use JavaScript methods and properties because JavaScript interprets primitive values as objects when executing methods and properties.

The toString() Method The toString() method returns a string representation of an integer. Any sort of number can be utilized with any number procedure (literals, variables, or expressions).

JavaScript Code:

1var x = 123;
2x.toString(); // returns 123 from variable x
3(123).toString(); // returns 123 from literal 123
4(100 + 23).toString(); // returns 123 from expression 100 + 23.

The toExponential() Method

toExponential() provides a string containing a number that has been rounded and expressed in exponential notation. The number of characters after the decimal point is defined by a parameter. The parameter is not required. JavaScript will not round the number if you don't specify it.

JavaScript Code:

1var x = 9.656;
2x.toExponential(2); // returns 9.66e+0
3x.toExponential(4); // returns 9.6560e+0
4x.toExponential(6); // returns 9.656000e+0.

The valueOf() Method

valueOf() returns a number in its original form. A number can be either a primitive value (typeof = number) or an object (typeof = object) in JavaScript. In JavaScript, the valueOf() function is used to convert Number objects to primitive values. It's pointless to utilize it in your code.

JavaScript Code:

1var x = 123;
2x.valueOf(); // returns 123 from variable x
3(123).valueOf(); // returns 123 from literal 123
4(100 + 23).valueOf(); // returns 123 from expression 100 + 23.

Converting Variables to Numbers

To convert variables to numbers, you can use one of three JavaScript methods:

  • The parseInt() method
  • The Number() method
  • The method parseFloat()

These methods are global JavaScript methods, not numerical methods.

Global JavaScript Methods

All JavaScript data types can be utilized with global methods in JavaScript. When working with numbers, these are the most useful techniques:

Number()Returns a number, converted from its argument.
parseFloat()Parses its argument and returns a floating point number
parseInt()Parses its argument and returns an integer

The Number() Method; To convert JavaScript variables to numbers, use Number(). If the number cannot be converted, the result is NaN (Not a Number).

JavaScript Code:

1Number(true); // returns 1
2Number(false); // returns 0
3Number('10'); // returns 10
4Number(' 10'); // returns 10
5Number('10 '); // returns 10
6Number(' 10 '); // returns 10
7Number('10.33'); // returns 10.33
8Number('10,33'); // returns NaN
9Number('10 33'); // returns NaN
10Number('John'); // returns NaN.

The Number() Method Used on Dates: A date can also be converted to a number using Number():

JavaScript Code:

1Number(new Date('2017-09-30')); // returns 1506729600000

Since 1.1.1970, the Number() method returns the number of milliseconds.

The parseInt() Method: parseInt() returns a whole number after parsing a string. There are no restrictions on the number of spaces available. The first number is the only one that is returned:

JavaScript Code:

1parseInt('10'); // returns 10
2parseInt('10.33'); // returns 10
3parseInt('10 20 30'); // returns 10
4parseInt('10 years'); // returns 10
5parseInt('years 10'); // returns NaN

If the number cannot be translated, the result is NaN (Not a Number).

The parseFloat() Method: parseFloat() returns a number after parsing a string. There are no restrictions on the number of spaces available. The first number is the only one that is returned:

JavaScript Code:

1parseFloat('10'); // returns 10
2parseFloat('10.33'); // returns 10.33
3parseFloat('10 20 30'); // returns 10
4parseFloat('10 years'); // returns 10
5parseFloat('years 10'); // returns NaN.

Number Properties

MAX_VALUEReturns the largest number possible in JavaScript
MIN_VALUEReturns the smallest number possible in JavaScript
POSITIVE_INFINITYRepresents infinity (returned on overflow)
NEGATIVE_INFINITYRepresents negative infinity (returned on overflow)
NaNRepresents a "Not-a-Number" value.

Variables Cannot Have Number Properties Number attributes are part of the Number number object wrapper in JavaScript. Only Number can be used to access these properties. MAX VALUE. I'm going to use myNumber. MAX VALUE will return undefined if myNumber is a variable, expression, or value:

JavaScript Code:

1var x = 6;
2var y = x.MAX_VALUE; // y becomes undefined.

Contrast Bootstrap UI Kit

Create Stunning websites and web apps

Trying to build out all user interfaces and components for your website or web app from scratch can become a very tedious task. A huge reason why we created Contrast Bootstrap to help reduce the amount of time we spend doing that, so we can focus on building some other aspects of the project. Contrast Bootstrap PRO consists of a UI Kit featuring over 10000+ component variants. Together with a template of 5 admin dashboards and 23+ additional multipurpose pages template for building almost any type of website or web app. You can view a demo and learn more about Contrast by clicking here.Try contrast pro


Contrast Bootstrap PRO was built using the most popular CSS framework Bootstrap to help build your next landing, admin SAAS, prelaunch etc project with a clean, prebuilt and well documented template and UI components.Learn more about Contrast

Related Posts