Conversions

Top  Previous  Next

Most operators, functions, methods, and properties accept operands/arguments of specific types. If the argument is not of the correct type, an automatic conversion is performed according to these rules.

 

To Boolean

Undefined

false

Null

false

Number

false if 0 or NaN, otherwise true

String

false if empty string, otherwise true

Object

true

 

To Number

Undefined

NaN

Null

0

Boolean

0 if false, otherwise 1

String

parse string as number, NaN if not valid (see below)

Object

NaN

 

To String

Undefined

“undefined”

Null

“null”

Boolean

“false” if false, otherwise “true”

Number

convert to string representation

Object

empty string, except arrays which return array.join()

Numeric String Parsing

When parsing a string to a number, a string with the same format as a numeric literal will be accepted. In addition, white space before and after the string is ignored, but embedded white space is not accepted. The number may be preceded by a "+" or "-" sign and leading zeros, unless the number is hex format (starts with "0x" or "0X").

 

An empty string, or a string containing nothing but white space, is accepted and returns zero.

 

The strings "Infinity", "+Infinity", and "-Infinity" are accepted. They may include leading and trailing white space.