Тема: What is wrong with this code?

I want to compare two vars and if they both match a value execute a code.

if (string == "1") && if (string2 == "1") {alert('Select a diferent value for string2');}
if (string == "1") && if (string2 == "2") {window.open('http://example.com');}

The code above doesn't work. Any help

Thanks

2

Re: What is wrong with this code?

In JS you may not write like this:

if(condidion1) && if(condition2) {doSomething();}

You have to write like this:

if(condidion1 && condition2) {doSomething();}

Generally, "if" statement's header consists of only one "if" keyword in the beginning and only one condition in the parentheses after "if". Condition is any expression, it may contain operators like "==" or "&&" but may not contain "if" keyword.

Подякували: privateloader1

3

Re: What is wrong with this code?

Please, don't describe the problem in negative terms like "doesn't work". Instead write what is it ought to do, and what is really does, like "I expect this code to show an alert message, but I get "Uncaught SyntaxError: Unexpected token &&" message in console".
This will:
1. help us to find the solution quicker;
2. help us with you to avoid some hard to diagnose problems hidden under obvious small mistakes;
3. help you to see the problem more clearly, and, possibly, you will solve it without writing here.

4

Re: What is wrong with this code?

P.Y. написав:

In JS you may not write like this:

if(condidion1) && if(condition2) {doSomething();}

You have to write like this:

if(condidion1 && condition2) {doSomething();}

Generally, "if" statement's header consists of only one "if" keyword in the beginning and only one condition in the parentheses after "if". Condition is any expression, it may contain operators like "==" or "&&" but may not contain "if" keyword.

Thanks...

5

Re: What is wrong with this code?

string1 === "1" && string2 === "1" && alert("Bla bla bla");