how to check variable type is object in javascript code example
Example 1: javascript check if variable is object
let myObject = {
firstname: 'harry',
lastname: 'potter'
}
console.log(typeof myObject);
if(typeof myObject === 'object') {
console.log('this is object');
}
Example 2: check data type in javascript
typeof("string");
typeof(123);
Example 3: javascript check if variable is object
function isObject(val) {
if (val === null) { return false;}
return ( (typeof val === 'function') || (typeof val === 'object') );
}
var person = {"name":"Boby Snark"};
isObject(person);