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]); }
<table><tr> <th>Title 1</th> <th>Title 2</th> </tr></table>
.some_class { line-height: 150%; }
document.getElementById("id_button").onclick = function(){ window.open("http://coursesweb.net/"); }
$ar_dir = scandir("dir_name"); var_export($ar_dir);