xxxxxxxxxx
// 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'
xxxxxxxxxx
let counter = 120; // counter is a number
console.log(typeof(counter)); // "number"
counter = false; // counter is now a boolean
console.log(typeof(counter)); // "boolean"
counter = "Hi"; // counter is now a string
console.log(typeof(counter)); // "string"Code language: JavaScript (javascript)
xxxxxxxxxx
if (typeof i != "number") {
console.log('This is not number');
}
xxxxxxxxxx
> typeof "foo"
"string"
> typeof true
"boolean"
> typeof 42
"number"
xxxxxxxxxx
> typeof "foo"
"string"
> typeof true
"boolean"
> typeof 42
"number"
xxxxxxxxxx
var x = [1,2,3,4];
console.log(typeof x) // object
console.log(typeof null) // object