Skip to content

Instantly share code, notes, and snippets.

@alldayalone
Last active January 26, 2020 10:39
Show Gist options
  • Save alldayalone/841dd6ec2ac68197db973040e14b20d3 to your computer and use it in GitHub Desktop.
Save alldayalone/841dd6ec2ac68197db973040e14b20d3 to your computer and use it in GitHub Desktop.
JavaScript Types
function typeOf(input) {
return Object
.prototype
.toString
.call(input)
.replace(/\[object (\w+)\]/, '$1')
.toLowerCase();
}
// boolean
console.log(typeOf(true));
// number
console.log(typeOf(42));
// string
console.log(typeOf('Jhon Snow'));
// array
console.log(typeOf([1, 2, 3]));
// object
console.log(typeOf({foo: 42, bar: 'xyz'}));
// function
console.log(typeOf(function() {}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment