Most Common Number Methods and Properties in Javascript
Tags: javascript, codenewbie, beginners, programming
Published: 2022-10-01 03:54:18

JavaScript General Use-Full Number Methods and Properties. Some use the full number of methods of Javascript used in the Javascript programming.

**TOSTRING**
The toString() method returns a number as a string.

```javascript
const num = 123;
var result = num.toString();

console.log(result);
// output: 123

var result1 = (100+23).toString();

console.log(result1);
//output: 123
```

You can find more examples of how to convert an [integer to a string in JavaScript](https://devhubby.com/thread/how-to-convert-an-integer-to-a-string-in-javascript).


**TOEXPONENTIAL**
The toExponential() method returns a string, with a number rounded and written using exponential notation.

```javascript
const num1 = 9.656;
var result2 = num.toExponential(2);

console.log(result2);
// Output: 9.66e+0
```

**TOFIXED**
The toFixed() method returns a string, with the number written with a specified number of decimals.

```javascript
// TOFIXED Method 

const num2 = 9.656;

var result3 = num2.toFixed(0);

console.log(result3);
  
// Output: 10

var result4 = num2.toFixed(2);

console.log(result4);

//output: 9.66
```

**TOPRECISION**
The toPrecision() method returns a string, with a number written with a specified lengh.

```javascript
// TOPRECISION Method 

const num3 = 9.656;

var result5 = num3.toPrecision(2);

console.log(result5);
 
// output: 9.7

var result6 = num3.toPrecision(4);

console.log(result6);
 
//output: 9.656
```

**VALUEOF**
The valueOf() method returns a number as a number.

```javascript
//  VALUEOF Method 

const num4 = 123;

var result7 = num4.valueOf(4);

console.log(result7);
//output: 123
```

**NUMBER**
The number() method can be used to convert JavaScript variables to numbers.

```javascript
// Number Method

console.log(Number(true));
console.log(Number(false));
console.log(Number("10"));
console.log(Number("10.23"));
console.log(Number("10,23"));
//oputput
// 1
// 0
// 10
// 10.23
// NaN
```

You can find more examples of how to convert [string to an integer in JavaScript](https://devhubby.com/thread/how-to-convert-string-to-an-integer-in-javascript).

**PARSEINT**
The parseInt() method perses a string and return a whole number. Spaces are allowed. Only the first number is returned.

```javascript
// ParseInt Method
console.log(parseInt(“10”));
console.log(parseInt(“10.33”));
console.log(parseInt(“10 20 30”));
console.log(parseInt(“10 years”));
console.log(parseInt(“years 10”));
// Output
//10
//10
//10
//10
//NaN
```

**PARSEFLOAT**
The parseFloat() method parses a string and return a number. Spaces are allowed. Only the first number is returned.

```javascript
//   ParseFloat Method

console.log(parseFloat("10"));
console.log(parseFloat("10.33"));
console.log(parseFloat("10 20 30"));
console.log(parseFloat("10 years"));
console.log(parseFloat("years 10"));
// Output
// 10
// 10.33
// 10
// 10
// NaN
```

If you think I missed something - please comment or like it please :)

If you still have any coding or programming questions you can ask them on one of the top rated [Q&A sites for coding questions](https://elvanco.com/blog/best-qanda-sites-for-coding-and-programming).