Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)

I am having trouble recalling how to compare these two strings in an if statement. What I am trying to do is check if my variable compare equals page1 or page2 if not, go to the else statement.

 var compare = "page3";

if (compare === "page1" || "page2") {

  document.body.innerHTML = "github url";

} else {

  document.body.innerHTML = "non-github url";

}

1 Answer

0 votes
by (13.1k points)

You could check every option 

if (compare === "page1" || compare === "page2") {

Or you could use an array and check with an existential quantifier like Array#some against, like

if (["page1", "page2"].some(a => a === compare)) {

var compare = "page3";

if (compare === "page1" || compare === "page2") {

    document.body.innerHTML = "github url";

} else {

    document.body.innerHTML = "non-github url";

}

Interested in full stack development? Check out the Full stack developer course from Intellipaat.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Mar 17, 2021 in Java by dante07 (13.1k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...