Back
You can use typeof to get the variable type:
if(typeof bar === 'number') { //whatever}
if(typeof bar === 'number') {
//whatever
}
Also, you can use Object.prototype.toString:
> Object.prototype.toString.call([1,2,3])"[object Array]"> Object.prototype.toString.call("foo bar")"[object String]"> Object.prototype.toString.call(45)"[object Number]"> Object.prototype.toString.call(false)"[object Boolean]"> Object.prototype.toString.call(new String("foo bar"))"[object String]"> Object.prototype.toString.call(null)"[object Null]"> Object.prototype.toString.call(/123/)"[object RegExp]"> Object.prototype.toString.call(undefined)"[object Undefined]"
> Object.prototype.toString.call([1,2,3])
"[object Array]"
> Object.prototype.toString.call("foo bar")
"[object String]"
> Object.prototype.toString.call(45)
"[object Number]"
> Object.prototype.toString.call(false)
"[object Boolean]"
> Object.prototype.toString.call(new String("foo bar"))
> Object.prototype.toString.call(null)
"[object Null]"
> Object.prototype.toString.call(/123/)
"[object RegExp]"
> Object.prototype.toString.call(undefined)
"[object Undefined]"
I hope this will help.
Want to become a Java Expert? Join Java Training now!!
31k questions
32.8k answers
501 comments
693 users