Tutorial: Javascript Number Methods
Tags: javascript, programming, webdev, codenewbie
Published: 2022-01-22 10:49:12

---
title: Tutorial: Javascript Number Methods
published: True
date: 2021-01-10 01:16:04 UTC
tags: ["javascript", "Programming", 'webdev', 'codenewbie']
canonical_url: https://www.devwares.com/blog//javascript-number-methods
---

## JavaScript Number Methods

You can use number methods to work with numbers.


**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:**

```javascript
var x = 123;
x.toString(); // returns 123 from variable x
(123).toString(); // returns 123 from literal 123
(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:**

```javascript
var x = 9.656;
x.toExponential(2); // returns 9.66e+0
x.toExponential(4); // returns 9.6560e+0
x.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:**

```javascript
var x = 123;
x.valueOf(); // returns 123 from variable x
(123).valueOf(); // returns 123 from literal 123
(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:

| Method | Description |
| --- | --- |
| 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:**

```javascript
Number(true); // returns 1
Number(false); // returns 0
Number('10'); // returns 10
Number(' 10'); // returns 10
Number('10 '); // returns 10
Number(' 10 '); // returns 10
Number('10.33'); // returns 10.33
Number('10,33'); // returns NaN
Number('10 33'); // returns NaN
Number('John'); // returns NaN.

```

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

**JavaScript Code:**

```javascript
Number(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:**

```javascript
parseInt('10'); // returns 10
parseInt('10.33'); // returns 10
parseInt('10 20 30'); // returns 10
parseInt('10 years'); // returns 10
parseInt('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:**

```javascript
parseFloat('10'); // returns 10
parseFloat('10.33'); // returns 10.33
parseFloat('10 20 30'); // returns 10
parseFloat('10 years'); // returns 10
parseFloat('years 10'); // returns NaN.

```

**Number Properties**

| Property | Description |
| --- | --- |
| MAX\_VALUE | Returns the largest number possible in JavaScript |
| --- | --- |
| MIN\_VALUE | Returns the smallest number possible in JavaScript |
| --- | --- |
| POSITIVE\_INFINITY | Represents infinity (returned on overflow) |
| --- | --- |
| NEGATIVE\_INFINITY | Represents negative infinity (returned on overflow) |
| --- | --- |
| NaN | Represents 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:**

```javascript
var x = 6;
var y = x.MAX_VALUE; // y becomes undefined.

```

[![Contrast Bootstrap UI Kit](https://www.devwares.com/static/6b149e3d75b230f7e8452ace9e21bb05/587b0/banner-ad-1.png "Contrast Bootstrap UI Kit")](https://www.devwares.com/product/bootstrap-contrast-pro)

## Resources

You may find the following resources useful:

- [Get Contrast PRO](https://www.devwares.com/product/contrast-pro)
-[Tutorial: JavaScript Dom Events](https://www.devwares.com/blog/javascript-html-dom-events)
-[Tutorial: JavaScript Dom Methods](https://www.devwares.com/blog/javascript-html-dom-methods)
-[What is Nodejs, Express and When Should It Be Used](https://www.devwares.com/blog/what-is-nodejs-and-express)
-[Tutorial: How JavaScript Works](https://www.devwares.com/blog/javascript-how-it-works)

- [Installation](https://www.devwares.com/docs/contrast/angular/Installation/)
- [Accordion](https://www.devwares.com/docs/contrast/angular/components/accordion/)
- [Alert](https://www.devwares.com/docs/contrast/angular/components/alert/)
- [Animation](https://www.devwares.com/docs/contrast/angular/components/animations/)
- [Autocomplete](https://www.devwares.com/docs/contrast/angular/components/autocomplete/)
- [Badges](https://www.devwares.com/docs/contrast/angular/components/badge/)
- [Box](https://www.devwares.com/docs/contrast/angular/components/box/)
- [Breadcrumb](https://www.devwares.com/docs/contrast/angular/components/breadcrumb/)
- [ButtonGroup](https://www.devwares.com/docs/contrast/angular/components/buttonGroup/)
- [ButtonToolbar](https://www.devwares.com/docs/contrast/angular/components/buttonToolbar/)
- [Button](https://www.devwares.com/docs/contrast/angular/components/buttons/)
- [Checkbox](https://www.devwares.com/docs/contrast/angular/components/checkbox/)
- [Card](https://www.devwares.com/docs/contrast/angular/components/card/)
- [Collapse](https://www.devwares.com/docs/contrast/angular/components/collapse/)
- [DropDown](https://www.devwares.com/docs/contrast/angular/components/dropdown/)
- [Footer](https://www.devwares.com/docs/contrast/angular/components/footer/)
- [InputGroup](https://www.devwares.com/docs/contrast/angular/components/inputgroup/)
- [Iframe](https://www.devwares.com/docs/contrast/angular/components/iframe/)
- [Modal](https://www.devwares.com/docs/contrast/angular/components/modal/)
- [Mask](https://www.devwares.com/docs/contrast/angular/components/mask/)
- [Multiselect](https://www.devwares.com/docs/contrast/angular/components/multiselect/)
- [Notification](https://www.devwares.com/docs/contrast/angular/components/notification/)
- [Pane](https://www.devwares.com/docs/contrast/angular/components/pane/)
- [Panel](https://www.devwares.com/docs/contrast/angular/components/panels/)
- [Progress](https://www.devwares.com/docs/contrast/angular/components/progress/)
- [Radio](https://www.devwares.com/docs/contrast/angular/components/radio/)
- [Rating](https://www.devwares.com/docs/contrast/angular/components/rating/)
- [Select](https://www.devwares.com/docs/contrast/angular/components/select/)
- [Select 2](https://www.devwares.com/docs/contrast/angular/components/select2/)
- [Slider](https://www.devwares.com/docs/contrast/angular/components/slider/)
- [Spinner](https://www.devwares.com/docs/contrast/angular/components/spinner/)
- [Switch](https://www.devwares.com/docs/contrast/angular/components/switch/)
- [Stepper](https://www.devwares.com/docs/contrast/angular/components/stepper/)
- [Icon](https://www.devwares.com/docs/contrast/angular/components/icon/)
- [Input](https://www.devwares.com/docs/contrast/angular/components/input/)
- [ListGroup](https://www.devwares.com/docs/contrast/angular/components/listgroup/)
- [Forms](https://www.devwares.com/docs/contrast/angular/forms/)
- [Navbar](https://www.devwares.com/docs/contrast/angular/navigation/navbar/)
- [Pagination](https://www.devwares.com/docs/contrast/angular/navigation/pagination/)
- [Tab](https://www.devwares.com/docs/contrast/angular/navigation/tabs/)
- [Sidebar](https://www.devwares.com/docs/contrast/angular/navigation/sidebar/)
- [Time Picker](https://www.devwares.com/docs/contrast/angular/sections/timepicker/)
- [Data Table](https://www.devwares.com/docs/contrast/angular/table/datatables/)
- [Editable Table](https://www.devwares.com/docs/contrast/angular/table/editabletable/)
- [Fixed Data Table](https://www.devwares.com/docs/contrast/angular/table/fixed_datatables/)
- [Table](https://www.devwares.com/docs/contrast/angular/table/table/)
- [Widgets](https://www.devwares.com/docs/contrast/angular/widgets/)