Type Conversion - JavaScript Interview Questions
JavaScript Fundamentals: Type Conversion
What are the two types of type conversion in JavaScript?
View Answer:
- Implicit Conversion (Coercion)
In this example, we're adding a number to a string. JavaScript automatically converts the number to a string before performing the concatenation.
let num = 5;
let str = '10';
let result = num + str; // num is implicitly converted to a string.
console.log(result); // Output: '510'
- Explicit Conversion
Here, we're explicitly converting a string to a number using the Number()
function before adding it to another number.
let str = '10';
let num = 5;
let result = Number(str) + num; // str is explicitly converted to a number.
console.log(result); // Output: 15
In another example, we're explicitly converting a boolean to a string using the String()
function.
let bool = true;
let result = String(bool); // bool is explicitly converted to a string.
console.log(result); // Output: 'true'
What is the difference between explicit and implicit type conversion?
View Answer:
How is explicit type conversion implemented in JavaScript?
View Answer:
// Conversion from string to number
let str = "123";
let num = Number(str); // str is explicitly converted to a number.
console.log(num); // Output: 123
console.log(typeof num); // Output: 'number'
// Conversion from number to string
let num2 = 456;
let str2 = String(num2); // num2 is explicitly converted to a string.
console.log(str2); // Output: '456'
console.log(typeof str2); // Output: 'string'
// Conversion from boolean to number
let bool = true;
let numFromBool = Number(bool); // bool is explicitly converted to a number.
console.log(numFromBool); // Output: 1
console.log(typeof numFromBool); // Output: 'number'
Can you name one method that automatically converts a value to a string?
View Answer:
- Number to String Conversion:
let num = 123;
let str = num.toString(); // num is explicitly converted to a string.
console.log(str); // Output: '123'
console.log(typeof str); // Output: 'string'
- Boolean to String Conversion:
let bool = true;
let str = bool.toString(); // bool is explicitly converted to a string.
console.log(str); // Output: 'true'
console.log(typeof str); // Output: 'string'
Keep in mind that not all types in JavaScript have a toString()
method. For instance, null
and undefined
do not have this method, and trying to use toString()
on these values will result in a TypeError.
What built-in object do we use to explicitly convert values to a string?
View Answer:
let value = true;
console.log(typeof value); // boolean
value = String(value); // now value is a string "true"
console.log(typeof value); // string
When do numeric conversions happen in JavaScript?
View Answer:
Here are a few examples illustrating when numeric conversions happen in JavaScript:
- Implicit Numeric Conversion with Mathematical Operators:
let str = '123';
let result = str / 3; // str is implicitly converted to a number.
console.log(result); // Output: 41
- Explicit Numeric Conversion with
Number()
Function:
let bool = true;
let result = Number(bool); // bool is explicitly converted to a number.
console.log(result); // Output: 1
- Explicit Numeric Conversion with
parseInt()
Function:
let str = '350px';
let result = parseInt(str); // str is explicitly converted to a number.
console.log(result); // Output: 350
In all these examples, JavaScript converts the non-number data types to numbers in order to perform the operations or as explicitly instructed by the built-in functions.
What object do we use to convert values to a number explicitly?
View Answer:
let str = '123';
console.log(typeof str); // string
let num = Number(str); // becomes a number 123
console.log(typeof num); // number
What is the result of an explicit attempt to convert a non-number to a number?
View Answer:
let age = Number('an arbitrary string instead of a number');
console.log(age); // NaN, conversion failed
If there is an attempt to convert an undefined value to a number. What is the return result?
View Answer:
let thisNumber;
let result = Number(thisNumber);
console.log(result); // returns NaN, conversion failed
What is the return result when attempting to convert a null value to a number?
View Answer:
let thisNumber = null;
let result = Number(thisNumber);
console.log(result); // returns 0
What value returns when you attempt a numeric conversion of true and false?
View Answer:
let numFromTrue = Number(true); // Explicitly converting true to a number.
console.log(numFromTrue); // Output: 1
let numFromFalse = Number(false); // Explicitly converting false to a number.
console.log(numFromFalse); // Output: 0
This conversion also applies when true
and false
are used in mathematical operations, as JavaScript implicitly converts them to 1
and 0
respectively:
let num = 10 + true; // true is implicitly converted to 1.
console.log(num); // Output: 11
num = 10 + false; // false is implicitly converted to 0.
console.log(num); // Output: 10
Can you give a brief description of the potential outcomes when converting a string to a number?
View Answer:
let myString = '';
let outcome = Number(myString);
console.log(outcome); // returns 0
Do boolean conversions happen implicitly, explicitly, or both?
View Answer:
Implicit Boolean Conversion:
JavaScript implicitly converts values to boolean in logical contexts (like in if
conditions).
let str = 'hello';
if (str) { // str is implicitly converted to boolean.
console.log('The string is not empty.'); // Output: The string is not empty.
}
let num = 0;
if (num) { // num is implicitly converted to boolean.
console.log('This won\'t be printed.'); // This line won't execute as num converts to false.
}
Explicit Boolean Conversion:
You can explicitly convert values to boolean using the Boolean()
function or the !!
operator.
let str = 'hello';
let bool = Boolean(str); // str is explicitly converted to boolean.
console.log(bool); // Output: true
let num = 0;
bool = !!num; // num is explicitly converted to boolean using !! operator.
console.log(bool); // Output: false
Are there any objects we can use for explicit Boolean conversions?
View Answer:
console.log(Boolean(1)); // true
console.log(Boolean(0)); // false
console.log(Boolean('hello')); // true
console.log(Boolean('')); // false
In the context of Boolean conversion, which values evaluate to false, and which values evaluate to true?
View Answer:
- Intuitively empty values, like 0, an empty string, null, undefined, and NaN, become false.
- Other values become true.
What is the Boolean return value for the string “0” in JavaScript?
View Answer:
let numZero = Boolean(0);
let stringZero = Boolean('0');
console.log(numZero); // returns false
console.log(stringZero); // returns true
What are the three widely used type conversions in JavaScript?
View Answer:
What is type coercion in JavaScript?
View Answer:
What is the toString() method in JavaScript?
View Answer:
What is the valueOf() method in JavaScript?
View Answer:
let strObj = new String("Hello");
let strVal = strObj.valueOf();
console.log(strVal); // Output: "Hello"
let numObj = new Number(123);
let numVal = numObj.valueOf();
console.log(numVal); // Output: 123
let boolObj = new Boolean(true);
let boolVal = boolObj.valueOf();
console.log(boolVal); // Output: true
In this example, valueOf()
is used to get the primitive values ("Hello", 123, and true) of the String, Number, and Boolean objects respectively. Note that, when called on a primitive type (not an object), valueOf()
will return the primitive itself.