Skip to content

Instantly share code, notes, and snippets.

@samarpanda
Created December 10, 2012 19:33
Show Gist options
  • Save samarpanda/4252771 to your computer and use it in GitHub Desktop.
Save samarpanda/4252771 to your computer and use it in GitHub Desktop.
Elegant way of checking parameter types in Javascript
function something(param1, param2, param3, param4){
// args contains all the parameters as array
// i.e args[0] == param1, args[1] == param2 and so on
var args = Array.prototype.slice.call(arguments);
}
//To check if a variable is of type Sting
var str = "Samar",
bool = false;
bool = /string/.test(typeof str); //Returns true as str is a type of String.
bool = /string|number|object|function|boolean/.test(typeof str1); //Returns true if str1 is string|number|Object|funciton|boolean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment