Skip to content

Instantly share code, notes, and snippets.

@jasdeepkhalsa
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasdeepkhalsa/be6c165495bea87468a2 to your computer and use it in GitHub Desktop.
Save jasdeepkhalsa/be6c165495bea87468a2 to your computer and use it in GitHub Desktop.
Testing Different Ways to Use If Statements to Check Multiple Possible Values of a Variable

From StackOverflow: http://stackoverflow.com/a/11836108/1365289

if (extension != "jpg" && 
    extension != "gif" && 
    extension != "bmp" && 
    extension != "png" && 
    extension != "whatever else") {
    // This will execute when the extension is NOT one of the expected 
    // extensions.
}

Furthermore, you could handle it a little more succinctly with a regular expression:

if (!/jpg|gif|bmp|png|whatever/.test(extension)) {
    // This will execute when the extension is NOT one of the expected 
    // extensions.
}

###Addendum:

The examples above execute the body of the if-statement when the value of extension is not one of the supported values.

If you wanted to execute the body of the if-statement when the value of extensions is one of the supported values, you would change the logic from not equal/and to equal/or, like so:

if (extension == "jpg" || 
    extension == "gif" || 
    extension == "bmp" || 
    extension == "png" || 
    extension == "whatever else") {
    // This will execute when the extension is one of the expected extensions.
}

And again, it'd be more concise using a regular expression:

// I just removed the leading ! from the test case.
if (/jpg|gif|bmp|png|whatever/.test(extension)) {
    // This will execute when the extension is one of the expected extensions.
}

From StackOverflow: http://stackoverflow.com/a/1930504/1365289

The && operator "short-circuits" - that is, if the left condition is false, it doesn't bother evaluating the right one.

Similarly, the || operator short-circuits if the left condition is true.

/**
* The winners are:
* testWithOrAndRepeatedVariables - Winner for testing if a string IS a particular value
* testWithoutAndAndRepeatedVariables - Winner for testing if a string IS NOT a particular value
*/
function testWithOrAndOneVariable(str)
{
if(str === 'bar' || 'quz' || 'dog')
{
return true;
}
else
{
return false;
}
}
// Winner for testing if a string IS a particular value
function testWithOrAndRepeatedVariables(str)
{
if(str === 'bar' || str === 'quz' || str === 'dog')
{
return true;
}
else
{
return false;
}
}
function testWithAndAndOneVariable(str)
{
if(str === 'bar' && 'quz' && 'dog')
{
return true;
}
else
{
return false;
}
}
function testWithAndAndRepeatedVariables(str)
{
if(str === 'bar' && str === 'quz' && str === 'dog')
{
return true;
}
else
{
return false;
}
}
function testWithoutOrAndOneVariable(str)
{
if(str !== 'bar' || 'quz' || 'dog')
{
return true;
}
else
{
return false;
}
}
function testWithoutOrAndRepeatedVariables(str)
{
if(str !== 'bar' || str !== 'quz' || str !== 'dog')
{
return true;
}
else
{
return false;
}
}
function testWithoutAndAndOneVariable(str)
{
if(str !== 'bar' && 'quz' && 'dog')
{
return true;
}
else
{
return false;
}
}
// Winner for testing if a string IS NOT a particular value
function testWithoutAndAndRepeatedVariables(str)
{
if(str !== 'bar' && str !== 'quz' && str !== 'dog')
{
return true;
}
else
{
return false;
}
}
var output = '';
function test(expected, actual, name)
{
var name = name || test.caller.name;
if(expected === actual)
{
output += '<p>PASS: "'+actual+' - called by '+name+'"</p>'
$('.output').html(output);
}
else
{
output += '<p>FAIL: "'+'expected: '+expected+', actual: '+actual+' - called by '+name+'"</p>';
$('.output').html(output);
}
}
test(false, testWithOrAndOneVariable('foo'), 'testWithOrAndOneVariable');
test(false, testWithOrAndRepeatedVariables('foo'), 'testWithOrAndRepeatedVariables');
test(false, testWithAndAndOneVariable('foo'), 'testWithAndAndOneVariable');
test(false, testWithAndAndRepeatedVariables('foo'), 'testWithAndAndRepeatedVariables');
test(true, testWithOrAndOneVariable('bar'), 'testWithOrAndOneVariable');
test(true, testWithOrAndRepeatedVariables('bar'), 'testWithOrAndRepeatedVariables');
test(true, testWithAndAndOneVariable('bar'), 'testWithAndAndOneVariable');
test(true, testWithAndAndRepeatedVariables('bar'), 'testWithAndAndRepeatedVariables');
test(true, testWithOrAndOneVariable('quz'), 'testWithOrAndOneVariable');
test(true, testWithOrAndRepeatedVariables('quz'), 'testWithOrAndRepeatedVariables');
test(true, testWithAndAndOneVariable('quz'), 'testWithAndAndOneVariable');
test(true, testWithAndAndRepeatedVariables('quz'), 'testWithAndAndRepeatedVariables');
test(true, testWithOrAndOneVariable('dog'), 'testWithOrAndOneVariable');
test(true, testWithOrAndRepeatedVariables('dog'), 'testWithOrAndRepeatedVariables');
test(true, testWithAndAndOneVariable('dog'), 'testWithAndAndOneVariable');
test(true, testWithAndAndRepeatedVariables('dog'), 'testWithAndAndRepeatedVariables');
test(true, testWithoutOrAndOneVariable('foo'), 'testWithoutOrAndOneVariable');
test(true, testWithoutOrAndRepeatedVariables('foo'), 'testWithoutOrAndRepeatedVariables');
test(true, testWithoutAndAndOneVariable('foo'), 'testWithoutAndAndOneVariable');
test(true, testWithoutAndAndRepeatedVariables('foo'), 'testWithoutAndAndRepeatedVariables');
test(false, testWithoutOrAndOneVariable('bar'), 'testWithoutOrAndOneVariable');
test(false, testWithoutOrAndRepeatedVariables('bar'), 'testWithoutOrAndRepeatedVariables');
test(false, testWithoutAndAndOneVariable('bar'), 'testWithoutAndAndOneVariable');
test(false, testWithoutAndAndRepeatedVariables('bar'), 'testWithoutAndAndRepeatedVariables');
test(false, testWithoutOrAndOneVariable('quz'), 'testWithoutOrAndOneVariable');
test(false, testWithoutOrAndRepeatedVariables('quz'), 'testWithoutOrAndRepeatedVariables');
test(false, testWithoutAndAndOneVariable('quz'), 'testWithoutAndAndOneVariable');
test(false, testWithoutAndAndRepeatedVariables('quz'), 'testWithoutAndAndRepeatedVariables');
test(false, testWithoutOrAndOneVariable('dog'), 'testWithoutOrAndOneVariable');
test(false, testWithoutOrAndRepeatedVariables('dog'), 'testWithoutOrAndRepeatedVariables');
test(false, testWithoutAndAndOneVariable('dog'), 'testWithoutAndAndOneVariable');
test(false, testWithoutAndAndRepeatedVariables('dog'), 'testWithoutAndAndRepeatedVariables');
/*
Output:
FAIL: "expected: false, actual: true - called by testWithOrAndOneVariable"
PASS: "false - called by testWithOrAndRepeatedVariables"
PASS: "false - called by testWithAndAndOneVariable"
PASS: "false - called by testWithAndAndRepeatedVariables"
PASS: "true - called by testWithOrAndOneVariable"
PASS: "true - called by testWithOrAndRepeatedVariables"
PASS: "true - called by testWithAndAndOneVariable"
FAIL: "expected: true, actual: false - called by testWithAndAndRepeatedVariables"
PASS: "true - called by testWithOrAndOneVariable"
PASS: "true - called by testWithOrAndRepeatedVariables"
FAIL: "expected: true, actual: false - called by testWithAndAndOneVariable"
FAIL: "expected: true, actual: false - called by testWithAndAndRepeatedVariables"
PASS: "true - called by testWithOrAndOneVariable"
PASS: "true - called by testWithOrAndRepeatedVariables"
FAIL: "expected: true, actual: false - called by testWithAndAndOneVariable"
FAIL: "expected: true, actual: false - called by testWithAndAndRepeatedVariables"
PASS: "true - called by testWithoutOrAndOneVariable"
PASS: "true - called by testWithoutOrAndRepeatedVariables"
PASS: "true - called by testWithoutAndAndOneVariable"
PASS: "true - called by testWithoutAndAndRepeatedVariables"
FAIL: "expected: false, actual: true - called by testWithoutOrAndOneVariable"
FAIL: "expected: false, actual: true - called by testWithoutOrAndRepeatedVariables"
PASS: "false - called by testWithoutAndAndOneVariable"
PASS: "false - called by testWithoutAndAndRepeatedVariables"
FAIL: "expected: false, actual: true - called by testWithoutOrAndOneVariable"
FAIL: "expected: false, actual: true - called by testWithoutOrAndRepeatedVariables"
FAIL: "expected: false, actual: true - called by testWithoutAndAndOneVariable"
PASS: "false - called by testWithoutAndAndRepeatedVariables"
FAIL: "expected: false, actual: true - called by testWithoutOrAndOneVariable"
FAIL: "expected: false, actual: true - called by testWithoutOrAndRepeatedVariables"
FAIL: "expected: false, actual: true - called by testWithoutAndAndOneVariable"
PASS: "false - called by testWithoutAndAndRepeatedVariables"
*/
<div class="output"></div>
@jasdeepkhalsa
Copy link
Author

@jasdeepkhalsa
Copy link
Author

Shorthand:

  • is - if(var === x || var === y) { // is } else { // is not }
  • is not - if(var !== x && var !== y) { // is not } else { // is }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment