1.1 Explicit Type Conversion
Type conversion in JavaScript is the process of converting a value from one data type to another. There are two types of conversion: explicit (manual) and implicit (automatic). Understanding these processes is important to prevent errors and create predictable code.
Explicit conversion, also known as manual conversion, is performed using built-in JavaScript functions and methods. This is the conversion where the programmer explicitly specifies which data type should be converted to another type.
Main methods of explicit conversion:
1. Conversion to string
- Method
String(value)
: converts a value to a string
let num = 123;
let str = String(num);
console.log(typeof str); // "string"
value.toString()
: object method that converts a value to a string
let bool = true;
let str = bool.toString();
console.log(typeof str); // "string"
2. Conversion to number
Number(value)
: converts a value to a number
let str = "456";
let num = Number(str);
console.log(typeof num); // "number"
parseInt(value, base)
: converts a string to an integer. Thebase
parameter indicates the base of the numeral system
let str = "123";
let num = parseInt(str, 10);
console.log(typeof num); // "number"
parseFloat(value)
: converts a string to a floating-point number
let str = "123.45";
let num = parseFloat(str);
console.log(typeof num); // "number"
3. Conversion to boolean
Boolean(value)
: converts a value to boolean
let str = "";
let bool = Boolean(str);
console.log(typeof bool); // "boolean"
1.2 Implicit Type Conversion
Implicit conversion, also known as automatic or hidden conversion, is performed by JavaScript automatically when performing operations with different data types. Implicit conversion can lead to unexpected results, so it needs to be understood and used with caution.
1. Conversion to string
Whenever you add any object to a string, JavaScript will attempt to convert it to a string.
Example:
let result = "The number is " + 123;
console.log(result); // "The number is 123"
2. Conversion to number
Whenever you use any arithmetic operators (except +) with strings containing numbers.
let result = "123" - 0;
console.log(result); // 123 (number)
How this works in practice:
let result = "123" - 10;
console.log(result); // 113 (number)
let result2 = "123" + 10;
console.log(result2); // 12310 (string)
3. Conversion to boolean
When using values in logical contexts (conditional operators, loops, etc.).
let value = "hello";
if (value) {
console.log("Value is truthy"); // Output: "Value is truthy"
}
Examples of conversions in a logical context:
false
,0
,""
(empty string),null
,undefined
, andNaN
are considered false values- All other values are considered true
if ("") {
console.log("This won't be logged.");
} else {
console.log('"" is considered false in a boolean context.');
}
// '"" is considered false in a boolean context.'
1.3 Examples of Implicit Conversion
Example with arithmetic operations:
let width = "100";
let height = "200";
let area = Number(width) * Number(height); // Explicit conversion
console.log(area); // 20000
let perimeter = (+width) + (+height); // Using unary plus for explicit conversion
console.log(perimeter); // 300
Example with logical context:
let userInput = ""; // Empty string
if (!userInput) {
console.log("User did not enter data."); // Implicit conversion of string to boolean
}
Example with combined types:
let value = "10";
let increment = 5;
let result = value + increment; // Implicit conversion of number to string
console.log(result); // "105"
result = Number(value) + increment; // Explicit conversion of string to number
console.log(result); // 15
GO TO FULL VERSION