Variable Naming Rules in JavaScript


TL ; DR:

  • In JavaScript, we use the camelCase convention to name our variables. It implies separating different words with capital letters, like this:

    let companyName = "Apple";
    let foundingYear = 1976;
    let isUnicorn = true;
    let numberOfEmployees = 137000;
    





Full lesson:

Variable names must consist ONLY of letters, numbers and underscores and they must start with a letter or underscore.

Bad variable names:

let 18spam;
let x.15;

If you try to create a variable with any of these names, JavaScript will throw a SyntaxError.


Naming conventions: camelCase and snake_case

Sometimes a variable name might consist of multiple words, for example if you want to store the first name of a person. You might be tempted to separate those names by whitespaces, like this:

let first name = "Kate";

This code will also throw a SyntaxError.

When you name your variables, you want to use one of these two conventions:

snake_case = separate different words with an underscore (_), like this:

let first_name = "Jane";
let last_name = "Doe";

camelCase = separate different words with capital letters, like this:

let firstName = "Jane";
let lastName = "Doe";

It's important to be consistent with the casing technique you use. Do not combine both of them in the same code, like this:

let first_name = "Jane";
let lastName = "Doe";

Case sensitive:

Variable names are case sensitive:

let name = "Kate";
let Name = "Andy";
let NAME = "George";
console.log(name, Name, NAME); // Output: Kate Andy George

Although JavaScript allow us to do this, it's always confusing and a bad coding practice to differentiate variable names just by case!


Mnemonic Variable Names:

Since we programmers are given a choice in how we choose our variable names, there is a bit of "best practice".

Sometimes we want them short like x and y, but sometimes we want them descriptive, especially if they are important.

Here is a great example:

let a = 25;
let b = 13;
let c = a * b;
console.log(c);

compared to:

let hours = 25;
let rate = 13;
let pay = hours * rate;
console.log(pay);

JavaScript understands and is happy with both of these pieces of code. But it's clearly which version makes your reader happier.


Assignment
Follow the Coding Tutorial and let's name some variables well!


Hint
Look at the examples above if you get stuck.


Introduction

In this lesson, we will explore the rules and best practices for naming variables in JavaScript. Proper variable naming is crucial for writing clear, maintainable, and error-free code. Understanding these rules will help you avoid common pitfalls and improve your coding efficiency.

Understanding the Basics

Variable names in JavaScript must adhere to specific rules:

For example, let companyName = "Apple"; is a valid variable name, while let 18spam; is not.

Main Concepts

There are two main naming conventions in JavaScript:

Consistency is key. Do not mix these conventions within the same codebase.

Examples and Use Cases

Consider the following examples:

let firstName = "Jane";
let lastName = "Doe";

These variables use camelCase, which is common in JavaScript. In contrast, snake_case might be used in other languages like Python.

Common Pitfalls and Best Practices

Avoid the following common mistakes:

Best practices include:

Advanced Techniques

As you become more experienced, you might encounter advanced naming techniques such as:

These techniques can help improve code readability and maintainability in larger projects.

Code Implementation

// Example of camelCase
let companyName = "Apple";
let foundingYear = 1976;
let isUnicorn = true;
let numberOfEmployees = 137000;

// Example of snake_case
let first_name = "Jane";
let last_name = "Doe";

// Avoid mixing conventions
let first_name = "Jane";
let lastName = "Doe"; // Not recommended

// Case sensitivity
let name = "Kate";
let Name = "Andy";
let NAME = "George";
console.log(name, Name, NAME); // Output: Kate Andy George

// Mnemonic variable names
let hours = 25;
let rate = 13;
let pay = hours * rate;
console.log(pay); // Output: 325

Debugging and Testing

When debugging variable names, ensure they are spelled correctly and follow the naming conventions. Use descriptive names to make debugging easier. Writing tests for your functions can help catch errors related to variable naming.

// Example test case
function calculatePay(hours, rate) {
    return hours * rate;
}

console.assert(calculatePay(25, 13) === 325, "Test case failed");

Thinking and Problem-Solving Tips

When naming variables, consider the following strategies:

Conclusion

Mastering variable naming rules in JavaScript is essential for writing clear and maintainable code. By following best practices and avoiding common pitfalls, you can improve your coding efficiency and reduce errors. Keep practicing and exploring further applications to enhance your skills.

Additional Resources