Skip to main content

Type Conversion in JavaScript

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: implicit and explicit.

What is the distinction between explicit and implicit JavaScript type conversion?

View Answer:
Interview Response: Implicit type conversion is a manual conversion of type, and explicit is automatic.

Technical Response: JavaScript translates one data type to another automatically (to the right type). This behavior is known as implicit type conversion or automated type conversion. An explicit type conversion is the sort of conversion that you perform manually.

How is explicit type conversion implemented in JavaScript?

View Answer:
Interview Response: We can implement Explicit type conversions by using built-in methods and objects.

Name one method that automatically converts a value to a string?

View Answer:
Interview Response: The alert method returns a string value; this is automatic type conversion.

What built-in object do we use to explicitly convert values to a string?

View Answer:
Interview Response: The string object, we use it to convert values to a string explicitly.

Technical Response: We use the String(value) object to convert values to strings explicitly. For primitive values, the conversion to string is typically apparent.

Code Example:

let value = true;

alert(typeof value); // boolean
value = String(value); // now value is a string "true"
alert(typeof value); // string

When do numeric conversions happen in JavaScript?

View Answer:
Interview Response: Numeric conversion happens in mathematical functions and expressions automatically.

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';
alert(typeof str); // string
let num = Number(str); // becomes a number 123
alert(typeof num); // number

What is the result of an explicit attempt to convert a non-number to a number?

View Answer:
Interview Response: If the string is not a valid number, the return value is NaN (Not a Number).

Code Example:

let age = Number('an arbitrary string instead of a number');

alert(age); // NaN, conversion failed

If there is an attempt to convert an undefined value. 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);

alert(result); // returns NaN, conversion failed

If there is an attempt to convert a NULL value to a number. What is the return result?

View Answer:
Interview Response: If we attempt a numeric conversion on Null values it returns zero (0).

Code Example:

let thisNumber = null;

let result = Number(thisNumber);

alert(result); // returns 0

What value returns when you attempt a numeric conversion of true and false?

View Answer:
Interview Response: True returns 1, and False returns 0.

Give a brief description of the potential outcomes when converting a string to a number?

View Answer:
Interview Response: We trim whitespace at the beginning and the end of the string. Zero returns if the remaining string contains no characters. Otherwise, the string's number is "read". If an error occurs, it returns NaN.

Code Example:

let myString = '';

let outcome = Number(myString);

alert(outcome); // returns 0

Do Boolean conversions happen implicitly, explicitly, or both?

View Answer:
Interview Response: Both. Logic operations do this automatically, but you can also do it explicitly by using the Boolean() native object.

Are there any objects we can use for explicit Boolean conversions?

View Answer:
Interview Response: The Boolean object converts strings and numerical values to Boolean true or false.

Code Example:

alert(Boolean(1)); // true
alert(Boolean(0)); // false
alert(Boolean('hello')); // true
alert(Boolean('')); // false

What are the two basic Boolean conversion rules in JavaScript?

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, a non-empty string always returns true. Zero is considered empty and returns false.

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.