Javascript Course


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;
 }
}

The function has two parameters: "obj", for the variable (or object) you want to check, and "type", which is a string with the name of the type to test.
If the type of "obj" is the name specified in the "type" parameter, the function returns True, otherwise, returns False.
If no argument specified for "type", the function returns a string with the type of "obj".

Here are some examples.

Get variable type

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
- The function returns the type of 'obj' in lowercase string, so, to check the type of a variable, just add for the "type" argument a string with the name of type you want to check.

Check variable type

// 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
Another example, if a variable, "arr" is Array, it writes in page the first item.
// 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]);
}

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Get and Check variable type in JavaScript

Last accessed pages

  1. PHP Simple HTML DOM Parser (12452)
  2. Disable button and Enable it after specified time (17532)
  3. Get Mime Type of file or string content in PHP (6229)
  4. PHP MySQL - using MySQLi (9669)
  5. Integer and Float value in Select with PDO from string to numeric (8658)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (477)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (71)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (63)
  5. CSS3 2D transforms (46)