Q:

javascript how to know type of variable

var foo = "Hello";
console.log(typeof foo); // string
5
> typeof "foo"
"string"
> typeof true
"boolean"
> typeof 42
"number"

if(typeof bar === 'number') {
   //whatever
}
3
// You can use the built-in method 'typeof' in order to check the variable datatype

//examples:

typeof "hello" // "string"

//or
var a = 1;
typeof(a);
//the output will be > 'number'
0
> typeof "foo"
"string"
> typeof true
"boolean"
> typeof 42
"number"
0

New to Communities?

Join the community