1
function test(variable, check) {

    var check = check || ???;

    if (variable == check) {
        //do stuff
    }
}

I want this function to check if the variable is anything that if(variable) return true; would return true, any number greater than 0 for example. I'd like to keep this simple formatting and use the == operator for when check is a specific value if possible, so what could I default it to in order to achieve this?

I could achieve the same thing with this code:

function test(variable, check) {

    if(check) {

        //check specific value
        if (variable == check) {
            //do stuff
        }

    } else {

        //check isn't set, so accept any truthy values (like 5 or 'string')
        if(variable) {
            //do stuff
        }
    }
}
Suffick
  • 641
  • 1
  • 6
  • 15
  • @LoïsDiQual but `5 == false` would return false too. – Suffick Feb 15 '14 at 10:12
  • See http://stackoverflow.com/questions/3982663/falsey-values-in-javascript – Lee Taylor Feb 15 '14 at 10:14
  • @MrAdam nope, this will alert(5||false); popup 5 – Mohammed Joraid Feb 15 '14 at 10:17
  • @Joraid Well of course, but if I use `var check = check || false` as suggested then the variable will be checked against `false` if check isn't set, so if the variable is 5 then it's going to return false when my goal is for it to return true. – Suffick Feb 15 '14 at 10:26
  • So, if the input is `false`, `0`, `""`, `undefined` or `null`, `variable == check` should always be considered truthy, right? –  Feb 15 '14 at 10:35
  • @wared The variable will always be something that `if(variable)` would return true, such as numbers > 0, I basically want to achieve the exact same thing as `if(variable)` but with the `==` operator for simplicity's sake, so the function can check if the input is a specific value OR any value that `if(variable)` would return true. I could just repeat the code in an if statement to check if `check` is set or not, but I wanted to see if I could keep it simple. Perhaps it isn't possible without more code. – Suffick Feb 15 '14 at 10:41
  • 1
    @wared There we go. I don't want to return anything, it's just a check. I might end up having to do it the second way (or a variation of the second way), I was just wondering if I could keep it as simple as the first way. – Suffick Feb 15 '14 at 10:57
  • If `check` is set, regardless if it's truthy or falsy, compare with `variable`, otherwise, `variable` must be truthy, right? –  Feb 15 '14 at 10:59
  • Or, if `check` is truthy, compare with `variable`, otherwise, `variable` must be truthy? –  Feb 15 '14 at 11:01
  • @wared `check` will always be truthy if it's set. So if it is set, it must equal `variable`, if `check` isn't set then `variable` must be truthy. Basically what you said except `check` will always be truthy if set. – Suffick Feb 15 '14 at 11:03

2 Answers2

2

After chatting a little, here is the answer :

if (check ? check == variable : variable) {
    // do something
}

That was obvious actually... =D

1

If I understand the question correctly, you would like to basically return if(variable) true, as well as check to see if it equals the second parameter? If so, this should suffice:

function test(variable, check) {
  if(variable == check) return true; //check against the check value
  if(variable) return true; //default check
  return false; //return false otherwise.
}
kennypu
  • 5,950
  • 2
  • 22
  • 28
  • `if(variable)return true;return false;` could be `return !!variable;`. –  Feb 15 '14 at 10:47
  • Even shorter : `return variable == check || !!variable;`. –  Feb 15 '14 at 10:48
  • @wared true, but for simplicity, this should be easier to understand. perhaps you should post that as an alternate answer – kennypu Feb 15 '14 at 10:50
  • 1
    A comment is enough since the code does exactly the same as yours. I agree though, your code is easier to understand. –  Feb 15 '14 at 10:52