type coercion
type coercion
Let's dive into how type coercion works when performing operations between two
different data types in JavaScript. This will help you understand how JavaScript converts values
automatically when they are used together in operations. I'll provide you with a list of common
data types paired together, explain what happens during coercion, and show examples with
results.
1. String + Number
When you add a string and a number, JavaScript will coerce the number to a string and
concatenate them.
Example:
2. String + Boolean
When you add a string and a boolean, JavaScript will coerce the boolean to a string and
concatenate them.
Example:
3. String + Object
When you add a string and an object, JavaScript will call the object's toString() method and
concatenate the result to the string.
Example:
4. String + Array
When you add a string and an array, JavaScript will convert the array to a string by joining its
elements with commas and then concatenate.
Example:
5. Number + Boolean
When you add a number and a boolean, JavaScript will coerce the boolean to a number (true
becomes 1 and false becomes 0), and then perform the addition.
Example:
6. Number + Object
When you add a number and an object, JavaScript will convert the object to a primitive value
(usually via toString() or valueOf()), and then perform the addition.
Example:
7. Number + Array
When you add a number and an array, JavaScript will coerce the array to a string (by joining
its elements), and then perform the addition.
Example:
8. Boolean + Boolean
When you add two booleans, JavaScript will coerce both booleans to numbers (true becomes
1, false becomes 0), and then perform the addition.
Example:
9. Boolean + Object
When you add a boolean and an object, JavaScript will coerce the boolean to a number (true
becomes 1, false becomes 0), then coerce the object to a primitive value, and perform the
addition.
Example:
When you add a boolean and an array, JavaScript will coerce the boolean to a number and the
array to a string, and then perform the addition.
Example:
When you add an object and a number, JavaScript will coerce the object to a string via its
toString() method, then concatenate the result with the number.
Example:
When you add an object and a boolean, JavaScript will coerce the object to a string and the
boolean to a string, then concatenate them.
Example:
When you add two objects, JavaScript will convert both objects to strings via their
toString() methods and then concatenate them.
Example:
When you add an object and an array, JavaScript will convert the object to a string via its
toString() method, and then convert the array to a string by joining its elements with
commas, then concatenate.
Example:
When you add an array and a number, JavaScript will convert the array to a string (by joining
its elements) and then concatenate the result with the number.
Example:
I hope this clears up how type coercion works when you use different data types together in
JavaScript! If you have more questions or need further examples, feel free to ask.