Kottans
๐จ
๐ฎ ๐
Values & Operations
string โ "kottan"number โ 0, 1337, NaN, Infinityboolean โ true, falsenull โ nullundefined โ undefinedsymbol โย Symbol("foo")bigint โ 1n, 9007199254740991ntypeof 'Kottans ๐โโฌ' // "string"typeof false // "boolean"typeof 1422 // "number"typeof {} // "object"typeof [] // "object"typeof null // "object"typeof function() {} // "function"
14.22.toString() // "14.22"
// JS Under The Hood
new Number(14.22).toString() // "14.22"
const str = new String('Kottans ๐โโฌ')
const n = new Number(1422)
const hasSomething = new Boolean(false)
if (hasSomething) {
console.log('Bo!๐ป')
}
String(1422) // "1422"
Number('1422') // 1422
Boolean(0) // false
ObjectArrayFunctionRegExpErrorDate
new Object()
new Array(1, 2, 3)
new Function('a, b', 'return a + b')
new RegExp('\w', 'g')
{}
[1, 2, 3]
function(a, b) { return a + b }
/\w/g
Most have a protected constructor: new Array(1, 2, 3) OR Array(1, 2, 3) // [1, 2, 3]
const obj = {}
const arr = []
Object.prototype.toString.call(obj) // "[object Object]"
Object.prototype.toString.call(arr) // "[object Array]"
const kottan = {
[Symbol.toStringTag]: '๐โโฌ'
}
Object.prototype.toString.call(kottan) // "[object ๐โโฌ]"
dotJS 2012 - Brian Leroux - WTFJS
ToNumberToStringToBooleanToPrimitivetruetrue, except for 7 values
falsenullundefined''0, -0NaNToPrimitivetrue/false to 1/0ToPrimitive0NaN
Number(' 14 ') // 14
Number('14px') // NaN
pastInt('14px') // 14
pastFloat('14px') // 14
Symbol.toPrimitivevalueOf and/or toString
const kottan = {
[Symbol.toPrimitive](hint) {
switch (hint) {
cast 'string': return '๐โโฌ'
cast 'number': return '๐โ'
cast 'default': return 'cat'
}
}
}
const kottan = {
valueOf() { return '๐โโฌ' },
toString() { return {} }
}
'Cat: ' + kottan // "Cat: ๐โโฌ"
String(kottan) // "๐โโฌ"
Boolean()!!if, for, do/while? :|| and &&
String()/`${kottan}`object[key]Number()value + value)
valueOf, then toString
===, !== โ without conversions===, == โ compare objects by reference==, !=, >, <, >=, <= โ prefer numbers
undefined == null ๐คฏ
const tricky = NaN
tricky === NaN // false
isNaN(NaN) // true
isNaN('wat') // true
Number.isNaN(NaN) // true
Number.isNaN('wat') // false
Object.is(NaN, NaN) // true
String(symbol)Errortrue