Conventions and Rules for naming Variables

As we discussed previously, variables are instrumental in any programming language and without variables, I would have to change the value everywhere manually. But with variables everywhere we reference the variable, it will automatically get adjusted to the new and updated value. For a detailed introduction of variables, you can go through this post.

Javascript Variable name Convention

The convention and rules for naming javascript variables are the most important aspects that we should never ignore. After all, in good coding practice, we should not give random names to variables.

If you are following along the course in the sequence, you might have come across me naming a variable firstName. This way of naming is called camelCase. camelCase means that whenever I have multiple words in a variable name, I write the first word starting with the lower case, and then all the following words will also start with capitals.

// One word, so no need to worry about capitalisation
let hero = 'Naruto';

// Two words, first and name;
let firstName = 'Akhil'; // second word, name; should sart with Capital
let secondName = 'Naidu';

// Multiple Words, "his favourite anime"
let hisFavouriteAnime = 'Death Note';
This is an example of camelCase variable notation.

The camelCase notation is a standard variable notation in the javascript world, but other ways of naming variables exist.

// This type of notation is popular in programming languages like Ruby
let hero = 'Naruto'

let first_name = 'Akhil';
let second_name = 'Naidu';
let his_favourite_anime = 'Death Note';
You can use whatever notation you feel like, but remember that camelCase is a standard in javascript and, if possible, prefer camelCase.

In the majority of the sample codes or documentation, you will find people are using camelCase, so it is always better to stick with such a notation. So, that's a convention on "How we can name variables in javascript", but javascript also has some rules while naming a variable.

Rules of naming a Variable

  • We cannot use integers as variables.
  • We cannot start a variable with an integer.
  • Variable names can only contain letters, numbers, underscores, or the dollar sign.
  • We cannot use a reserved javascript keyword like new, functionas a variable
  • Even though name is a reserved js variable, we can still use it.

Other Conventions, but to be considered as Rules

  • We should not start a variable with an uppercase letter
  • Variables that are all in Uppercase are reserved for constants that we know will never change
  • Make sure that the variable names are descriptive
// You cannot name your variables as mentioned below

let 3 = 'number'; // number cannot be a variable
let 3years = 3; // cannot start with a number
let akhil&naruto = 'friends' // & cannot be used
let new = 27; // "new" is a javascript reserved variable
let function = 'Should Attend'; // "function" is reserved js variable
All such illegal variables will result in a Uncaught SyntaxError: Unexpected Token
// "name" is a reserved js variable but will work
let name = 'Itachi'; // this is not illegal variable
// But in some cases this might fail, so it is better to avoid

// cannot start with an uppercase
let FirstName = 'Akhil'; // This is not an illegal, but a convention
// We will use such variables in specific case in js.
// Which is Object Oriented Programming

let PI = 3.1415; // Not illegal, but a convention 

let myFirstCourse = 'Hacking'; // Try to be descriptive like this
let myCurrentCourse = 'JavaScript'; // being descriptive is good

// Not descriptive and avoid such variable names
let course1 = 'Hacking'; 
let course2 = 'JavaScript';
These are not illegal variables but some conventions; even most of the coding editors will respond accordingly.

These rules and conventions are fundamental to write cleaner code. Any developers code should be straightforward to understand. With these rules and patterns, we can understand what value the variable is holding by reading the variable's name.

Loading comments...
You've successfully subscribed to Leewardslope
Great! Next, complete checkout to get full access to all premium content.
Error! Could not sign up. invalid link.
Welcome back! You've successfully signed in.
Error! Could not sign in. Please try again.
Success! Your account is fully activated, you now have access to all content.
Error! Stripe checkout failed.
Success! Your billing info is updated.
Error! Billing info update failed.