Table of Contents
JavaScript Numeric Operation
We have already performed several arithmetic operations on numbers till now. Apart from this, JavaScript provides following operations on numbers:
Operation | Description | Example |
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 variable | var 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 |
0 Comments