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

JavaScript Switch Statement Absolute Beginner Lesson 14

JavaScript Switch statement is used when we have a lot of variables that we want to match. In the following example we get the day of the week. We can then show something for each day of the week.

Getting A Day JavaScript Built In Function

There are already functions that JavaScript comes with in every browser. The one we are going to use is they date. There are different variations of the date that you can get. We are going to focus on the day of the week. We use new Date().getDay to get the numbers 0 through 6. 0 is Sunday while 6 is Saturday.

Tip: Click on images to make them larger. If you find anything wrong on this page or need further assistance please comment below or contact me. 🙂

Switch Statements in JavaScript

In Switch statements we try to match a variable. When that variable is matched we can then print out on the page what we want it to. In the following example in today’s lesson we are printing out the days of the week. Here is the code.

Code For Today’s Lesson

<p id="foo"></p>;
var boo = document.getElementById('foo');
var cday = new Date().getDay();
var day;
switch (cday){
case 0:
day = "Sunday";
case 1:
day ="Monday";
case 2:
day = "Tuesday";
case 3:
day = "Wednesday";
case 4:
day = "Thursday";
case 5:
day = "Hey Its Friday";
case 6:
day = "Saturday";

Next Lesson

Subscribe To My YouTube Channel:
JavaScript Switch Statement Absolute Beginner Lesson 14 was last modified: June 24th, 2017 by Maximus Mccullough

Please like, share and subscribe.

Advanced Programming Made Easy

I hope you enjoyed the article. If I was able to help you please consider a tip for the content.

One Time Tip

more tips

Reoccuring Tips


Cool People Share:

Leave a Reply

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

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