Explicit Type Conversion
Explicit type conversion occurs when developers manually convert values from one type to another using specific methods or operators.
Numerical Operators (+ and -)
The unary plus (+) and minus (-) operators can convert any value to a number, similar to the Number() function. These are commonly used for explicit type conversion.
let num1 = +'42'; // 42
let num2 = -'3.14'; // -3.14
let num3 = +'hello'; // NaN
let num4 = -true; // -1
parseInt Function
The parseInt() function converts a string to an integer. It parses the string until it encounters a non-digit character, then stops.
let value1 = parseInt('123abc'); // 123
let value2 = parseInt('abc123'); // NaN
let value3 = parseInt('10', 2); // 2 (binary to decimal)
let value4 = parseInt('10', 8); // 8 (octal to decimal)
Wrapper Objects
JavaScript provides wrapper objects that can be used to explicitly convert between types.
String(42); // '42'
String(true); // 'true'
Number('3.14'); // 3.14
Number(false); // 0
Boolean(0); // false
Boolean(''); // false
Boolean('hello'); // true
Implicit Type Conversion
Implicit type conversion happens automatically when JavaScript performs operations between different types.
Number and String Operations
When a number is operated with a string, JavaScript converts the number to a string and performs concatenation rather than mathematical addition.
let result1 = 5 + '2'; // '52' (string concatenation)
let result2 = '5' - 2; // 3 (string converted to number)
let result3 = '5' * 2; // 10 (string converted to number)
let result4 = '5' / 2; // 2.5 (string converted to number)
Number and Boolean Operations
Boolean values are converted to numbers when used in arithmetic operations: true becomes 1 and false becomes 0.
let calc1 = true + 5; // 6
let calc2 = false + 10; // 10
let calc3 = 3 * true; // 3
let calc4 = 4 * false; // 0
undefined and null in Operations
undefined cannot be converted to a number, while null can be converted to 0 in arithmetic operations.
let val1 = null + 5; // 5
let val2 = undefined + 5; // NaN
let val3 = null == 0; // true
let val4 = undefined == 0; // false
Logical NOT (!) Operator
The logical NOT operator converts any value to a boolean, with truthy values becoming false and falsy values becoming true.
let bool1 = !0; // true
let bool2 = !1; // false
let bool3 = !''; // true
let bool4 = !'hello'; // false
let bool5 = !null; // true
let bool6 = !undefined; // true
Equality Operator (==) Conversion
The equality operator performs type coercion when comparing values of different types.
let eq1 = 1 == '1'; // true (string converted to number)
let eq2 = 0 == false; // true (false converted to 0)
let eq3 = '' == false; // true (empty string converted to 0)
let eq4 = null == undefined; // true
Comparison Oeprators
Comparison operators also perform type conversion, typically converting values to numbers before comparison.
let comp1 = '2' > 1; // true (string converted to number)
let comp2 = '01' == 1; // true (string converted to number)
let comp3 = '10' < 2; // false (string converted to number)
Common Interview Questions
Special Comparisons
// Equality comparisons
NaN == NaN; // false
NaN !== NaN; // true
typeof NaN; // 'number'
null instanceof Object; // false
// Array comparisons
[] == ![]; // true
{} == !{}; // false
// String and number operations
1 + '1' == '11'; // true
true + true == 2; // true
4 + [] == '4'; // true
4 + {} == '4[object Object]'; // true
4 + [1] == '41'; // true
4 + [1,2,3] == '41,2,3'; // true
'a' + + 'b' == 'aNaN'; // true
// Boolean comparisons
'true' == true; // false
'true' === true; // false
String Comparisons
When comparing two strings, JavaScript compares them character by character based on their Unicode values rather than converitng to numbers.
'apple' < 'banana'; // true (compares 'a' and 'b')
'100' < '2'; // false (compares '1' and '2')
'20' < '3'; // true (compares '2' and '3')