Menu
Posted At Sun, Oct 15, 2023 3:15 PM

JavaScript Naming Rules

Listen | 2 min read

Table of Contents

1. Introduction to JavaScript Naming Rules
2. Valid Characters in JavaScript Identifiers
3. Rules for Naming Variables
- Case sensitivity
- Starting with letters, underscores, or dollar signs
- Containing letters, digits, underscores, or dollar signs
- Reserved words and keywords
4. Best Practices for Naming
5. Conclusion

1. Introduction to JavaScript Naming Rules

JavaScript naming rules ensure clean, readable, and error-free code. This article dives into the guidelines and conventions for naming variables, functions, and other identifiers in JavaScript.

2. Valid Characters in JavaScript Identifiers

JavaScript allows a specific set of characters in identifiers, ensuring consistency in naming. Identifiers can contain letters, digits, underscores, and dollar signs.

Example:

// Valid identifiers
let myVar;
let user_age;
let _counter;
let $totalAmount;

// Invalid identifiers (non-compliant characters)
let my-Var; // Hyphens are not allowed
let user@age; // @ symbol is not allowed

3. Rules for Naming Variables

- Case Sensitivity
: JavaScript is case-sensitive.

Example:

let myVar;
let myvar;
// These are two distinct variables.

- Starting with Letters, Underscores, or Dollar Signs: Identifiers must start with a letter, underscore, or dollar sign.

Example:

let variableName;  // Valid
let _privateVar;   // Valid
let $price;        // Valid
let 1invalidVar;   // Invalid (starts with a digit)

- Containing Letters, Digits, Underscores, or Dollar Signs: Identifiers can contain letters, digits, underscores, or dollar signs.

Example:

let variableName;   // Valid
let var123;          // Valid
let _private_var;    // Valid
let $totalAmount;   // Valid

- Reserved Words and Keywords: Avoid using reserved words and keywords as identifiers.

Example:

let if; // Invalid (if is a reserved keyword)
let class; // Invalid (class is a reserved keyword)

4. Best Practices for Naming
Choose meaningful and descriptive names for variables and functions.

Example:

let userName;      // Descriptive name for a user's name
let calculateTotal; // Descriptive name for a function that calculates totals

Use camelCase for variable and function names.

Example:

let myVariableName;
let calculateTotalAmount;

Capitalize constructor functions for better distinction.

Example:

function Person(name) {
  this.name = name;
}

const john = new Person('John');

Avoid single-letter variable names unless used in very local contexts.

Example:

// Use meaningful names
let itemCount;

// Limit single-letter names to short-lived variables
for (let i = 0; i < itemCount; i++) {
  // i is acceptable here due to its localized usage.
}

Maintain consistency in naming conventions throughout your codebase.

5. Conclusion

Mastering JavaScript naming rules is essential for writing maintainable and bug-free code. By following the outlined rules and best practices, you can enhance the readability and quality of your JavaScript projects.

Next one is learning about JavaScript Variable Scopes.

Click this link! 👇
JavaScript Variable Scopes.

1453 1

Comments