So in any language, there is a set of rules for naming a variable, today we will see about some of those rules or otherwise called naming conventions of a variable.
Naming Conventions:
- When it comes to alphabets we can make use of both upper and lower case (eg: a-z, A-Z).
- When it comes to numbers we can use from (eg: 0-9).
- When it comes to special characters we can make use of two characters one is the Dollar($) sign and the other one is the Underscore(_).
Examples:
employee
customer
employee2
$amount
_info
Example #1:
Normally you have to keep in mind that a variable should not contain any whitespaces (tabs or spaces).
first name //not allowed
user profile //not allowed
Example #2:
A variable cannot begin with a number.
1user //not allowed
10products //not allowed
Example #3:
Reserved keywords are also strictly prohibited to be used as a variable naming convention. These are all the words that are already available in the language which cannot be used for custom purposes.
Array
function
Object
Example #4:
Always use meaningful names to be much ease for the coding environment and anyone who views the code from the non-technical area. All variables are case sensitive which means we have to be precise when calling the variable in the field of case. You cannot name a variable val and call it Val.
use user instead of u
use orderItems instead of oI
employee instead of e
Example #5:
Javascript uses camel casing that's a style that has been adapted in javascript. Camel Case means the first word will be lowercase and the second part of the word starts in upper case.
firstName
userInfo
orderDetails
finalInput