15 Naming Convention Points to Specify Names in JS

Javascript Naming Conventions

This article is bout naming conventions in javascript. Specify a constant or variable name in JavaScript by following the 15 naming convention points below.

  • Variable names and constant names should be meaningful. In the Below code, the variable name is playerscore which represents a player’s score and the constant name is width which represents the size of the width. But, If you give alphabets like x,  y as names, it has no meaning

Ex:

var Playerscore=1;
const height=30;

Do not name like this

var x=0;
const y=1;
  • Keywords should never be used as varibale name or constant name.JavaScript has more than 25+ keywords which we never use as variable name or constant name. In the code below, ‘public’ and ‘new’ are keywords, so you should not use them

Do not name like this:

var public=0;
const new=1;
  • The first character can be an alphabet (lowercase or uppercase), underscore (_), or dollar ($) character. Whenever you create a variable or a constant the first character can be an alphabet, it may be uppercase or lowercase also you can use underscore or dollar.
  • The first character should not be a number in a variable or constant name. In the above code, player1 and row2 are valid names but 1stplayer and 2ndrow is not valid name.

Ex:

var 1stPlayer=1;// player1
const 2ndrow=30; // row2
  •  All subsequent characters can be letters, numbers, or underscores.
  • No special characters are allowed except an underscore or dollar. Even you cannot use space characters as a name in javascript.
  • More than one successive underscore or dollars should not be used. You should not use “__name” or “$$height“, like this type of name.
  • Variable names and constant names are case sensitive, which means JS treates lowercase and uppercase alphabets completely different.In the below example, the num and Num are different variable names.
    Ex:

    var num, Num;
  • White space is not allowed while declaring a variable or constant name.
  • Use camel case naming convention for naming constant or variable. i.e.If a variable name contains a single word, all letters must be lowercase, and if a variable name contains more than one word, all letters of the first word must be lowercase, and all letters must be lowercase. The first letters of words should be in uppercase and all subsequent letters in lowercase.
READ ALSO  What is this Keyword in JavaScript With Example

Ex:

var player1Score=0;

In the above code is the camel case variable name. There are two words we have player and score, In the first word all characters are in a small letter the second word the first character is in uppercase, and all other characters are in lowercase, that’s what the camelcase naming convention says.

  • Whereas while naming a constant, all characters should be in upper case letters, if there is more than one word in a constant, then words should be separated by an underscore character.
    Ex:

    const CANWAS_WIDTH=300, CANWAS_HEIGHT=400;
  • In the above code, you can see two constants are here, both constants all characters are in uppercase. If there is more than one word we should separate them by using underscore(_) that’s what I have done here.
  • No two variables or constants should have same name in the same scope. Do not create two variables or constants with the same name as in the example below example.

Ex:

var num, num;
const row, row;
  • Always initialize a variable and constant, when they are declared.

Leave a Reply