-2

I am a javascript newbie. I am learning and I am trying to get the day of week and write it to the document. I am using an array and switch case.

Check my code:

var currentDate=new Date();
var currentDay=currentDate.getDay();
var daysOfWeek=["Sunday","Monday","Tuesday","Wed","Thu","Fri","Sat"];

switch(currentDay){
    case 0:
    document.write('Today is  <b>' +daysOfWeek[0]);

    break;
    case 1:

    break;
    case 2:

    break;
    case 3:

    break;
    case 4:

    break;
    case 5:

    break;

    case 6:

    break;
    default:
    break;
}

It's working fine but I need to know whether this the best possible code i can write or if I can improve it.

RobG
  • 142,382
  • 31
  • 172
  • 209
Cloudboy22
  • 1,496
  • 5
  • 21
  • 39
  • 2
    What is `statements_1`? –  May 17 '15 at 02:31
  • 2
    What is the `switch` statement supposed to be good for? You can directly access the right array element via the day number, no need for a switch statement at all. – CBroe May 17 '15 at 02:36
  • possible duplicate of [How to get the day of week and the month of the year?](http://stackoverflow.com/questions/4822852/how-to-get-the-day-of-week-and-the-month-of-the-year) – bud-e May 17 '15 at 02:41
  • What is your definition of "best"? – RobG May 17 '15 at 03:29

1 Answers1

-1

One better way of doing this (while keeping your general approach) would be:

var currentDate=new Date();
var currentDay=currentDate.getDay();
var daysOfWeek=["Sunday","Monday","Tuesday","Wed","Thu","Fri","Sat"]; 
document.write('Today is  <b>' +daysOfWeek[currentDay] + '</b>');
Sukima
  • 9,965
  • 3
  • 46
  • 60
S McCrohan
  • 6,663
  • 1
  • 30
  • 39