Javascript is a loosely typed language. That means a variable x may now hold a number, two lines down a string and another ten lines down an HTML element. What might be confusing is that Javascript often converts types automatically. The rules behind automatic type convertion sometimes are - well, surprisingly abnormal. Did you know that
evaluates to true and why?
Programmers from other languages such as Java often find dynamic typing challenging, and therefore try to twist Javascript to help avoid typing problems like that:
function isString(obj) { return typeof(obj) == 'string'; }
function isNumber(obj) { return typeof(obj) == 'number'; }
function isBoolean(obj) { return typeof(obj) == 'boolean'; }
function isFunction(obj) { return typeof(obj) == 'function'; }
function isObject(obj) { return typeof(obj) == 'object' || isFunction(obj); }
Read more