BruceyBandit
BruceyBandit

Reputation: 4334

Does this code mean 'not equal'?

I just want to know if (clickedNumber === 'Yes or No') means if the clickedNumber = 'Yes or No', then if I want to say the clickedNumber doesn't equal 'Yes or No', then is it like this:

(clickedNumber !== 'Yes or No')?

Thanks

Upvotes: 2

Views: 12181

Answers (3)

T.J. Crowder
T.J. Crowder

Reputation: 1075755

The code you've quoted checks to see if the variable clickedNumber is strictly equal to the string "Yes or No". The opposite of that is clickedNumber !== 'Yes or No', but it's important to understand what you're dealing with.

If clickedNumber is 'Yes', then clickedNumber === 'Yes or No' is false, because 'Yes' is not strictly (or loosely) equal to 'Yes or No'. Strict equality (===) means:

  • The operands are of the same type (this is why it's "strict" rather than "loose"; "loose" equality allows casting, for instance 1 == "1").

  • If the operands are of the same type, they have the same value.

Naturally 'Yes or No' and 'Yes' do not have the same value (they do have the same type).

If you genuinely do want to check against 'Yes or No', fair enough, but if your goal is to check against 'Yes' or 'No', individually, you must do that expressly:

if (checkedNumber === 'Yes' || checkedNumber === 'No') {
    // checkedNubmer is 'Yes' **or** it's 'No'
}

...the opposite of which is

if (checkedNumber !== 'Yes' && checkedNumber !== 'No') {
    // checkedNubmer is neither 'Yes' **nor** 'No'
}

Upvotes: 10

nnnnnn
nnnnnn

Reputation: 150080

Yes. The code you mentioned:

(clickedNumber !== 'Yes or No')

Will evaluate as true if clickedNumber is not equal to the string 'Yes or No'. It will evalue as false if clickedNumber is the string 'Yes or No'.

Note that this is not the same as testing whether it is not equal to either of the two separate strings 'Yes' or 'No', which you would do like this:

!(clickedNumber === 'Yes' || clickedNumber === 'No')

Further reading: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Comparison_Operators

Upvotes: 1

Merlyn Morgan-Graham
Merlyn Morgan-Graham

Reputation: 59151

if I want to say the clickedNumber doesn't equal 'Yes or No', then is it like this:

(clickedNumber !== 'Yes or No')

What you said is correct, but it might not give the results you expect.

For example, what you currently have just checks if the string clickedNumber is equal to the string 'Yes or No':

var clickedNumber = 'Yes or No';

if(clickedNumber === 'Yes or No')
{
    alert("check 1"); // this will be triggered
}

if(clickedNumber !== 'Yes or No')
{
    alert("check 2"); // this will *not* be triggered
}

if(clickedNumber === 'Yes')
{
    alert("check 3"); // this will *not* be triggered
}

if(clickedNumber === 'No')
{
    alert("check 4"); // this will *not* be triggered
}

It doesn't use the contents of that string to define any boolean logic, so the word or doesn't actually do anything - it just just part of a string:

var clickedNumber = 'Yes';

if(clickedNumber === 'Yes or No')
{
    alert("check 1"); // this will *not* be triggered
}

if(clickedNumber !== 'Yes or No')
{
    alert("check 2"); // this will be triggered
}

if(clickedNumber === 'Yes')
{
    alert("check 3"); // this will be triggered
}

if(clickedNumber === 'No')
{
    alert("check 4"); // this will *not* be triggered
}

If you want boolean logic to apply, then you'll have to use boolean operators in code rather than in the contents of a string:

var clickedNumber = 'Yes';

if(clickedNumber === 'Yes' || clickedNumber === 'No')
{
    alert("check 1"); // this will be triggered
}

if(clickedNumber !== 'Yes' && clickedNumber !== 'No')
{
    alert("check 2"); // this will *not* be triggered
}

Upvotes: 1

Related Questions