how to get type of variable in javascript
var foo = "Hello";
console.log(typeof foo); // string// get type of variable
var number = 1
var string = 'hello world'
var dict = {a: 1, b: 2, c: 3}
console.log(typeof number) // number
console.log(typeof string) // string
console.log(typeof dict) // objectfunction doSomething(x) {
if(typeof(x) === 'string') {
alert('x is a string')
} else if(typeof(x) === 'number') {
alert('x is a number')
}
}> typeof "foo"
"string"
> typeof true
"boolean"
> typeof 42
"number"
if(typeof bar === 'number') {
//whatever
}//typeof() will return the type of value in it's parameters.
//some examples of types: undefined, NaN, number, string, object, array
//example of a practical usage
if (typeof(value) !== "undefined") {//asuming value is already set
//execute code
}> typeof "foo"
"string"
> typeof true
"boolean"
> typeof 42
"number"