Naming Conventions

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 1 — What is camelCase?

In JavaScript, we follow a naming convention called camelCase for variables. But what does that mean?

- The first word starts with a lowercase letter - Every following word starts with an uppercase letter - No spaces or special characters between words

let codemonName = "Aquachu"; let playerLevel = 15; console.log(codemonName); console.log(playerLevel);

Notice how each variable name looks like a camel's humps? 🐫 The uppercase letters create "humps" in the middle of the word!

Why does it matter?

Using camelCase makes your code easier to read and understand, especially when working with other developers. It's a standard convention in the JavaScript community, so following it makes your code look professional and consistent.

Exercise 2 — Rules for Valid Variable Names

Before we talk about style, let's understand what JavaScript actually allows as variable names. There are strict rules you must follow:

Valid names must: - Start with a letter, $ or _ - Contain only letters, numbers, $ or _ - NOT start with a number - NOT contain spaces or special characters (except $ and _) - NOT be a reserved keyword (like let, const, if, etc.) - BONUS: Try picking names that describe the value they hold!

// ✅ VALID names let codemonName = "Pyrochu"; let _privateValue = 42; let $specialVar = 100; let name123 = "Valid";// ❌ INVALID names (these would cause errors) // let 123name = "Invalid"; // Cannot start with number // let codemon-name = "Invalid"; // Cannot use hyphens // let codemon name = "Invalid"; // Cannot have spaces // let let = "Invalid"; // Cannot use reserved keywords

Reserved Keywords

JavaScript has special words that are reserved for the language itself, like let, const, if, for, function, etc. You cannot use these as variable names because they already have special meanings in JavaScript!

Exercise 3 — Spot the Valid camelCase

Which of the following variable names follows BOTH JavaScript rules AND camelCase convention?

Options
• codemonHealthPoints
• CodemonHealthPoints
• codemon_health_points
• codemon-health-points

Hint : Remember: camelCase starts with a lowercase letter, then each new word starts with an uppercase letter. No spaces, hyphens, or underscores!

Exercise 4 — Convert to camelCase

Convert the following variable names to proper camelCase format:

Hint : In camelCase, remove underscores and capitalize the first letter of each word EXCEPT the first word. Example: user_name becomes userName

Exercise 5 — Meaningful Variable Names

Beyond following camelCase, variable names should be descriptive and meaningful. Look at this code:

// Storing a Codémon's name let x = "Aquachu";

Which variable name would be MORE meaningful and clear?

Options
• codemonName
• x
• n
• theNameOfTheCurrentCodemonThatWeAreUsing

Hint : Good variable names are descriptive enough to understand what they store, but not so long that they become difficult to read. Strike a balance between clarity and brevity!

Exercise 6 — Common Naming Mistakes

Let's identify common mistakes in variable naming. Match each bad variable name with the reason why it's problematic:

Items to match
• x
• CodemonName
• codemon name
• theNameOfTheCurrentCodemonThatIsInBattle
Definitions
• Not descriptive at all
• Starts with uppercase (not camelCase)
• Contains a space
• Way too long

Exercise 7 — Fix the Variable Names

The following code has poorly named variables. Replace them with proper camelCase names that are meaningful:

Hint : Think about what each variable stores. Use descriptive names in camelCase. For booleans, it's common to start with is, has, or can.

Back to the course