Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript
twcompetition
communitycreator

What is getDay() in JavaScript?

Victor Akanmidu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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.

RELATED TAGS

javascript
twcompetition
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring