You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
switch (new Date().getDay()) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
break;
}
The text was updated successfully, but these errors were encountered:
I would say try to avoid using switch. It is many times a smell of some kind of object hierarchy. In your example way clearer to use a plain data-structure that contains the day information:
var DAYS = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var day = DAYS[new Date().getDay()];
I don't state that switch is always wrong, in some cases it is the best option, but it is a dangerous tool (you can forget breaks) and many times there is a way clearer and easier to read way.
What is the best practices with switch?
The text was updated successfully, but these errors were encountered: