Type conversion, also known as type coercion, is the process in JavaScript where a value of one data type is automatically or explicitly converted to another data type. JavaScript is a dynamically typed language, which means that variables can hold values of different types, and type conversion is a common practice to ensure that values are in the expected format for various operations.
There are two types of type conversion in JavaScript:
Implicit Type Conversion (Coercion): This occurs automatically by the JavaScript engine when values of different types are used together in an operation. JavaScript tries to convert one or both of the values to a common data type before performing the operation.
Example 1: Implicit conversion of a number to a string.
let number = 42;
let string = "The answer is: " + number; // The number is implicitly converted to a string.
Example 2: Implicit conversion of a string to a number (if possible).
let strNum = "42";
let num = strNum * 1; // The string "42" is implicitly converted to a number.
Explicit Type Conversion: This type of conversion is done explicitly in your code using functions like
Number(), String(), or Boolean() to change a value from one type to another.
Example 1: Explicit conversion from a string to a number.
let str = "123";
let num = Number(str); // Explicitly converts the string "123" to a number.
Example 2: Explicit conversion from a number to a string.
let number = 42;
let str = String(number); // Explicitly converts the number 42 to a string.
Example 3: Explicit conversion from a value to a boolean.
let value = "Hello";
let bool = Boolean(value); // Explicitly converts the value to a boolean (true, since it's not a falsy value).
Type conversion is a fundamental aspect of JavaScript, and understanding how it works is crucial for writing code that behaves as expected. Implicit type conversion can sometimes lead to unexpected results, so it's important to be aware of the rules governing such conversions and use explicit type conversion when needed to ensure your code's correctness.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Type conversion, also known as type coercion, is the process in JavaScript where a value of one data type is automatically or explicitly converted to another data type. JavaScript is a dynamically typed language, which means that variables can hold values of different types, and type conversion is a common practice to ensure that values are in the expected format for various operations.
There are two types of type conversion in JavaScript:
Implicit Type Conversion (Coercion): This occurs automatically by the JavaScript engine when values of different types are used together in an operation. JavaScript tries to convert one or both of the values to a common data type before performing the operation.
Example 1: Implicit conversion of a number to a string.
Example 2: Implicit conversion of a string to a number (if possible).
Explicit Type Conversion: This type of conversion is done explicitly in your code using functions like Number(), String(), or Boolean() to change a value from one type to another.
Example 1: Explicit conversion from a string to a number.
Example 2: Explicit conversion from a number to a string.
Example 3: Explicit conversion from a value to a boolean.
Type conversion is a fundamental aspect of JavaScript, and understanding how it works is crucial for writing code that behaves as expected. Implicit type conversion can sometimes lead to unexpected results, so it's important to be aware of the rules governing such conversions and use explicit type conversion when needed to ensure your code's correctness.