Type Conversion and Type Coercion in JavaScript
Last Updated :
29 Jan, 2025
Data types in JavaScript are flexible due to which the type of the variables can be changed when the program runs. Type Conversion and Type Coercion are the two ways through which we can change the data type of the variables from one type to the other.
Type Conversion
Type Conversion is the process in JavaScript in which the data type of the variables is converted from one type to another type manually. This is also known as explicit type casting.
- Performed manually by the programmer.
- Uses built-in JavaScript methods like Number(), String(), and Boolean().
- Ensures control over data types in code.
Performing type conversion-
1. String to Number
We can convert a string into numbers using the Number() function, parseInt(), and parseFloat() methods.
JavaScript
let s = "123";
let n = Number(s); // Converts string to number
console.log(n);
2. Number to String
We can convert a number into a string using String() function or we can concate it with an empty string("").
JavaScript
let n = 123;
let s = String(n); //Converts number to string
console.log(s);
3. Boolean to Number
We can convert a boolean into a number using Number(), so true becomes 1 and false becomes 0.
JavaScript
let bool = true;
let n = Number(bool); //Converts boolean to number
console.log(n);
4. Boolean to String
We can convert boolean into string using String() or by contacting it with empty string.
JavaScript
let bool = true;
let s = String(bool); //Converts boolean to string
console.log(s);
For more details read this article - Type Conversion
Type Coercion
Type coercion is the automatic conversion of one data type to another by JavaScript during operations. This is also known as implicit type casting.
- Performed automatically by JavaScript.
- Happens mostly in comparison and arithmetic operations.
- Can lead to unexpected results if not handled properly.
Examples of Type Coercion
1. String + Number
JavaScript will automatically or implicitly convert the number to a string if there is the string present in the arithmetic operation.
JavaScript
let n = 5;
let s = "5";
let res = n + s; // JavaScript converts num to string
console.log(res);
console.log(typeof(res))
2. Boolean + Number
JavaScript converts the boolean value into a number, true becomes 1 and false becomes 0, when we perform the arithematic operations.
JavaScript
let bool = true;
let n = 10;
let res = bool + n; // JavaScript converts boolean to number
console.log(res);
3. Comparison of Different Types
JavaScript convert the two value of data type into the common type when we compare there values.
JavaScript
let s = "10";
let n = 10;
console.log(s == n); // true, JavaScript converts str to number
4. Boolean Context
Javascript converts non-boolean value into the boolean value when the value is in the if statement.
JavaScript
let s = "";
if (s) {
console.log("This won't print"); // Empty string is falsy
} else {
console.log("This will print"); // Empty string is coerced to false
}
For more details read this article - Type Coercion
Difference Between Type Conversion and Type Coercion
Feature | Type Conversion | Type Coercion |
Performed by | Programmer | JavaScript Engine |
Type | Explicit | Implicit |
Control | Full control | Happens automatically |
Examples | Number("5") → 5 | "5" - 2 → 3 |
Best Practices for Handling Type Conversion and Coercion
- Always use explicit type conversion where possible to avoid unexpected results.
- Use === instead of == to prevent unintended type coercion in comparisons.
- Convert inputs explicitly before performing operations.
- Use typeof to check data types before processing values.
Similar Reads
What is Type Coercion in JavaScript? JavaScript is a loosely typed or dynamic language, which means it automatically converts types when necessary, making it easier for developers to work with different data types in expressions, comparisons, and operations. However, this flexibility can sometimes lead to unexpected results if you're n
4 min read
JavaScript Type Conversion In JavaScript Type Conversion can be defined as converting the data type of the variables from one type to the other manually by the programmer(explicitly) or automatically by the JavaScript(implicitly).Implicit Type Conversion (Coercion): Implicit Type Conversion occurs automatically by the JavaScr
4 min read
Explain non-boolean value coercion to a boolean one in JavaScript As we all know javascript is a forgiving language. It does not mind silly mistakes which programmers do. So, sometimes it gives unexpected results. Â So it senses Javascript says "I can do every possibility". Coercion is one of those in which javascript gives weird results because javascript automati
3 min read
Interesting Facts About JavaScript Data Types JavaScript (often abbreviated as JS) is one of the most popular programming languages in the world. It comes with its unique take on data types that sets it apart from languages like C, C++, Java, or Python. Understanding how JavaScript handles data types will be very interesting, which can help you
6 min read
Check if a Variable is of Function Type using JavaScript A function in JavaScript is a set of statements used to perform a specific task. A function can be either a named one or an anonymous one. The set of statements inside a function is executed when the function is invoked or called. javascriptlet gfg = function(){/* A set of statements */};Here, an an
3 min read