JavaScript: Number function


The number function is used to converts a specified object to a number.




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


const tips_uniqueElementsBy = (arr, fn) =>
  arr.reduce((acc, v) => {
    if (!acc.some(x => fn(v, x))) acc.push(v);
    return acc;
  }, []);

    { 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


[[object Object] {
  id: 0,
  value: "p"
}, [object Object] {
  id: 1,
  value: "q"
}, [object Object] {
  id: 2,
  value: "r"