1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 5.00 out of 5)

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

JavaScript AND OR in IF Statement to Check For Multiple Conditions

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. In the last lesson  we were checking the date. If a certain date met the condition we displayed something. What if we had a couple dates where we wanted to display something? Lets see how this can be accomplished in JavaScript.

new Date().getDay *

We can get the number of the day with a JavaScript function.  Notice that the first number in this function starts with “0”. So Sunday would be equal to “0” and Saturday would be equal to “6”. If you use the code below it will display today’s day.

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

Using OR || in The Condition Statement *

Now what we want to do is display “It’s The Weekend” on Friday, Saturday and Sunday. On the rest of the days we will display, “Have a nice day”. Lets see how this is accomplished.

<script>
var x = document.getElementById('foo');
var y = new Date().getDay();
if(y==5 || y==6 || y==0){
x.innerHTML="Its The Weekend!";
}else{
x.innerHTML="Have A Nice Day";
}
</script>;

Explanation of Code *

This will give us the number of the current day. We store that in a variable called “y”.  Next we started our if statement and conditions. We are checking to see if the day is Friday, Saturday or Sunday. If it is then we display, “Its The Weekend”. We then use the “else” statement for every other day to display, “Have a Nice Day”.

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 the month of June. Then for rest of the months we just want to say “It’s the Weekend”.  How can this be accomplished? take a 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 the month of June. If these conditions were met we display, “Its the weekend in the month of 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’s in June. If this condition is met then we display “Have a Nice Day.

 

Next Lesson

 

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
A1WEBSITEPRO LLC

Related Post

JavaScript AND OR in IF Statement to Check For Multiple Conditions Absolute Beginner was last modified: June 23rd, 2017 by Maximus Mccullough

Advanced Programming Made Easy

I hope you enjoyed the article. If I was able to help you please consider buying me a drink.

Drinks

Leave a Reply

Your email address will not be published. Required fields are marked *