Type Conversions
Type Conversions
EN
Buy EPUB/PDF
→ The JavaScript language → JavaScript Fundamentals
For example, alert automatically converts any value to a string to show it. Mathematical operations convert
values to numbers.
There are also cases when we need to explicitly convert a value to the expected type.
Later, after we learn about objects, in the chapter Object to primitive conversion we’ll see how objects fit in.
String Conversion
String conversion happens when we need the string form of a value.
String conversion is mostly obvious. A false becomes "false" , null becomes "null" , etc.
Numeric Conversion
Numeric conversion in mathematical functions and expressions happens automatically.
https://javascript.info/type-conversions 1/4
3/9/25, 11:57 AM Type Conversions
Explicit conversion is usually required when we read a value from a string-based source like a text form but
expect a number to be entered.
If the string is not a valid number, the result of such a conversion is NaN . For instance:
Value Becomes…
undefined NaN
null 0
true and false 1 and 0
Whitespaces (includes spaces, tabs \t , newlines \n etc.) from the start and end are
string removed. If the remaining string is empty, the result is 0 . Otherwise, the number is “read”
from the string. An error gives NaN .
Examples:
Please note that null and undefined behave differently here: null becomes zero while undefined
becomes NaN .
Most mathematical operators also perform such conversion, we’ll see that in the next chapter.
Boolean Conversion
Boolean conversion is the simplest one.
It happens in logical operations (later we’ll meet condition tests and other similar things) but can also be
performed explicitly with a call to Boolean(value) .
For instance:
Summary
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.
Value Becomes…
undefined NaN
null 0
true / false 1 / 0
The string is read “as is”, whitespaces (includes spaces, tabs \t , newlines \n etc.) from both
string
sides are ignored. An empty string becomes 0 . An error gives NaN .
Value Becomes…
https://javascript.info/type-conversions 3/4
3/9/25, 11:57 AM Type Conversions
Value Becomes…
any other value true
Most of these rules are easy to understand and memorize. The notable exceptions where people usually make
mistakes are:
Objects aren’t covered here. We’ll return to them later in the chapter Object to primitive conversion that is
devoted exclusively to objects after we learn more basic things about JavaScript.
Comments
● If you have suggestions what to improve - please submit a GitHub issue or a pull request instead of
commenting.
● If you can't understand something in the article – please elaborate.
● To insert few words of code, use the <code> tag, for several lines – wrap them in <pre> tag, for
more than 10 lines – use a sandbox (plnkr, jsbin, codepen…)
https://javascript.info/type-conversions 4/4