What is getDay() in JavaScript?

In JavaScript, getDay() is a method that returns the day of the week for a specified date. It returns a number corresponding to the days of the week, as shown below:

  • 0 = Sunday
  • 1 = Monday
  • 2 = Tuesday
  • 3 = Wednesday
  • 4 = Thursday
  • 5 = Friday
  • 6 = Saturday

Consider the example below:

  • JavaScript

We created a date and assigned it to the variable myBday.

On line number 1, we used the getDay() method to get the day of the week from myBday with myBday.getDay().

Now when we run the code, it returns 1, which corresponds to Monday.

Returning weekday in words

For the number returned by the getDay(), we can get the corresponding weekday in words. Consider the following example:

  • JavaScript

We still have our previous date, myBday. We created an array, specified that it should contain 7 items, and called it weekday.

We assigned each position in the array to a weekday, for example, weekday[3] = "wednesday".

The myBday.getDay() method returned 1. The return value of the myBday.getDay() method was used to access the word form of the corresponding week day from the array weekday.

Free Resources