DEV Community

Kate Galushko
Kate Galushko

Posted on

Most Common Number Methods and Properties in Javascript

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.

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

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

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

console.log(result1);
//output: 123
Enter fullscreen mode Exit fullscreen mode

You can find more examples of 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.

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

console.log(result2);
// Output: 9.66e+0
Enter fullscreen mode Exit fullscreen mode

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

// 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
Enter fullscreen mode Exit fullscreen mode

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

// 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
Enter fullscreen mode Exit fullscreen mode

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

//  VALUEOF Method 

const num4 = 123;

var result7 = num4.valueOf(4);

console.log(result7);
//output: 123
Enter fullscreen mode Exit fullscreen mode

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

// 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
Enter fullscreen mode Exit fullscreen mode

You can find more examples of 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.

// 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
Enter fullscreen mode Exit fullscreen mode

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

//   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
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (1)

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Most of the content here is near identical to this post:

Even the code examples are the same!

It could be that they're both copied from the same source, or one was copied from the other. Either way, what was the point?