Javascript Implicit Type Conversions

What are implicit type conversions? #

Implicit type conversions refer to the following

A type conversion that is not explicitly done during the processing of a process. Implicit type conversions are performed during the processing of arithmetic operations and functions. In this section, we will focus on implicit type conversions in operators.

Implicit Type Conversions in Equivalence Operators The most famous implicit type conversion is the equivalence operator (==), which was mentioned earlier. The equivalence operator performs an implicit type conversion so that the operands are of the same type, and then compares them.

Comparison with the equivalence operator (==) produces surprising results, as shown below.

// Implicit type conversion is performed when they are different types
console.log(1 == "1"); // => true
console.log(0 == false); // => true
console.log(10 == ["10"]); // => true

However, there is an easy way to avoid the implicit type conversion of equivalence operators.

It is to always use the strict equivalence operator (===). By always using the strict equivalence operator when comparing values, you can compare values without implicit type conversion.

console.log(1 === "1"); // => false
console.log(0 === false); // => false
console.log(10 === ["10"]); // => false

By using the strict equivalence operator (===), you can avoid unintended comparison results. For this reason, it is recommended to use the strict equivalence operator (===) instead of the equivalence operator (==) for comparisons.

Various Implicit Type Conversions #

Let's take a look at some concrete examples of other operators.

In the following code, the number 1 and the string "2" are handled by the plus operator. The plus operator (+) is multiply defined so that it can perform both addition of numbers and concatenation of strings. In this case, JavaScript has a specification that gives priority to string concatenation. Therefore, the numeric value of 1 is implicitly converted to the string value of "1", and then the string is concatenated.

1 + "2"; // => "12"
// During the arithmetic process, an implicit type conversion is performed as follows
"1" + "2"; // => "12"

Let's take another look at implicit type conversion between numbers and strings. The following code subtracts the string "2" from the number "1".

In JavaScript, there is no definition of the minus operator (-) for strings. Therefore, an implicit type conversion is performed to the numeric value that is the target of the minus operator. This implicitly converts the string "2" to a numeric value of 2 and then subtracts it.

1 - "2"; // => -1
// During the arithmetic process, an implicit type conversion is performed as follows
1 - 2; // => -1

Up to two values, we can still predict the type of the result. However, when dealing with three or more values, it becomes difficult to predict the result.

In the following example, the + operator is used to operate on three or more values, and if the types of the values are mixed up, the result will be different depending on the order of the operations.

const x = 1, y = "2", z = 3;
console.log(x + y + z); // => "123"
console.log(y + x + z); // => "213"
console.log(x + z + y); // => "42"

In this way, the type of the operand is automatically converted during the processing process, which is called implicit type conversion.

In implicit type conversion, the type of the result value depends on the type of the operand. To avoid this, it is necessary to perform non-implicit conversions - that is, explicit type conversions.

Explicit Type Conversions #

We will see how to do an explicit type conversion to a primitive type.

Arbitrary values → boolean values In JavaScript, the Boolean constructor function can be used to convert arbitrary values to true or false boolean values.

Boolean("string"); // => true
Boolean(1); // => true
Boolean({}); // => true
Boolean(0); // => false
Boolean(""); // => false
Boolean(null); // => false

In JavaScript, the following rules determine which values are true and which values are false.

Values that are falsy will be false Values that are not falsy will be true A falsy value is one of the following seven types of values

false
undefined
null
0n
0n
NaN
"" (empty string)

This conversion rule is similar to the evaluation of conditional expressions in if statements. When a value other than true or false is passed to an if statement, it is implicitly converted to a true or false value before being judged, as follows

/ x is undefined
let x; 
if (!x) {
    console.log("Show if value is falsy", x);
}

Since there are few implicit type conversion rules for boolean values, they are often handled without explicitly converting them. However, to make more accurate judgments and obtain boolean values, use the exact equivalence operator (===) for comparison, as follows

// x is undefined
let x;
if (x === undefined) {
    console.log("Show if x is undefined", x);
}

Numbers → Strings #

To explicitly convert a number to a string, use the String constructor function.

String(1); // => "1"

The String constructor function can convert a variety of values other than numbers to strings.

String("str"); // => "str"
String(true); // => "true"
String(null); // => "null"
String(undefined); // => "undefined"
String(Symbol("Symbol description")); // => "Symbol(Symbol description)"
// For values that are not primitive types
String([1, 2, 3]); // => "1,2,3"
String({ key: "value" }); // => "[object Object]"
String(function() {}); // "function() {}"

As you can see from the above results, explicit conversion with the String constructor function is not a universal method. For values of boolean, numeric, string, undefined, null, and symbol primitive types, conversion can yield strings that look exactly as they appear.

For objects, on the other hand, it does not return a very meaningful string. On the other hand, it does not return a meaningful string for objects, because there are more appropriate methods for objects than the String constructor function. For arrays there is the join method, for objects there is the JSON.stringify method, and so on. Therefore, conversion with the String constructor function should only be done for primitive types.

Tags:

Javascript