2

Assuming I have this :

var a = parseInt(prompt("Enter A"));
var b = parseInt(prompt("Enter B"));
var c = parseInt(prompt("Enter C"));

What is the shortest way to check if a value exists in all 3 variables ?

Doing if(a && b && c) is not accurate cause 0 is falsy.

p.s. a,b,c can be ints or strings...Also , There must be a trick here...

for the string example :

var a = (prompt("Enter A"));
var b = (prompt("Enter B"));
var c = (prompt("Enter C"));
Royi Namir
  • 144,742
  • 138
  • 468
  • 792

6 Answers6

2

The only way to categorically confirm whether all of these values is defined, regardless of type, is this:

if( (typeof a !== 'undefined') || 
    (typeof b !== 'undefined') || 
    (typeof c !== 'undefined')) { /* ... */ }

Unfortunately, shortcuts that rely on truthiness of values and, as you point out, this is not always an acceptable approach.

As Alex K shows, you can do some tricky stuff if you know the type of the variable beforehand, but knowing the type beforehand may require as much code as you avoid in the comparison.

Edit 1

Dave Newton pointed out that prompt always returns a defined value; it returns null if user's press cancel.

Thus, a better approach would be:

function isValid(value) {
    return typeof value !== 'undefined' && value !== null);
}

if( isValid(a) && isValid(b) && isValid(c) ){ /* ... */ }

Edit 2

Of course, the simplest thing to do is to check for validity before parsing the ints. Since

!!""  === false
!!"0" === true
!!0   === false

You can use (a && b & c) on the string/null that prompt returns and only if they are valid, do your parsing.

Dancrumb
  • 26,597
  • 10
  • 74
  • 130
1

How about this:

ok = !isNaN(a+b+c);
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • Alas, OP just edited with a completely different second case. – apsillers Jan 04 '13 at 15:33
  • @apsillers I edited now, but wrote it when first publishing.(a,b,c can be also strings). the _edit_ was written now. – Royi Namir Jan 04 '13 at 15:34
  • @RoyiNamir But saying "they can also be strings" didn't make any sense given the code. The code is always the defining artifact, since it's what runs. – Dave Newton Jan 04 '13 at 15:38
0

Did you try (!!a && !!b && !!c)?

AFAIK, unless the value of a string variable is empty, null or undefined it should evaluate to true. 0 would also return false but I'm writing on behalf of your prompt() call since its value would not be of type Number.

inhan
  • 7,394
  • 2
  • 24
  • 35
0

Always use:

if (typeof( variableName ) != "undefined") {
    //Do something
}

Since Javascript uses Json object notation the following code is completely valid:

var x = {};

if (x.test == undefined) {
    //Do something
}

But if "x" does not exists this produces an error. There is no way to do this safer in Javascript.

Dillen Meijboom
  • 963
  • 7
  • 13
0

The shortest way to check if a set of variables has valid, acceptable values is to create a helper function according to your specification which accepts a variable number of arguments. In this example, this function will return false if an argument is undefined, null, or a NaN number. Strings will return true for isNaN, so we check if the argument's type is a number before checking if it is NaN.

function isValid() {
    var i, args = Array.prototype.slice.call(arguments, 0);
    for(i = 0; i < args.length; i += 1) {
        if( typeof args[i] === 'undefined' || args[i] === null
        || (typeof args[i] === 'number' && isNaN(args[i]))
        ) {
            return false;
        }
    }
    return true;
}

Utilize it like so:

if( isValid(a, b, c) ) {
    ...
}
Ryan Stein
  • 7,930
  • 3
  • 24
  • 38
0

If the values can only be strings or numbers, you can use:

var valid = ('' + a) && ('' + b) && ('' + c); //always convert into string

Here's also a fancy but relatively short one, which deals with undefined, null and empty string:

var valid = [a].join() && [b].join() && [c].join();

In Array.prototype.join(), when an element is undefined or null it will be converted into an empty string.

But it's neither elegant nor efficient. :(

Justineo
  • 770
  • 6
  • 12