for example, sometimes I need to define a hardcoded string with some variable parts, I often have trouble to choose the style:

style 1 : reuse every characters when possible

  let strArr=['Mon','Tues','Wednes','Thurs','Fri','Satur','Sun'];
  alert('Today is '+strArr[num]+'day');

style 2: Keep the varible part as a whole world when possible

  let strArr=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
  alert('Today is '+strArr[num]);

style 3: each individual case have their string even if they have some similar part:

  let strArr=[
    'Today is Monday',
    'Today is Tuesday',
    'Today is Wednesday',
    'Today is Thursday',
    'Today is Friday',
    'Today is Saturday',
    'Today is Sunday',

which style should I use?

  • 1
    The first one looks strange to me, I probably wouldn't join parts of words even if they're all the same. The last is clear but requires more work if you want to change the first part of the string (which wouldn't matter for such a small array, but would if it was much larger). I'd go with style 2.
    – Turksarama
    Commented Nov 1, 2018 at 3:46
  • 2
    @Turksarama Agreed, style 2 is also more robust towards translation into other languages.
    – Omegastick
    Commented Nov 1, 2018 at 8:23
  • @Omegastick, third option is more translation friendly, because different languages can have different phrase structures based on the variable value.
    – Fabio
    Commented Nov 7, 2018 at 20:20
  • @Fabio Yeah, I guess it's a trade-off with 1 being the most concise, 3 being the most robust, and 2 somewhere in the middle.
    – Omegastick
    Commented Nov 8, 2018 at 1:22
  • style 4: Remove "Today is" and display only name of the weekday. I bet the fact that it is today will be obvious from the surrounding context where you displaying the text. Then your code will look simpler a more translation friendly ;)
    – Fabio
    Commented Nov 8, 2018 at 2:13

1 Answer 1


As we are living in a more and more global world, you should really consider how easy it is to translate your texts to other languages. This almost automatically disqualifies your first option, as there will almost certainly be an (important) language that does not have a common part between all possibilities of the dynamic part.

The second option (literally with string concatenation) is also problematic for translations, as different languages will have different word orders and different positions within the text to place the dynamic part.

The third option is reasonable, but not very friendly for maintenance due to the repetitions of the common parts.

A fourth option that is both maintenance friendly and translation friendly is to use string replacements.

  let strArr=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
  alert('Today is {0}'.format(strArr[num]));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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