JSFiddle - React, Tailwind, and code Playground
by texinwien
JavaScript
var identity = function (input) {return input}
var toDo = {
n: null
, o:{}
, i: 1
, s: 'string'
, u: undefined
, b: true
}
Object.keys(toDo)
.forEach(
function(key) {
var iama = doItWithIfs(toDo[key])
console.log('With Ifs:', key, 'is', iama)
iama = doItWithoutIfs(toDo[key])
console.log('Without Ifs:', key, 'is', iama)
}
)
// Do It With Ifs
function doItWithIfs (input) {
var out
var type = typeof input
if (input === null) {
out = 'null'
} else if (type === 'object') {
out = 'an Object'
} else if (type === 'number') {
out = 'a Number'
} else if (type === 'string') {
out = 'a String'
} else if (type === 'undefined') {
out = 'undefined'
} else if (type === 'boolean') {
out = 'a Boolean'
}
// do other stuff with out
return out
}
// Do It Without Ifs
function doItWithoutIfs (input) {
var type = typeof input
var matchType = {
object: function () {return 'an Object'}
, number: function () {return 'a Number'}
, string: function () {return 'a String'}
, undefined: function () {return 'undefined'}
, boolean: function () {return 'a Boolean'}
}
var out = matchType[type]()
// do other stuff with out
return out
}