0% found this document useful (0 votes)
3 views

Type Conversions

The document discusses type conversions in JavaScript, focusing on string, numeric, and boolean conversions. It explains how values are automatically converted to the appropriate type in various operations and provides rules for explicit conversions using functions like String(value) and Number(value). Key points include the behavior of undefined, null, and the treatment of non-empty strings in boolean contexts.

Uploaded by

rakibul8998r11
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Type Conversions

The document discusses type conversions in JavaScript, focusing on string, numeric, and boolean conversions. It explains how values are automatically converted to the appropriate type in various operations and provides rules for explicit conversions using functions like String(value) and Number(value). Key points include the behavior of undefined, null, and the treatment of non-empty strings in boolean contexts.

Uploaded by

rakibul8998r11
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

3/9/25, 11:57 AM Type Conversions

EN
Buy EPUB/PDF 
 → The JavaScript language → JavaScript Fundamentals

 January 24, 2023


Type Conversions
Most of the time, operators and functions automatically convert the values given to them to the right type.

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.

 Not talking about objects yet


In this chapter, we won’t cover objects. For now, we’ll just be talking about primitives.

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.

For example, alert(value) does it to show the value.

We can also call the String(value) function to convert a value to a string:

1 let value = true;


 
2 alert(typeof value); // boolean
3
4 value = String(value); // now value is a string "true"
5 alert(typeof value); // string

String conversion is mostly obvious. A false becomes "false" , null becomes "null" , etc.

Numeric Conversion
Numeric conversion in mathematical functions and expressions happens automatically.

For example, when division / is applied to non-numbers:

1 alert( "6" / "2" ); // 3, strings are converted to numbers


 

We can use the Number(value) function to explicitly convert a value to a number:

https://javascript.info/type-conversions 1/4
3/9/25, 11:57 AM Type Conversions

1 let str = "123";


 
2 alert(typeof str); // string
3
4 let num = Number(str); // becomes a number 123
5
6 alert(typeof num); // number

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:

1 let age = Number("an arbitrary string instead of a number");


 
2
3 alert(age); // NaN, conversion failed

Numeric conversion rules:

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:

1 alert( Number(" 123 ") ); // 123


 
2 alert( Number("123z") ); // NaN (error reading a number at "z")
3 alert( Number(true) ); // 1
4 alert( Number(false) ); // 0

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) .

The conversion rule:


https://javascript.info/type-conversions 2/4
3/9/25, 11:57 AM Type Conversions
● Values that are intuitively “empty”, like 0 , an empty string, null , undefined , and NaN , become
false .
● Other values become true .

For instance:

1 alert( Boolean(1) ); // true


 
2 alert( Boolean(0) ); // false
3
4 alert( Boolean("hello") ); // true
5 alert( Boolean("") ); // false

 Please note: the string with zero "0" is true


Some languages (namely PHP) treat "0" as false . But in JavaScript, a non-empty string is always
true .

1 alert( Boolean("0") ); // true


 
2 alert( Boolean(" ") ); // spaces, also true (any non-empty string is

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.

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 (includes spaces, tabs \t , newlines \n etc.) from both
string
sides are ignored. An empty 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

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:

● undefined is NaN as a number, not 0 .


● "0" and space-only strings like " " are true as a boolean.

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.

 Previous lesson Next lesson



Share    Tutorial map

 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…)

© 2007—2025 Ilya Kantorabout the projectcontact usterms of usage


privacy policy

https://javascript.info/type-conversions 4/4

You might also like