Combining the best of the answers above, plus my own small improvements.
Usually people want a list of months to start with January, not December. Switching from new Date(Date.UTC(2021, m)))
to simply new Date(2021, m))
resolves this, as it creates a date in the user's own locale.
/**
* Get a list of the 12 months of the year as strings, according to specified locale and format
* @typedef {Object} Options
* @property {string} [locale=navigator.language] : name of locale, e.g. en-GB, defaults to
* the user's own locale
* @property {string} [monthFormat="long"] : "short", "numeric", or "long" (default)
*
* @param {Options} [options] : input options
* @return {string[]} : an array of 12 strings, the months of the year in the requested format
*/
function getAllMonths({ locale = navigator.language, format = "long"} = {}) {
const applyFormat = new Intl.DateTimeFormat(locale, { month: format }).format;
return [...Array(12).keys()].map((m) => applyFormat(new Date(2021, m)));
}
// Testing in en-US locale
console.log(getAllMonths());
// ["January", "February", ... "December"]
console.log(getAllMonths({ format: "numeric" }));
// ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"]
console.log(getAllMonths({ format: "short" }));
// ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"]
console.log(getAllMonths({ locale: "es-mx" }));
// ["enero", "febrero", ... "diciembre"]
console.log(getAllMonths({ locale: "ja-JP", format: "short" }));
// ["1ζ", "2ζ", "3ζ", "4ζ", "5ζ", "6ζ", "7ζ", "8ζ", "9ζ", "10ζ", "11ζ", "12ζ"]