Skip to main content

Number() in JavaScript – How to Convert Values to Numbers

Whenever you invoke Number(), the method converts its argument to a number.

note

Suppose Number() cannot convert its argument to a Number type. In that case, the method will return NaN (Not a Number).

Syntax of the Number() Method

Number() accepts only one argument. Here is the syntax:

Number(value);

Example 1: Convert String Number to Number Type

Number("578");

// The invocation above will return: 578

Try it on StackBlitz

Example 2: Convert String Text to Number Type

Number("Pink");

// The invocation above will return: NaN

Try it on CodePen

Example 3: Convert true to Number Type

Number(true);

// The invocation above will return: 1

Try it on StackBlitz

Example 4: Convert false to Number Type

Number(false);

// The invocation above will return: 0

Try it on StackBlitz

Example 5: Convert Date to Number Type

Number(new Date(2022, 06, 01));

// The invocation above will return: 1656630000000

Try it on StackBlitz

Overview

The Number() global method converts its argument to a Number type or NaN.