0% found this document useful (0 votes)
7 views5 pages

type coercion

The document explains type coercion in JavaScript when performing operations between different data types, detailing how values are automatically converted. It provides examples for various combinations, such as String + Number resulting in concatenation and Number + Boolean where the boolean is coerced to a number. Overall, it illustrates how JavaScript handles operations involving strings, numbers, booleans, objects, and arrays.

Uploaded by

danishrao299
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

type coercion

The document explains type coercion in JavaScript when performing operations between different data types, detailing how values are automatically converted. It provides examples for various combinations, such as String + Number resulting in concatenation and Number + Boolean where the boolean is coerced to a number. Overall, it illustrates how JavaScript handles operations involving strings, numbers, booleans, objects, and arrays.

Uploaded by

danishrao299
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Sure!

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:

let result = "The number is: " + 5;


console.log(result); // "The number is: 5"

2. String + Boolean

When you add a string and a boolean, JavaScript will coerce the boolean to a string and
concatenate them.

Example:

let result = "The answer is: " + true;


console.log(result); // "The answer is: true"

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:

let obj = { name: "Alice" };


let result = "User: " + obj;
console.log(result); // "User: [object Object]"

The object is coerced to the string [object Object].

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:

let arr = [1, 2, 3];


let result = "Array: " + arr;
console.log(result); // "Array: 1,2,3"

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:

let result = 10 + true;


console.log(result); // 11 (because `true` is coerced to `1`)

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:

let obj = { valueOf: () => 5 };


let result = 10 + obj;
console.log(result); // 15 (because `obj.valueOf()` returns `5`)

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:

let arr = [1, 2];


let result = 10 + arr;
console.log(result); // "101,2" (array coerced to string "1,2")

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:

let result = true + false;


console.log(result); // 1 (because `true` is `1` and `false` is `0`)

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:

let obj = { valueOf: () => 3 };


let result = true + obj;
console.log(result); // 4 (because `true` is `1`, `obj.valueOf()` is `3`)

10. Boolean + Array

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:

let arr = [4, 5];


let result = true + arr;
console.log(result); // "14,5" (because `true` is `1`, and array is coerced to
"4,5")

11. Object + Number

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:

let obj = { toString: () => "Object" };


let result = obj + 5;
console.log(result); // "Object5"

12. Object + Boolean

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:

let obj = { toString: () => "Object" };


let result = obj + true;
console.log(result); // "Objecttrue"

13. Object + Object

When you add two objects, JavaScript will convert both objects to strings via their
toString() methods and then concatenate them.
Example:

let obj1 = { toString: () => "Object1" };


let obj2 = { toString: () => "Object2" };
let result = obj1 + obj2;
console.log(result); // "Object1Object2"

14. Object + Array

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:

let obj = { toString: () => "Object" };


let arr = [1, 2];
let result = obj + arr;
console.log(result); // "Object1,2"

15. Array + Number

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:

let arr = [1, 2];


let result = arr + 3;
console.log(result); // "1,23"

Summary of Data Type Coercion:

Data Types Result Example


String + Concatenation (number coerced "The number is: " + 5 → "The number is:
Number to string) 5"
String + Concatenation (boolean coerced "The answer is: " + true → "The answer
Boolean to string) is: true"
String + Object coerced to string and "User: " + { name: "Alice" } → "User:
Object concatenated [object Object]"
String + Array coerced to string and
Array "Array: " + [1, 2, 3] → "Array: 1,2,3"
concatenated
Number + Boolean coerced to number and
Boolean 10 + true → 11
added
Number + Object coerced to primitive and
Object 10 + { valueOf: () => 5 } → 15
added
Data Types Result Example
Number + Array coerced to string and
Array 10 + [1, 2] → "101,2"
concatenated
Boolean + Boolean coerced to number and
Boolean true + false →1
added
Boolean + Boolean coerced to number,
Object true + { valueOf: () => 3 } →4
object to primitive and added
Boolean + Boolean coerced to number,
Array true + [4, 5] → "14,5"
array to string and concatenated
Object + Object coerced to string and { toString: () => "Object" } + 5 →
Number concatenated "Object5"
Object coerced to string and
Object +
boolean to string, then { toString: () => "Object" } + true →
Boolean "Objecttrue"
concatenated
{ toString: () => "Object1" } +
Object + Objects coerced to strings and
Object { toString: () => "Object2" } →
concatenated "Object1Object2"
Object + Object coerced to string, array to { toString: () => "Object" } + [1, 2] →
Array string and concatenated "Object1,2"
Array + Array coerced to string and
Number [1, 2] + 3 → "1,23"
concatenated

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy