JavaScript has only a few functions comparing with other programming languages, but gives you the freedom to easily create and use your own functions, that can be equivalent for functions from other programming languages.
In PHP there are many functions to work with data type, to check and test data type. These functions beggins with is_.
In this tutorial are presented these PHP functions, created to be used in JavaScript:
          is_string()           is_numeric()           is_bool()
          is_object()           is_int()                   is_null()
          is_array()            is_float()

- To use these functions, copy them in your code, then can be used like in PHP. If you don't know PHP, see and test the examples presented to each function.

is_string

is_string(obj) - Find whether the type given variable is string. Returns TRUE if obj is of type string, FALSE otherwise.
Code:
function is_string(obj) {
  return (typeof(obj) === 'string');
}
Example:
<script type="text/javascript"><!--
// is_string function - coursesweb.net/javascript/
function is_string(obj) {
  return (typeof(obj) === 'string');
}

var str = 'JavaScript Course: https://coursesweb.net/javascript/';
var arr = [7, 8, 'abc'];

// test str,  displays:  "str variable is a string"
if(is_string(str)) alert('str variable is a string');
else alert('str variable not string');

// test arr,  displays:  "arr variable not string"
if(is_string(arr)) alert('arr variable is a string');
else alert('arr variable not string');
--></script>

is_numeric

is_numeric(obj) - Finds whether the given variable is numeric. Returns TRUE if obj is a number or a numeric string, FALSE otherwise.
Code:

function is_numeric(obj) {
  return /^[0-9]+[\.,]{0,1}[0-9]*$/i.test(obj);
}
Example:
<script type="text/javascript"><!--
// is_numeric function - coursesweb.net/javascript/
function is_numeric(obj) {
  return /^[0-9]+[\.,]{0,1}[0-9]*$/i.test(obj);
}

var num = 88;
var num2 = '7.8';        // number in a string
var str = 'JavaScript Course: https://coursesweb.net/javascript/';

// test num,  displays:  "num variable is a number"
if(is_numeric(num)) alert('num variable is a number');
else alert('num variable not a number');

// check num2,  displays:  "num2 variable contains a number"
if(is_numeric(num2)) alert('num2 variable contains a number');
else alert('num2 variable not a number');

// test str,  displays:  "str variable not a number"
if(is_numeric(str)) alert('str variable is a number');
else alert('str variable not a number');
--></script>

is_int

is_int(obj) - Finds whether the type of the given variable is integer. Returns TRUE if obj is an integer, FALSE otherwise.
Code:
function is_int(n) {
  return typeof(n)==="number" && Math.round(n) == n;
}
- is_int() doesn't always work if you are validating form input. This is because form inputs are strings, even if the user typed a number, in this case use is_numeric().
Example:
<script type="text/javascript"><!--
// is_int function - coursesweb.net/javascript/
function is_int(n) {
  return typeof(n)==="number" && Math.round(n) == n;
}

var num = 12;
var num2 = 7.8;
var str = '123';

// test num,  displays:  "num variable is integer"
if(is_int(num)) alert('num variable is integer');
else alert('num variable not integer');

// check num2,  displays:  "num2 variable not integer"
if(is_int(num2)) alert('num2 variable is integer');
else alert('num2 variable not integer');

// test str,  displays:  "str variable not integer"
if(is_int(str)) alert('str variable is integer');
else alert('str variable not integer');
--></script>

is_float

is_float(obj) - Finds whether the type of the given variable is float. Returns TRUE if obj is a float, FALSE otherwise.
Code:
function is_float(n) {
  return n===+n && n!==(n|0);
}
- is_float() doesn't always work if you are validating form input. This is because form inputs are strings, even if the user typed a number, in this case use is_numeric().
Example:
<script type="text/javascript"><!--
// is_float function - coursesweb.net/javascript/
function is_float(n) {
  return n===+n && n!==(n|0);
}

var num = 12;
var num2 = 7.8;
var str = '12.3';

// test num,  displays:  "num variable not a float"
if(is_float(num)) alert('num variable is a float');
else alert('num variable not a float');

// check num2,  displays:  "num2 variable is a float"
if(is_float(num2)) alert('num2 variable is a float');
else alert('num2 variable not a float');

// test str,  displays:  "str variable not a float"
if(is_float(str)) alert('str variable is a float');
else alert('str variable not a float');
--></script>

is_array

is_array(obj) - Finds whether the given variable is an array. Returns TRUE if obj is an array, FALSE otherwise.
Code:
function is_array(obj) {
  if (obj.constructor.toString().match(/array/ig)) return true;
  else return false;
}
Example:
<script type="text/javascript"><!--
// is_array function - coursesweb.net/javascript/
function is_array(obj) {
  if (obj.constructor.toString().match(/array/ig)) return true;
  else return false;
}

var arr = [12, 7.8, 'jsphp'];
var str = 'JavaScript Course: https://coursesweb.net/javascript/';

// test arr,  displays:  "arr variable is an array"
if(is_array(arr)) alert('arr variable is an array');
else alert('arr variable not an array');

// check str,  displays:  "str variable not an array"
if(is_array(str)) alert('str variable is an array');
else alert('str variable not an array');
--></script>

is_object

is_object(obj) - Finds whether the given variable is an object. Returns TRUE if obj is an object, FALSE otherwise.
Code:
function is_object(obj) {
  if (obj.constructor.toString().match(/object/ig)) return true;
  else return false;
}
Example:
<script type="text/javascript"><!--
// is_object function - coursesweb.net/javascript/
function is_object(obj) {
  if (obj.constructor.toString().match(/object/ig)) return true;
  else return false;
}

var object1 = new Object();
var json_object = {'ab': 'xyz', 'arr': [1, 2]};
var str = 'JavaScript Course: https://coursesweb.net/javascript/';

// test object1,  displays:  "object1 variable is an object"
if(is_object(object1)) alert('object1 variable is an object');
else alert('object1 variable not an object');

// check json_object,  displays:  "json_object variable is an object"
if(is_object(object1)) alert('json_object variable is an object');
else alert('json_object variable not an object');

// test str,  displays:  "str variable not an object"
if(is_object(str)) alert('str variable is an object');
else alert('str variable not an object');
--></script>

is_bool

is_bool(obj) - Finds whether the given variable is a boolean (true, or false). Returns TRUE if obj is a boolean, FALSE otherwise.
Code:
function is_bool(obj) {
  return (obj === true || obj === false);
}
Example:
<script type="text/javascript"><!--
// is_bool function - coursesweb.net/javascript/
function is_bool(obj) {
  return (obj === true || obj === false);
}

var vr1 = true;
var vr2 = null;
var str = 'false';

// test vr1,  displays:  "vr1 variable is a boolean"
if(is_bool(vr1)) alert('vr1 variable is a boolean');
else alert('vr1 variable not a boolean');

// test vr2,  displays:  "vr2 variable not a boolean"
if(is_bool(vr2)) alert('vr2 variable is a boolean');
else alert('vr2 variable not a boolean');

// test str,  displays:  "str variable not a boolean"
if(is_bool(str)) alert('str variable is a boolean');
else alert('str variable not a boolean');
--></script>

is_null

is_null(obj) - Finds whether the given variable is NULL. Returns TRUE if obj is null, FALSE otherwise.
Code:
function is_null(obj) {
  return obj === null;
}
Example:
<script type="text/javascript"><!--
// is_null function - coursesweb.net/javascript/
function is_null(obj) {
  return obj === null;
}

var vr1 = null;
var vr2 = false;
var str = 'null';

// test vr1,  displays:  "vr1 is null"
if(is_null(vr1)) alert('vr1 is null');
else alert('vr1 not null');

// check vr2,  displays:  "vr2 not null"
if(is_null(vr2)) alert('vr2 is null');
else alert('vr2 not null');

// check str,  displays:  "str not null"
if(is_null(str)) alert('str is null');
else alert('str not null');
--></script>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Click on the HTML tag which creates an horizontal line in web page.
<br /> <em> <hr />
Some content ...
<hr />
Content under line ...
Which CSS property defines the text color?
font-style font-variant color
h2 {
  color: #cbdafb;
}
Click on the function which searches if a character, or text exists in a string.
indexOf() toString() split()
var str = "Web courses - http://CoursesWeb.net/";
if(str.indexOf("http://") == -1) alert("http:// isn`t in string");
else alert("http:// is in string");
Which function splits a string into an array of strings based on a separator?
array_merge() explode() implode()
$str = "apple,banana,melon,pear";
$arr = explode(",", $str);
var_export($arr);      // array (0=>"apple", 1=>"banana", 2=>"melon", 3=>"pear")
JavaScript PHP is_ functions to test data type

Last accessed pages

  1. PHP MySQL - WHERE and LIKE (18942)
  2. Add Pause in JavaScript script (9986)
  3. PHP Unzipper - Extract Zip, Rar Archives (2141)
  4. Working with HTML attributes in PHP (6209)
  5. Define Custom List-item Markers, Bullets for UL, OL Lists (3681)

Popular pages this month

  1. Insert, Select and Update NULL value in MySQL (515)
  2. Read Excel file data in PHP - PhpExcelReader (430)
  3. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (319)
  4. PHP getElementById and getElementsByTagName (255)
  5. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (234)