The function presented below can be used to get the type of a JavaScript variable, and also, to check if a variable is of a certain type.
Here's the function:
function checkType(obj, type) { // coursesweb.net/ // if type not specified (null), returns a string with the object (obj) type if(type == null) return obj.constructor.toString().split(' ')[1].replace(/\(\)/g,'').toLowerCase(); else { //returns true if is it is type, else, false if (obj.constructor.toString().match(new RegExp(type, 'i', 'g'))) return true; else return false; } }
To get the "obj" type, the second argument ("type") no need to be specified.
// function to check /get obj type function checkType(obj, type) { // if type not specified (null), returns a string with the object (obj) type if(type == null) return obj.constructor.toString().split(' ')[1].replace(/\(\)/g,'').toLowerCase(); else { // returns true if is it is type, else, false if (obj.constructor.toString().match(new RegExp(type, 'i', 'g'))) return true; else return false; } } var str = 'a string'; var num = 78.5; var arr = new Array(); var obj = new Object(); var json_array = [7, 8, 'abc']; var json_object = {'ab': 'xyz', 'arr': [1, 2]}; // display in console the type of each variable created above console.log( checkType(str) ); // string console.log( checkType(num) ); // number console.log( checkType(arr) ); // array console.log( checkType(obj) ); // object console.log( checkType(json_array) ); // array console.log( checkType(json_object) ); // object
// function to check /get obj type function checkType(obj, type) { // if type not specified (null), returns a string with the object (obj) type if(type == null) return obj.constructor.toString().split(' ')[1].replace(/\(\)/g,'').toLowerCase(); else { // returns true if is it is type, else, false if (obj.constructor.toString().match(new RegExp(type, 'i', 'g'))) return true; else return false; } } var str = 'a string'; var num = 78.5; var arr = new Array(); var obj = new Object(); var json_array = [7, 8, 'abc']; var json_object = {'ab': 'xyz', 'arr': [1, 2]}; // display console.log with true, or false console.log( checkType(str, 'string') ); // true console.log( checkType(num, 'number') ); // true console.log( checkType(arr, 'array') ); // true console.log( checkType(obj, 'object') ); // true console.log( checkType(json_array, 'array') ); // true console.log( checkType(json_object, 'array') ); // false
// function to check /get obj type function checkType(obj, type) { // coursesweb.net/ // if type not specified (null), returns a string with the object (obj) type if(type == null) return obj.constructor.toString().split(' ')[1].replace(/\(\)/g,'').toLowerCase(); else { // returns true if is it is type, else, false if (obj.constructor.toString().match(new RegExp(type, 'i', 'g'))) return true; else return false; } } var arr = ['coursesweb.net', 'javascript', 'course']; // if arr is Array, writes the first item if(checkType(arr, 'array')) { document.write(arr[0]); }
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net