Computer scienceProgramming languagesJavaScriptBasicsOperations

Type Conversion

5 minutes read

JavaScript supports automatic data type conversion. This means the interpreter independently converts values to the type it expects to see in a particular part of your program. Such a conversion is implicit and is also called type coercion. At first glance, type coercion might look useful, but sometimes, such conversion may be confusing, leaving you to wonder why your code yields errors or behaves strangely. While programming, you will often find yourself in situations when you need to explicitly (that is, manually) indicate that you need to change the value of a particular data type.

Today, we will learn how strings, booleans, and numbers can be converted into each other.

String conversion

String conversion occurs when you want to represent something as a string. The String() function must be applied to cast a value to a string explicitly. For example:

String(123);   // "123"
String(false); // "false"
String(-12.3); // "-12.3"
String(true);  // "true"

As you can see, the conversion is simple and intuitive.

In JavaScript, implicit conversion to strings occurs when the binary + operator is used and one of the operands is a string:

"3" + 4                        // "34"
4 + ""                         // "4"
true + "detective"             // "truedetective"
"You are " + 25 + " years old" // "You are 25 years old"

Automatic conversion will take place regardless of the position of the operand string — on the right-hand or the left-hand side of the expression.

Remember the order of arithmetic operations. If there are several numbers before the string, these numbers will be added before the conversion:

3 + 10 + "1" // "131", not "3101"

Implicit conversion may be confusing, so be very attentive when writing programs.

Numeric conversion

Numeric conversion occurs when you want to represent something as a number. To perform an explicit conversion, apply the Number() function:

Number("1");    // 1
Number(" 37 "); // 37
Number("");     // 0
Number("\n3");  // 3
Number("\n");   // 0
Number("\t");   // 0
Number(true);   // 1
Number(false);  // 0

When converting a string to a number, spaces and \n and \t characters at the beginning and the end of the string are cut off. If the string turns out to be empty, the result will be 0. The boolean type behaves as expected: false turns into 0 and true turns into 1.

Not all values can be converted into numbers. The result of incorrect conversion attempts is NaN, which means Not a Number. For example, Number("apple") will return a NaN value.

Implicit conversion to numbers is a little more confusing. It occurs in almost all mathematical functions and expressions:

true + 43 // 44
3 - false // 3
10 / "5"  // 2
-true     // -1
+"85"     // 85

To be explicitly converted into a number, a string must be a correct number.

In contrast to the unary + and the Number() function, the binary operator + cannot turn a string into a number. So, the result of the expression "5" + 7 will be a string ("57"), and the result of Number("5" + 7) will be a number (57).

Boolean conversion

Boolean conversion occurs when you want to represent something as a boolean. To explicitly convert data into a boolean value, the Boolean() function should be applied:

Boolean(1);            // true
Boolean(0);            // false
Boolean("Am I nice?"); // true
Boolean("");           // false   

The rules for using this function are simple. The following values are converted to false: false, undefined, null, 0, NaN, and "". All other values are converted to true.

Implicit conversion occurs when using the logical operators (||, &&, and !):

!!3                      // true
0 || "go"                // "go"
"Master" && "Margarita"  // "Margarita"

With the || and && operators, implicit conversion occurs under the hood and eventually returns the original value of one of the operands.

Conclusion

We've discussed the two kinds of data type conversion in JavaScript – implicit and explicit. Both may be used to convert strings, booleans, and numbers into one another. We hope that now you will employ conversion effectively in your code. Just follow the simple rules and be cautious with implicit conversion!

588 learners liked this piece of theory. 32 didn't like it. What about you?
Report a typo