« Back to the index
Core is* examples
All the is* methods are used to test the type of a variable:
isDefined: returns true if the variable is not undefined.
isEmpty: returns true if the variable is empty: empty means undefined, null, empty array or empty string.
isBool: returns true if the variable is a boolean.
isNumber: returns true if the variable is numero, integer or float.
isString: returns true if the variable is a string or a String object.
isArray: returns true if the variable is an array.
isObject: returns true if the variable is an object. It includes String(), Date(), {}. etc. Note than Javasript consider null as an object too.
isFunction: returns true if the variable is a function.
isDate: return true if the variable is a Date object.
isDOM: return true if the variable is a DOM node object.
All the functions return true/false. True means the variable match the specified type by the is* method.
Chart of is* methods testing some variables (See the code after the chart):
here is the code of the full test:
var fctnames = ['isDefined', 'isEmpty', 'isBool', 'isNumber', 'isString',
'isArray', 'isObject', 'isFunction', 'isDate','isDOM'];
var fct = [WA.isDefined, WA.isEmpty, WA.isBool, WA.isNumber, WA.isString,
WA.isArray, WA.isObject, WA.isFunction, WA.isDate,WA.isDOM];
var datanames = ['undefined', 'null','[]','{}','""','true','0','123','123.45','"string"',
'[1,2,3]','{id:1}','fct()','Date()','document.body'
];
var data = [undefined, null, [], {}, '', true, 0, 123, 123.45, 'string',
[1,2,3], {id:1}, function() {}, new Date(), document.body];
document.write('<table border="1" class="chart" style="text-align: center;"><tr><td></td>');
for (var j=0, m = fct.length; j<m; j++)
{
document.write('<td>');
document.write( fctnames[j] );
document.write('</td>');
}
document.write('</tr>');
for (var j=0, m = data.length; j<m; j++)
{
document.write('<tr><td style="text-align: right;">'+datanames[j]+'</td>');
for (var i=0, l = fct.length; i<l; i++)
{
document.write( (fct[i](data[j]) ? '<td style="background-color: #bfb;">Yes</td>' :
'<td style="background-color: #fbb;">No</td>') );
}
document.write('</tr>');
}
document.write('</table>');
« Back to the index