Created
December 10, 2012 19:33
-
-
Save samarpanda/4252771 to your computer and use it in GitHub Desktop.
Elegant way of checking parameter types in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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