how to check type of object in javascript code example

Example 1: how to check if object is undefined in javascript

if (typeof something === "undefined") {
    alert("something is undefined");
}

Example 2: how to check type in c++

#include <typeinfo>
#include <iostream>

class someClass { };

int main(int argc, char* argv[]) {
    int a;
    someClass b;
    std::cout<<"a is of type: "<<typeid(a).name()<<std::endl; 
  	// Output 'a is of type int'
    std::cout<<"b is of type: "<<typeid(b).name()<<std::endl; 
  	// Output 'b is of type someClass'
    return 0;
  	// on the online compiler it comes as 'i' for int and 'c' for char
}

Example 3: type of javascript

//typeof() will return the type of value in its parameters.
//some examples of types: undefined, NaN, number, string, object, array

//example of a practical usage
if (typeof(value) !== "undefined") {//also, make sure that the type name is a string
  	//execute code
}

Example 4: check data type in javascript

typeof("string"); //string
typeof(123); //number

Example 5: javascript check if variable is object

//checks if is object, null val returns false
function isObject(val) {
    if (val === null) { return false;}
    return ( (typeof val === 'function') || (typeof val === 'object') );
}
var person = {"name":"Boby Snark"};
isObject(person);//true

Example 6: check data type in js

typeof("iAmAString");//This should return 'string'
//NO camelCase, as it is a JS Keyword

Tags:

Java Example