Skip to content

Instantly share code, notes, and snippets.

@oflow
Last active November 18, 2015 08:21
Show Gist options
  • Save oflow/da3b4b2649dbda1ed9ca to your computer and use it in GitHub Desktop.
Save oflow/da3b4b2649dbda1ed9ca to your computer and use it in GitHub Desktop.
JavaScriptのtypeofよりもう少しだけ細かく判定
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