0% found this document useful (0 votes)
717 views1 page

JS primitive type conversions

The document outlines the three main types of type conversions: string, numeric, and boolean. String conversion is straightforward for primitive values, numeric conversion follows specific rules for different values, and boolean conversion categorizes values into true or false based on defined criteria. Notable exceptions include undefined being NaN in numeric conversion and certain strings being true in boolean conversion.

Uploaded by

edelisakson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
717 views1 page

JS primitive type conversions

The document outlines the three main types of type conversions: string, numeric, and boolean. String conversion is straightforward for primitive values, numeric conversion follows specific rules for different values, and boolean conversion categorizes values into true or false based on defined criteria. Notable exceptions include undefined being NaN in numeric conversion and certain strings being true in boolean conversion.

Uploaded by

edelisakson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

The three most widely used type conversions are to string, to

number, and to boolean.

String Conversion – Occurs when we output something. Can be performed with


String(value). The conversion to string is usually obvious for primitive values.

Numeric Conversion – Occurs in math operations. Can be performed with Number(value).

The conversion follows the rules:

Value Becomes…
undefined NaN
null 0
true/false 1 / 0
The string is read “as is”, whitespaces from both sides are ignored. An empty string
string
becomes 0. An error gives NaN.

Boolean Conversion – Occurs in logical operations. Can be performed with


Boolean(value).

Follows the rules:

Value Becomes…
0, null, undefined, NaN, "" false
any other value true
Most of these rules are easy to understand and memorize. The notable exceptions where people usually
make mistakes are:
• undefined is NaN as a number, not 0.
• "0" and space-only strings like " " are true as a boolean.

You might also like