Skip to content

JavaScript constant naming convention | Basics

  • by

Use all caps for JavaScript constant naming. Most of the other programming languages (Java and Python) have all caps so I would suggest using that.

ES6 provides a new way of declaring a constant by using the const keyword. The const keyword creates a read-only reference to a value.

const CONSTANT_NAME = value;

By convention, the constant identifiers are in uppercase.

  • 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.

JavaScript constant naming convention

Simple example code.

<!DOCTYPE html>
<html>
<body>
  <script>    
    const myVar = "Const Value";
    const COLOR_CODES = {
      BLUE: 1,
      RED: 1
    };

    console.log(myVar);
    console.log(COLOR_CODES)
  </script>  

</body>
</html>

Output:

JavaScript constant naming convention

Capitalization helped us see values we wanted to remain constant.

var HOURS_IN_DAY = 24;
var hoursRemaining = currentHour - HOURS_IN_DAY;
var MY_NAME = 'Brandon';
MY_NAME = ... // oops don't want to do this.

Note: this naming convention is a convention, not a strict rule enforced by the language. JavaScript itself doesn’t enforce naming conventions, so it’s up to developers and teams to adopt and follow conventions consistently for better code maintainability and collaboration.

Comment if you have any doubts or suggestions on this Js constant topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading