JavaScript AND OR in IF Statement to Check For Multiple Conditions Absolute Beginner

Using “AND  &&” in The Conditions Statement

Sometimes we need to check and see if 2 condition are met. Let’s say we wanted to display “Its the Weekend In June” in June. Then for rest of the months we just want to say “It’s the Weekend”. How can we accomplish this? Look at this code.

<script>
var x = document.getElementById('foo');
var y = new Date().getDay();
var z = new Date().getMonth();

if(y==5 || y==6 || y==0 && z==5){
x.innerHTML="Its The Weekend in June!";
}else if(y==5 || y==6 || y==0){
x.innerHTML="Its the weekend";
}else{
x.innerHTML="Have a Nice Day";
}
</script>

Explanation of Code

We made a few changes from the previous code. The first change is we set a new variable of “z”. With it we got the month of the year. Remember that the numbering starts at “0”. In JavaScript, you have 0 to 11 for the months, so the month of June would be #5. In the IF condition, we added “&& z==5” to check and see if we were in June. If we met the conditions we display, “It’s the weekend in June”. Next, we added a “else if” statement to check for another condition. We are checking to make sure the days are still the weekend, however NOT the weekend in June. If we meet this condition, then we display “Have a Nice Day.

The next page will have video instructions. Click here to go to next page. 

Only cool people share!

JavaScript AND OR in IF Statement to Check For Multiple Conditions Absolute Beginner was last modified: November 10th, 2021 by Maximus Mccullough
Summary
JavaScript AND OR in IF Statement to Check For Multiple Conditions Absolute Beginner
Article Name
JavaScript AND OR in IF Statement to Check For Multiple Conditions Absolute Beginner
Description
There are times when you want to check if 2 or more conditions are met in JavaScript. To accomplish this we use the OR "||", AND "&&" statements within the IF condition.
Author
Publisher
A1WEBSITEPRO LLC
Logo
JavaScript AND OR in IF Statement to Check For Multiple Conditions

Pages:Previous 1 2 3 Next

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.