Update:
In MDN, Date constructors syntax includes
new Date()
new Date(value)
new Date(dateString)
new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]])
"2021-02-04" is a dateString which is strongly discouraged by MDN( This reference is wildly distributed in Internet ^v^ )
Parsing of date strings with the Date constructor (and Date.parse(), which works the same way) is strongly discouraged due to browser differences and inconsistencies.
Support for RFC 2822 format strings is by convention only.
Support for ISO 8601 formats differs in that date-only strings (e.g. "1970-01-01") are treated as UTC, not local.
In brief, there are many international standards that create conflicts in dateString mode. Different browsers may use different standards, that is "browser differences". So that's why I run your code in mdn, the output is 4 which is different from yours.
There is another answer facing the same situation as yours, and the output is smaller than the correct result by 1 as yours.
If you Google, you will find many people are trapped. I think this answer should be filed with other answers like:
So, I strongly recommend don't use dateString to construct Date. Just use new Date(2021, 02, 04)
, you will get the correct answer.