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
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License