0

I have absolutely no idea why this is not working. Makes no sense to me.

This returns a "syntax error: parse error":

if ($(this).attr("id") === 'search' || opening = true) return false;

For good measure, I also tried the following, which yielded the same result:

if (1 = 1 && 2 = 2) { return false; }
2
  • is there any more code? because here it doesn't do anything wrong, except for assignment of opening. Commented Jul 18, 2011 at 0:13
  • @Igois: Why do you need more code? You can see quite clearly that the problem here is the use of a single equals is being used to determine equality in a statement. Commented Jul 18, 2011 at 0:15

6 Answers 6

10

There are three different operators at play:

  • =: assignment
  • ==: equality
  • ===: strict equality

= actually modifies a variable, so you shouldn't use it inside if statements. That is, you should use ... || opening == true) instead of ... || opening = true).

Sign up to request clarification or add additional context in comments.

1 Comment

Note that using = in an if condition is (generally considered) bad practice but is still perfectly valid JS. The reason the code in the question gives an error is because operator precedence results in the expression being evaluated as ($(this).attr("id") === 'search' || opening) = true and of course the first part ($(this).attr("id") === 'search' || opening) is not a variable so it can't be assigned a value.
7

In JavaScript = is used to assign values, while == and === are used to compare them.

When you put opening = true in your if statement, you aren't checking if opening is true, you are setting opening to true. Try using == instead.

For example,

var x = 5;
if (x == 10) {
    alert("x is 10");
} else {
    alert("x isn't 10");
}

will display "x isn't 10", while

var x = 5;
if (x = 10) {
    alert("x is 10");
} else {
    alert("x isn't 10");
}

will display "x is 10".

1 Comment

The second one is giving an error because you cannot assign 1 a value as it is not a valid variable name. Same for 2.
1

the first example should read:

if ($(this).attr("id") === 'search' || opening == true) return false;

and the second:

if (1 == 1 && 2 == 2) { return false; }

Note the double equals (==) sign for logic equals is not the same as the single equals (=) sign, that handles variable assignment.

Comments

1

You have an error in your condition

if ($(this).attr("id") === 'search' || opening = true) return false;

should be

if ($(this).attr("id") === 'search' || opening == true) return false;

the problem is with the equals sign

= is different to ==

the first one is the assignment operator. the second one is for comparison

Comments

0

When you test like this:

opening=true;

What you are really doing is setting opening to the value of true. Use == instead.

Finally, order of operations, even if correct, can get confusing. Put parenthesis around each part of the comparison.

if (($(this).attr("id") === 'search') || (opening == true)) return false;

Comments

-4

My guess is that === does not exist. == is for testing equality

so if ($(this).attr("id") === 'search' || opening == true) return false; should be if ($(this).attr("id") == 'search' || opening == true) return false;

1 Comment

=== compares value and type. It definitely is a valid operator.

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.