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

Sometimes you want to check if we meet 2 or more conditions 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 of dates where we wanted to display something? Lets see how this can accomplish this 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;&lt;/script&gt;</pre>
<h2>Using OR || in The Condition Statement</h2>
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.
<pre class="EnlighterJSRAW" data-enlighter-language="generic">&lt;script&gt;
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, “It’s The Weekend”. We then use the “else” statement for every other day to display, “Have a Nice Day”.

Now let’s go over using “AND &&” in the conditions statement on the next page.

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: 1 2 3 Next

Leave a Reply

Your email address will not be published.

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