Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript
communitycreator

What is Number() in JavaScript?

Fatima Hasan

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Definition

Number() takes a variable of any data type and converts it to a number.

Note:

  • If the parameter cannot be converted to a number, then NaN is returned.
  • If a date object is passed, then the number of seconds elapsed since January 1st, 1970 is returned.
  • If more than one number is passed in a string separated by a space, then NaN is returned, unlike the parseFloat() function.

Parameter

An optional variable of any data type, e.g., string, bool, date, etc.

Syntax

Number(value)

Return value

The input parameter converted to a number. If no parameter is provided, then 0 is returned.

Example

console.log( Number("105") )
console.log( Number(true) )
console.log( Number(false) )
console.log( Number("10 20 30") )
console.log( Number("abc") )
console.log( Number(new Date()) )
console.log( Number() )

RELATED TAGS

javascript
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring