JavaScript variable name composition rules: start with letters, underscores, or dollar signs; can contain letters, numbers, underscores, and dollar signs; are case-sensitive; cannot use reserved keywords; cannot contain spaces.
Composition of JavaScript variable name
JavaScript variable name is the name used to identify the variable. It follows specific rules to form:
-
must start with a letter, underscore, or dollar sign: Variable names cannot start with a number.
-
Can contain letters, numbers, underscores, and dollar signs: Variable names can contain any of these characters.
-
Case sensitivity: JavaScript variable names are case-sensitive. For example, "age" and "AGE" are different variables.
-
Cannot use reserved keywords: JavaScript reserves certain keywords, such as "var", "function" and "for". These keywords cannot be used as variable names.
-
Cannot contain spaces: Variable names cannot contain spaces.
Best Practices
Here are some best practices for JavaScript variable naming:
-
Use what makes sense Name: Variable names should clearly reflect the value held by the variable.
-
Use camel nomenclature: Use camel nomenclature when concatenating multiple words to form a variable name. For example, "firstName", "customerAddress".
-
Avoid abbreviations and numbers: Variable names should be as concise as possible, but still easy to read. Avoid abbreviations and numbers unless they are very meaningful.
-
Consider the scope of the variable: Variable names should reflect the scope of the variable. For example, local variables can start with a lowercase letter, while global variables can start with an uppercase letter.
The above is the detailed content of The composition of variable names in javascript. For more information, please follow other related articles on the PHP Chinese website!