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:
Consider the example below:
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.
For the number returned by the getDay()
, we can get the corresponding weekday in words. Consider the following example:
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
.