Last active
November 18, 2015 08:21
-
-
Save oflow/da3b4b2649dbda1ed9ca to your computer and use it in GitHub Desktop.
JavaScriptのtypeofよりもう少しだけ細かく判定
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function typeOf(val) { | |
'use strict'; | |
return val === null ? 'null' : | |
val === undefined ? 'undefined' : | |
Array.isArray(val) ? 'array' : | |
Number.isNaN(val) ? 'NaN' : typeof val; | |
} | |
console.log(typeOf('')); // string | |
console.log(typeOf(null)); // null | |
console.log(typeOf([])); // array | |
console.log(typeOf(NaN)); // NaN |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment