JavaScript: Number function
Description
The number function is used to converts a specified object to a number.
Syntax
Number(obj)
Parameters
obj: Required. An object.
Note: If the object is a Date object the function returns a value in milliseconds calculated from 01 January, 1970 UTC (GMT). The return value is positive if the given date is after the said date and negative if before the said date.
If the object is a string and if there is no well-formed numeric literal the function returns NaN.
Example of Number() function
The following example demonstrates how to use Number() function.
JavaScript Code:
var str1 = "4400.23";
var num1 = Number(str1);
console.log('"4400.23" converted to : '+num1);
View the example in the browser
Live demo:
See the Pen number-function-1 by w3resource (@w3resource) on CodePen.
Previous: JavaScript: NaN
Next:
JavaScript: String function
JavaScript: Tips of the Day
Returns all unique values of an array, based on a provided comparator function
Example:
const tips_uniqueElementsBy = (arr, fn) => arr.reduce((acc, v) => { if (!acc.some(x => fn(v, x))) acc.push(v); return acc; }, []); console.log(tips_uniqueElementsBy( [ { id: 0, value: 'p' }, { id: 1, value: 'q' }, { id: 2, value: 'r' }, { id: 1, value: 's' }, { id: 0, value: 't' } ], (a, b) => a.id == b.id ));
Output:
[[object Object] { id: 0, value: "p" }, [object Object] { id: 1, value: "q" }, [object Object] { id: 2, value: "r" }]
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- React - JavaScript Library
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework