Coercion
Coerce is transforming a string into a number. Converting between types.**
ie.
var a = "45"; var b = Number(a); // coerce it, don't leave it console or JS to figure it out var amount = "$" + String(b); // coerce it, don't leave it console or JS to figure it out if(a == b) { // loose coerce } console.log(b);
Equality
== Allows coerce (checks value and type)
=== Does not
Arrays and Objects as coerced to strings for comparison ie.
var a = [1,2,3]; var b = [1,2,3]; var c = "1,2,3"; a == c; // true (a value is coerced) b == c; // true (b value is coerced) a == b; // false (a value is coerced)
Truthy & Falsy
Pretty easy. Javascript will automatically coerce from non-boolean value to boolean (Truthy, Falsy)
0 or "" would coerce to falsy. FALSE
1 or "Free" would coerce to truthy. TRUE
Falsy
- "" (empty string)
- 0, -0, NaN
- Null
- False
Truthy
- "hello"
- 42
- true
- arrays
- objects
- functions
page revision: 7, last edited: 08 Jan 2018 12:06