mralexlau mralexlau - 1 month ago 5
Javascript Question

Naming convention for const object keys in ES6

Is there a recommended naming convention for key names within a const object in es6? I haven't been able to find a resource which states if they should be uppercase or lowercase.

const COLOR_CODES = {
BLUE: 1,
RED: 1
};


vs

const COLOR_CODES = {
blue: 1,
red: 1
};


The examples from this MDN article show both styles, so maybe both are acceptable.

Answer

Google recommends the following:

const COLOR_CODES = {
  BLUE: 1,
  RED: 1
};

See: https://google.github.io/styleguide/javascriptguide.xml#Constants

  • Use NAMES_LIKE_THIS for constant values.
  • Use @const to indicate a constant (non-overwritable) pointer (a variable or property).
  • Never use the const keyword as it's not supported in Internet Explorer.