5

If i need to check a parameter i do this.

if ((typeof param == 'undefined') || (param == null)){
    param = ''; //or param = false;
}

And if it's meant to be a number i might throw in a isNaN check too. I was just wondering if there were any other things i should check for or what you do if you need to check your parameters. I know javascript has a lot of quirks that could affect something like this. What is good practice to check for?

Thanks

9
  • 3
    'undefined' is not the same as undefined, and use === instead of == Commented Mar 9, 2011 at 3:14
  • @Dre See what i mean, i didn't know you could do undefined without the qoutes. I picked up that from some website and didn't know there was another way to do it; or if there is a better way. Commented Mar 9, 2011 at 3:19
  • 1
    Actually @Dre, it IS "undefined" not undefined - please see developer.mozilla.org/en/JavaScript/Reference/Operators/… Commented Mar 9, 2011 at 3:22
  • @arnorhs Oops, I did not notice the typeof there, that totally changes things. Commented Mar 9, 2011 at 3:22
  • 1
    @Dre you're confusing it with if (object === undefined) - which is a whole other ball game... Commented Mar 9, 2011 at 3:23

2 Answers 2

7

Any object evaluates to false in a boolean expression if it is false, undefined, null, NaN, 0, "0", "false", or "" (the empty string).

To check for all of these at once concisely, you can just do it like this:

if(!param)
Sign up to request clarification or add additional context in comments.

3 Comments

well.. he might actually be receiving a boolean value or a zero that he still considers to be a valid parameter
@arnorhs - yea it depends on the scenario if 0 or false is valid or not but this is still very useful. +1 to Peter from me.
@arnorhs As you said in your answer, "it depends on what you want to do". This is a short and useful trick as long as, as you say, 0 or false are not inputs that you will have.
2

I would simply pull a cliché and say that "it depends on what you want to do"..

If you just want to make sure the value is defined and sent to the function, the code you used should be fine.

You can of course also check for elements within the arguments array, like

if (typeof arguments[0] != "string") {
    alert("Has to be string");
}

// or even

if (arguments.length < 1) {
   // there aren't any parameters
}

etc...

the arguments array is very helpful in many ways. You can also use it to overload functions - to provide different functionality or arguments depending on the number of arguments provided, etc..

But other than that, I don't know what you need.

3 Comments

This does help me peice it all together. Sometimes the parameter might not be passed in and if not i just want to assign '' to it or something. There are other scenarios but if i get how to do it for that one it should help me understand the other ones a lot more.
can you name a an example that you want to know how to validate the parameters?
i guess one simple example i can think of is i have a function called updateCell which updates a table cell. It has two parameters the cell id to update and the new contents. I need to check if the cell id is valid and there is a cell with that id - $('#' + cellId).length does that and if the new contents is empty or invalid then set it to a non-breaking space.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.