Skip to main content

Type Conversion - JavaScript Interview Questions

JavaScript Fundamentals: Type Conversion




What are the two types of type conversion in JavaScript?

View Answer:
Interview Response: Explicit and Implicit type conversion.

Technical Response: There are two types of type conversion in JavaScript: implicit (coercion) and explicit (casting). Implicit occurs automatically when the interpreter converts data types, while explicit requires manual intervention using specific methods or functions.

Code Example:

  1. 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'
  1. 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:
Interview Response: Explicit type coercion involves using specific methods, or functions, to convert data types intentionally, while implicit coercion happens automatically, when the interpreter coerces data types without explicit or direct instruction.

How is explicit type conversion implemented in JavaScript?

View Answer:
Interview Response: Explicit type conversion in JavaScript is implemented using built-in functions that convert values of one type to another type. These functions include Number(), String(), Boolean(), and parseInt().

Code Example:

// 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:
Interview Response: The toString() method in JavaScript is commonly used to automatically convert a value to a string representation. This method can be called on various data types such as numbers, booleans, and objects to obtain a string representation of their values.

Code Example:

  1. 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'
  1. 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:
Interview Response: In JavaScript, we can use the built-in String object to explicitly convert values to a string. The String object provides several methods for converting values of different data types to strings.

Code Example:

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:
Interview Response: Numeric conversions occur during arithmetic operations, comparison operations using the equality operator, or when explicitly converting a value using methods, like Number(), parseInt(), or parseFloat().

Code Examples:

Here are a few examples illustrating when numeric conversions happen in JavaScript:

  1. 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
  1. 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
  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:
Interview Response: We can use the Number() object to explicitly convert values to a number.

Code Example:

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:
Interview Response: In JavaScript, the result of an explicit attempt to convert a non-number to a number using the Number() function will return NaN (Not a Number).

Code Example:

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:
Interview Response: Numeric conversions on undefined values returns NaN (Not-a-Number).

Code Example:

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:
Interview Response: In JavaScript, when attempting to convert a null value to a number using the Number() constructor, the result is 0.

Code Example:

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:
Interview Response: In JavaScript, when converting true to a number, the return value is 1. When converting false to a number, the return value is 0.

Code Examples:

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:
Interview Response: In JavaScript, converting a string to a number can result in a valid number, NaN for non-numeric strings, or 0 for empty strings.

Code Example:

let myString = '';

let outcome = Number(myString);

console.log(outcome); // returns 0

Do boolean conversions happen implicitly, explicitly, or both?

View Answer:
Interview Response: Both. Boolean conversions in JavaScript can happen implicitly, such as when using boolean contexts (if, while, ? :), and explicitly, using the Boolean() function or the !! operator.

Code Examples:

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:
Interview Response: Yes, in JavaScript, you can use the Boolean object for explicit Boolean conversions. Additionally, you can use the double NOT operator to achieve the same effect.

Code Example:

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:
Interview Response: Values that are intuitively empty are false. All other values are true.

Technical Response: Conversion Rules

  1. Intuitively empty values, like 0, an empty string, null, undefined, and NaN, become false.
  2. Other values become true.

What is the Boolean return value for the string “0” in JavaScript?

View Answer:
Interview Response: In JavaScript, the Boolean return value for the string "0" is true because it's a non-empty string, making it a truthy value.

Code Example:

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:
Interview Response: String, Number, and Boolean conversions are the most widely used type conversions in JavaScript.

What is type coercion in JavaScript?

View Answer:
Interview Response: Type coercion in JavaScript refers to the automatic or implicit conversion of values from one data type to another during the execution of operations involving different types.

What is the toString() method in JavaScript?

View Answer:
Interview Response: The `toString()` method in JavaScript converts and returns a value as a string. It's commonly used on numbers, booleans, and objects.

What is the valueOf() method in JavaScript?

View Answer:
Interview Response: The valueOf() method is a built-in method in JavaScript that returns the primitive value of an object.

Code Example:

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.