Skip to content

Instantly share code, notes, and snippets.

View amysimmons's full-sized avatar

Amy Simmons amysimmons

View GitHub Profile
var a = "12"
var b = new String("12")

a // "12"
b // String {"12"}

a == b // true (a is compared with b's toPrimitive value)

// b's primitive value is obtained by calling toString()
"22" == true // false
"22" == 1 // false
22 == 1 // false
"2" - 0 // 2
"2" / 1 // 2
"2" * 1 // 2
a = { valueOf: () => 22, toString: () => 44 }

String(a) // "44"
a + "" // "22"
parseInt('22px') // 22
Number('22px') // NaN
// use for getting a specific (non-now) time
x = new Date().getTime() // 1518116090213

// use for getting the current time
y = Date.now() // 1518115963826
document.all // HTMLAllCollection(224) [html, head, meta, …]
Boolean(document.all) // false
// falsy values
Boolean(undefined) // false
Boolean(null) // false
Boolean(NaN) // false
Boolean(-0) // false
Boolean(+0) // false
Boolean("") // false

// everything else is truthy
x = {}
x.valueOf = () => 22
Number(x) // 22

y = []
y.toString = () => '22'
Number(y) // 22

z = {}
Number(null) // 0
Number(undefined) // NaN
Number(true) // 1
Number(false) // 0
Number(22) // 22
Number("hello") // NaN
Number("0") // 0