Unique Terms
What’s Coercion?
Coercion is the term that is
used for unexpected type casting in JavaScript. Often when you are working with
data you will need to transform it from one data type to another — something
that every developer does on a daily basis. Coercion refers to those not-obvious
type casts that happen as a side-effect of different operations.
Strings and Numbers
42 + "0" //
"420"
14 + "" //
"14"
"42" - 7 // 35
"42" - 0 // 42
"42" -
"9" // 33
["alex",
"sam"] + ["jon", "mary"] //
"alex,samjon,mary"
[3] - [1] // 2
["alex",
"sam"] - ["jon", "mary"] // NaN
Booleans
const a = 100
const b = "test"
const c = null
a || b // 100
a && b //
"test"
a || c // 100
a && c // null
b || c // "test"
b && c // null
Equality
42 == "42" // true
42 === "42" //
false
https://hackernoon.com/understanding-js-coercion-ff5684475bfc
Comments
Post a Comment