Skip to content

Variable Naming Conventions

So, we just learned what variables are and why they’re needed in our code, but if you have paid attention to the code snippet in the previous section we had a very weird way of writing the variable.

const birthYear = 2002;

Hmm, if you look at this variable name birthYear, doesn’t it look a little weird? I mean why is Y in the year an uppercase?

Well that’s because it is easier to read. You can write it all in no caps like birthyear but then it becomes a little harder to read.

Okay not really! birthYear is a small word, so it’s not hard to read. Right? How about this next example?

const totalsalesinlastsixweeks = 300;

Is this easier to read? All lowercase no caps? Or what about this one?

const totalSalesInLastSixWeeks = 300;

I think this second one was more instant and readable right? Well this way of writing variables is known as Camel Case.

Camel case is a naming convention where multiple words are joined together without spaces, and each word after the first starts with a capital letter.

Example:


We have to create variable for number of sales this week.

So, first word number should be all lowercase: number
next word: Of
next word: Sales
next word: This
next word: Week

all the next words after the first ones have their first letter as uppercase.

So `camelCase` becomes : numberOfSalesThisWeek

See how much easier it is to read numberOfSalesThisWeek than numberofsalesthisweek!

Okay you might be wondering, yes the above examples are valid and yes it’s easier to read this way but why not we create a variable with each word separated by a space.

Like this:

const number of sales this week = 50 // WRONG ERROR ERROR ERROR

Spaces are not allowed when naming variables in JavaScript, in fact in virtually every language.

Now that you know why spaces aren’t allowed, let’s look at the formal rules for what is allowed when you name a variable in JavaScript.

Now that you know why spaces don’t work, here are the concrete rules for naming any variable in JavaScript—plus tips to keep your code clean and consistent.

  1. Start with a letter, dollar sign $, or underscore _

    // ✅ valid
    const _firstName = "Alice"
    const $total = 100
    const userAge = 25
    
    // ❌ invalid (starts with a number)
    const 2ndPlace = "Bob"
  2. Use only letters, numbers, $, and _

    // ✅ valid
    let score2 = 42
    const max_value = 10
    
    // ❌ invalid (hyphens and spaces not allowed)
    let max-value = 10
    let max value = 10
  3. Avoid reserved words JavaScript has built-in keywords (like for, if, while, class). Don’t name your variables the same:

        // ❌ confusing and may break
        const class = "Math101"
        let for = 5
    
        // ✅ pick something descriptive
        const courseName = "Math101"
        let loopCount = 5
  4. Case Sensitive uppercase or lowercase letters can make a big difference and can make two identically spelled variables completely different. These 3 variables myVar, MyVar and MYVAR, though have same spellings but are still considered different due to different casing of letters.

        let myVar = 1
        let MyVar = 2
        let MYVAR = 3
        console.log(myVar, MyVar, MYVAR) // 1 2 3
  5. Choose descriptive names So can you tell what does these variables means?

        let x = 3.14
        let r = 12
        // More Descriptive
        const PI = 3.14
        let radius = 12

In above code snippets, you can see that x and r tells nothing but PI and radius, tells us exactly what these variables are declared for!

✅ Good Examples (camelCase, readable, valid)

Section titled “✅ Good Examples (camelCase, readable, valid)”
const birthYear = 2002;
const totalSalesInLastSixWeeks = 300;
const numberOfSalesThisWeek = 50;

const _firstName = "Alice";
const $total = 100;
let score2 = 42;
const max_value = 10;

const courseName = "Math101";
let loopCount = 5;

const PI = 3.14;
let radius = 12;

❌ Bad Examples (hard to read, invalid or confusing)

Section titled “❌ Bad Examples (hard to read, invalid or confusing)”
const totalsalesinlastsixweek = 300;        // Hard to read
const 2ndPlace = "Bob";                     // ❌ Starts with number
let max-value = 10;                         // ❌ Hyphen not allowed
let max value = 10;                         // ❌ Space not allowed
const class = "Math101";                    // ❌ Reserved keyword
let for = 5;                                // ❌ Reserved keyword
let x = 3.14, r = 12;                        // Undescriptive
let myVar = 1;
let MyVar = 2;
let MYVAR = 3;

console.log(myVar, MyVar, MYVAR); // 1 2 3 (all different)

So now you know why we write variable names like totalSalesInLastSixWeeks instead of totalsalesinlastsixweeks, and why camelCase is the go-to style in JavaScript.

Choosing the right variable names might seem like a small thing, but it makes your code easier to read, easier to debug, and way more professional—especially when working with teams or preparing for interviews.

Next time you write a variable, don’t just make it work—make it clear.