Javascript Β· Variables
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:
let ____ = 100; // player_score let ____ = "Aquachu"; // first_codemon
Options: playerScore player_score PlayerScore firstCodemon first_codemon FirstCodemon
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:
let ____ = "Florachu"; // n let ____ = 50; // l console.log(codemonName, codemonLevel);
Options: codemonName n name codemonLevel lvl level
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