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's some examples.

Get variable type

- To get the "obj" type, the second argument ("type") no need to be specified.
<script type="text/javascript"><!--
// 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 alert with the type of each variable created above
alert( checkType(str) );               // string
alert( checkType(num) );               // number
alert( checkType(arr) );               // array
alert( checkType(obj) );               // object
alert( checkType(json_array) );        // array
alert( checkType(json_object) );       // object
--></script>

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

Check variable type

<script type="text/javascript"><!--
// 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 alert with true, or false
alert( checkType(str, 'string') );               // true
alert( checkType(num, 'number') );               // true
alert( checkType(arr, 'array') );                // true
alert( checkType(obj, 'object') );               // true
alert( checkType(json_array, 'array') );         // true
alert( checkType(json_object, 'array') );        // false
--></script>

Another example, if a variable, "arr" is Array, alert its first element.
<script type="text/javascript"><!--
// 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, alert its first element
if(checkType(arr, 'array')) {
  alert(arr[0]);
}
--></script>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add definition lists into a <dl> element?
<dt> <dd> <li>
<dl>
 <dt>HTML</dt>
  <dd> - Hyper Text Markup Language</dd>
  <dd> - Language for web pages</dd>
</dl>
Which CSS property can hide an element on page, letting an empty space in its place?
display position visibility
#id {
  visibility: hidden;
}
Click on the event which is triggered when the mouse clicks on an object.
onclick onmouseover onfocus
document.getElementById("id").onclick = function(){
  alert("http://CoursesWeb.net/");
}
Indicate the PHP variable that contains the contents of both $_GET, $_POST, and $_COOKIE arrays.
$_SESSION $_GET $_REQUEST
if(isset($_REQUEST["id"])) {
  echo $_REQUEST["id"];
}
Get and Check variable type in JavaScript

Last accessed pages

  1. Select in MySQL, Output results in HTML Table (3789)
  2. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (8371)
  3. jQuery Drag and Drop Rows between two similar Tables (6875)
  4. Add Pause in JavaScript script (9348)
  5. Read Excel file data in PHP - PhpExcelReader (56706)

Popular pages this month

  1. Read Excel file data in PHP - PhpExcelReader (443)
  2. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (351)
  3. Register and show online users and visitors (319)
  4. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (300)
  5. Insert, Select and Update NULL value in MySQL (214)