All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript If Statement Multiple Conditions

Last Updated : Mar 11, 2024

JavaScript If Statement Multiple Conditions

In this tutorial we will show you the solution of JavaScript if statement multiple conditions, as we know that conditional statements are used to perform different tasks based on different conditions.

And there are many types of conditional statements are one from them is if statement.

In previous articles we come in touch with types of conditional statements and basic if statement also.

In today’s article we will understand about if statement with multiple conditions. Let us understand.

Step By Step Guide On JavaScript If Statement Multiple Conditions :-

As, there are many ways with the help of which we can use multiple conditions in if statement.

And below we are going to specify a basic JavaScript code as example in which we understand about today’s topic.

Let us see the code below.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>home page</title>
    <script src="js/jquery.js"></script>
</head>
<body>
 <script>
    if ((condition && condition ) || (condition && condition )) {
        // javascript code is here
    }
    if ((condition &&(condition || condition))) {
        //javascript code is here
    }
</script>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Here, first of all before closing head we link a file there with the help of script tag. That file is of jQuery file.
  6. You can link cdn and download file and give reference with help of script tag.
  7. Now, one thing to notice here that we write script or say JavaScript code in body tag but at last. You can also write it in head tag. But after necessary code it is best way to write JavaScript at last.
  8. Inside script tag, which is a paired tag. Here, we use or create two if statements.
  9. And inside if statement we have to write the code which we want to print if the condition inside if is true.
  10. One thing to note here again that if and IF or If are three different things. So, it means that JavaScript is case sensitive language. So, there should be no any mistake, if you want to run code without error.
  11. You can see the if statement with multiple condition in our codes. You can use any condition inside if.
  12. Here we use basic AND and OR condition. You can use any and replace this code. We hope that you understand this code properly.
  13. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to use multiple conditions in if statement with the help of JavaScript.

I hope this tutorial on JavaScript if statement multiple conditions helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪