113

Is there a way to write this on fewer lines, but still easily readable?

var month = '';

switch(mm) {
    case '1':
        month = 'January';
        break;
    case '2':
        month = 'February';
        break;
    case '3':
        month = 'March';
        break;
    case '4':
        month = 'April';
        break;
    case '5':
        month = 'May';
        break;
    case '6':
        month = 'June';
        break;
    case '7':
        month = 'July';
        break;
    case '8':
        month = 'August';
        break;
    case '9':
        month = 'September';
        break;
    case '10':
        month = 'October';
        break;
    case '11':
        month = 'November';
        break;
    case '12':
        month = 'December';
        break;
}
10
  • 7
    IMHO vidriduch's answer is the most appropriate. This is probably not the only part of your code that requires Date manipulations (even though the one you showed is particulary easy to code). You should seriously consider using existing, tested Date libraries.
    – coredump
    Apr 23, 2015 at 21:18
  • 2
    I don't know javascript, but doesn't it have a hashmap, like Python's dictionary or C++'s std::map?
    – Masked Man
    Apr 24, 2015 at 6:13
  • 28
    Isn't this suppose to be for codereview.stackexchange.com?
    – Loko
    Apr 24, 2015 at 9:00
  • 2
    So many answers changing the behavior of the code by not taking the default '' into account which results in undefined output, which is different as what the original does.
    – Pieter B
    Apr 24, 2015 at 13:04
  • 2
    This is not a duplicate question >:( this is asking a completely different question, the answer may be the same however.
    – Leon Gaban
    May 9, 2015 at 22:05

13 Answers 13

201

Define an array, then get by index.

var months = ['January', 'February', ...];

var month = months[mm - 1] || '';
18
  • 23
    instead of mm - 1 you can also set undefined as the first value (index 0) so the array indices will match month numbers
    – Touffy
    Apr 23, 2015 at 15:40
  • 9
    var month = month[(mm -1) % 12]
    – mpez0
    Apr 23, 2015 at 17:25
  • 77
    @mpez0 I think I'd prefer knowing that someone's managed to come up with month number 15, rather than hide what is probably bad data
    – Izkata
    Apr 23, 2015 at 18:23
  • 21
    @Touffy I think I'd stick with mm-1, so that months.length==12.
    – Teepeemm
    Apr 23, 2015 at 20:28
  • 48
    @Touffy I would argue that's not a matter of taste, but a matter of avoiding clever code. Imagine yourself reading some else's [undefined, 'January', 'February', ...] - I best your first reaction is WTF?!, which is usually not a good sign...
    – miraculixx
    Apr 23, 2015 at 22:52
82

what about not to use array at all :)

var objDate = new Date("10/11/2009"),
    locale = "en-us",
    month = objDate.toLocaleString(locale, { month: "long" });

console.log(month);

// or if you want the shorter date: (also possible to use "narrow" for "O"
console.log(objDate.toLocaleString(locale, { month: "short" }));

as per this answer Get month name from Date from David Storey

2
  • 2
    Given the problem statement in question, your answer is not really solving that problem but some different solution which might be correct in a different context. Selected answer is still the best and most efficient.
    – TechMaze
    Apr 24, 2015 at 5:56
  • 6
    Only new Date("2009-11-10") format is guaranteed to be parsed (see this specifiation: ecma-international.org/publications/standards/Ecma-262.htm). Other date formats (including one in your answer) may be parsed if browser chooses so, and hence are not portable.
    – jb.
    Apr 25, 2015 at 15:44
59

Try this:

var months = {'1': 'January', '2': 'February'}; //etc
var month = months[mm];

Note that mm can be an integer or a string and it will still work.

If you want non-existing keys to result in empty string '' (instead of undefined), then add this line:

month = (month == undefined) ? '' : month;

JSFiddle.

3
  • 4
    On larger datasets than "months of the year" this will probably be more efficient.
    – DGM
    Apr 24, 2015 at 0:48
  • 3
    This is effectively an enum (i.e. make it immutable), define it as var months = Object.freeze({'1': 'January', '2': 'February'}); //etc See Enums in JavaScript?
    – Alexander
    Apr 24, 2015 at 18:19
  • 1
    @Alexander If you swap the key and values, then yes it's similar to an enum. Apr 24, 2015 at 19:01
26

You could create an array instead and lookup the month name:

var months = ['January','February','March','April','May','June','July','August','September','October','November','December']


var month = months[mm-1] || '';

See the answer by @CupawnTae for the rational behind the code || ''

2
  • rather than start with 0 index you could keep undefined at 0 as var months = [ undefined, 'January','February','March', ..... In this way you will use month = months[mm]; Apr 24, 2015 at 6:28
  • @GrijeshChauhan: please avoid 'clever' code. The next person's first reaction whould be wtf. It's only a '-1', months.Length will then be 13, wtf^2. programmers.stackexchange.com/questions/91854/…
    – RvdK
    Apr 24, 2015 at 8:02
19

Be careful!

The thing that should immediately trigger alarm bells is the first line: var month = ''; - why is this variable being initialized to an empty string, rather than null or undefined? It may just have been habit or copy/pasted code, but unless you know that for sure, it is not safe to ignore it when you're refactoring code.

If you use an array of month names and change your code to var month = months[mm-1]; you are changing the behaviour, because now for numbers outside the range, or non-numeric values, month will be undefined. You may know that this is ok, but there are many situations where this would be bad.

For example, let's say your switch is in a function monthToName(mm), and someone is calling your function like this:

var monthName = monthToName(mm);

if (monthName === '') {
  alert("Please enter a valid month.");
} else {
  submitMonth(monthName);
}

Now if you change to using an array and returning monthName[mm-1], the calling code will no longer function as intended, and it will submit undefined values when it is supposed to display a warning. I'm not saying this is good code, but unless you know exactly how the code is being used, you can't make assumptions.

Or maybe the original initialization was there because some code further down the line assumes that month will always be a string, and does something like month.length - this will result in an exception being thrown for invalid months and potentially kill the calling script completely.

If you do know the entire context - e.g. it's all your own code, and no-one else is ever going to use it, and you trust yourself not forget you made the change sometime in the future - it may be safe to change the behaviour like this, but soooo many bugs come from this kind of assumption that in real life you're far better off programming defensively and/or documenting the behaviour thoroughly.

Wasmoo's answer gets it right (EDIT: a number of other answers, including the accepted one, have now been fixed too) - you can use months[mm-1] || '' or if you would prefer to make it more obvious at a glance what's happening, something like:

var months = ['January', 'February', ...];

var month;

if (mm >= 1 && m <= 12) {
  month = months[mm - 1];
} else {
  month = ''; // empty string when not a valid month
}
4
  • 1
    Nobody else has mentioned the change in behaviour yet, so this should be taken into account when re-factoring code.
    – Mauro
    Apr 24, 2015 at 10:18
  • This answer is spot on. Most of the other answers change the behavior of the code in a subtle way. This may not matter or it may become that irritatingly hard to find bug.
    – Pieter B
    Apr 24, 2015 at 13:03
  • Ah so it's always best to init a var to undefined? Does that save performance if the type gets converted?
    – Leon Gaban
    Apr 24, 2015 at 14:52
  • 2
    @LeonGaban it's not about performance: the original question initialized the variable to an empty string and left it at that if no valid month was selected, whereas a lot of the other answers here ignored that fact and changed the behaviour by returning undefined when the input wasn't 1..12. Except in very exceptional circumstances, correct behaviour trumps performance every time.
    – CupawnTae
    Apr 24, 2015 at 15:06
17

For completeness I'd like to supplement to current answers. Basically, you can ommit the break keyword and directly return an appropriate value. This tactic is useful if the value cannot be stored in a precomputed look-up table.

function foo(mm) {
    switch(mm) {
        case '1':  return 'January';
        case '2':  return 'February';
        case '3':  return 'March';
        case '4':  return 'April';
        // [...]
        case '12': return 'December';
    }
    return '';
}

Once again, using a look-up table or date functions is more succinct and subjectively better.

16

You could do it using an array:

var months = ['January', 'February', 'March', 'April', 
              'May', 'June', 'July', 'August', 
              'September', 'October', 'November', 'December'];

var month = months[mm - 1] || '';
0
12

Here's another option that uses only 1 variable and still applies the default value '' when mm is outside of range.

var month = ['January', 'February', 'March',
             'April', 'May', 'June', 'July',
             'August', 'September', 'October',
             'November', 'December'
            ][mm-1] || '';
1
  • Range checking, and throwing an exception could work also. And returning "Error", or "Undefined" might be alternative to the empty string. Apr 25, 2015 at 0:45
9

You could write it as an expression instead of a switch, using conditional operators:

var month =
  mm == 1 ? 'January' :
  mm == 2 ? 'February' :
  mm == 3 ? 'March' :
  mm == 4 ? 'April' :
  mm == 5 ? 'May' :
  mm == 6 ? 'June' :
  mm == 7 ? 'July' :
  mm == 8 ? 'August' :
  mm == 9 ? 'September' :
  mm == 10 ? 'October' :
  mm == 11 ? 'November' :
  mm == 12 ? 'December' :
  '';

If you haven't seen chained conditional operators before this may seem harder to read at first. Writing it as an expression makes one aspect even easier to see than the original code; it's clear that the intention of the code is to assign a value to the variable month.

1
  • 1
    I meant to suggest this one as well. It's actually very readable while remaining concise, and would work well for sparse mappings and non-numeric keys, which the array solution doesn't. PS I got a random unexplained downvote on my answer too - probably the same drive-by artist.
    – CupawnTae
    Apr 29, 2015 at 9:06
6

Building on Cupawn Tae's answer previous I'd shorten it to :

var months = ['January', 'February', ...];
var month = (mm >= 1 && mm <= 12) ? months[mm - 1] : '';

Alternatively, yes, I appreciate, less readable:

var month = months[mm - 1] || ''; // as mentioned further up
6
  • You can skip (!!months[mm - 1]) and simply do months[mm - 1].
    – YingYang
    Apr 29, 2015 at 11:56
  • That would result in undefined if the array index was out of range! Apr 30, 2015 at 12:49
  • months[mm - 1] will return undefined for an index that is out of range. Since undefined is falsy you will end up with '' as the value of month.
    – YingYang
    Apr 30, 2015 at 12:53
  • As stated in other answers you can simplify this line even more: var month = months[mm - 1] || '';
    – YingYang
    Apr 30, 2015 at 12:54
  • Although I've noticed further up (wasn't around when I posted), var month = months[mm - 1] || ''; Which would be neater still. Apr 30, 2015 at 12:56
5

Like @vidriduch, I would like to underline the importance of i20y ("internationalisability") of code in nowadays' context and suggest the following concise and robust solution together with the unitary test.

function num2month(month, locale) {
    if (month != Math.floor(month) || month < 1 || month > 12)
        return undefined;
    var objDate = new Date(Math.floor(month) + "/1/1970");
    return objDate.toLocaleString(locale, {month: "long"});
}

/* Test/demo */
for (mm = 1; mm <= 12; mm++)
    document.writeln(num2month(mm, "en") + " " +
                     num2month(mm, "ar-lb") + "<br/>");
document.writeln(num2month("x", "en") + "<br/>");
document.writeln(num2month(.1, "en") + "<br/>");
document.writeln(num2month(12.5, "en" + "<br/>"));

I try to stay as close as possible to the original question, i.e. transform numbers 1 to 12 into month names, not only for one special case, but return undefined in case of invalid arguments, using some of the formerly added criticism to and contents of other answers. (The change from undefined to '' is trivial, in case exact matching is needed.)

0
4
var getMonth=function(month){
   //Return string to number.
    var strMonth = ['January', 'February', 'March',
             'April', 'May', 'June', 'July',
             'August', 'September', 'October',
             'November', 'December'
            ];
    //return number to string.
    var intMonth={'January':1, 'February':2, 'March':3,
             'April':4, 'May':5, 'June':6, 'July':7,
             'August':8, 'September':9, 'October':10,
             'November':11, 'December':12
            };
    //Check type and return 
    return (typeof month === "number")?strMonth[month-1]:intMonth[month]
}
0

I'd go for wasmoo's solution, but adjust it like this :

var month = [
    'January',
    'February',
    'March',
    'April',
    'May',
    'June',
    'July',
    'August',
    'September',
    'October',
    'November',
    'December'
][mm-1] || '';

It is the exact same code, really, but differently indented, which IMO makes it more readable.

Not the answer you're looking for? Browse other questions tagged or ask your own question.