forked from bittu1040/JavaScript-Coding-and-Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoercion.js
More file actions
21 lines (13 loc) · 742 Bytes
/
coercion.js
File metadata and controls
21 lines (13 loc) · 742 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// coercion
// Coercion is an automatic type conversion that occurs in JavaScript when you want to perform certain operations.
// Type conversion can either be implicit (automatically done during code execution) or explicit (done by you the developer).
const sum = 35 + "hello"
console.log(sum); // Output: "35hello" --> numeric to string coercion
const a = "hello" + 22 + 5
console.log(a) // Output: hello225
// step1: "hello" + 22 = "hello22" (22 gets converted to string)
// step2: "hello22" + 5 = "hello225" (then 5 gets converted to string)
const b = 22 + 5 + "hello"
console.log(b) // Output: 27hello
//step1: 22 + 5 = 27 (no coercion since same datatype)
//step2: 27 + "hello" = "27hello" (27 gets converted to string)