JavaScript Numeric Operation

JavaScript Numeric Operation

We have already performed several arithmetic operations on numbers till now. Apart from this, JavaScript provides following operations on numbers:

OperationDescriptionExample
toString()It returns numbers as strings.var x = 123;

var y = x.toString();

Output: “123”

toFixed()It rounds a number to a given number of digits and returns it in the form of a string.var x = 9.656;

x.toFixed(0); Output: 10

x.toFixed(2); Output: 9.66

valueOf()It returns the value of a variablevar x = 123;

x.valueOf();

Output: 123

Number()It converts variables into a number. It can also convert a date into a number.Number(new Date(“2019-05-01”));

Output: 1556668800000

parseInt()It parses a string and returns a whole number. It returns only the first number.parseInt(“10.33”);

Output: 10

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.