Menu
Coddy logo textTech

Naming Conventions

Part of the Fundamentals section of Coddy's JavaScript journey — lesson 8 of 77.

In JavaScript, there are certain rules and conventions for naming variables:

  • Variable names are case-sensitive, meaning myVariable and myvariable are considered different.
  • Variable names can only contain letters (a-z, A-Z), numbers (0-9), underscores (_), and dollar signs ($).
  • Variable names must start with a letter, underscore, or dollar sign. They cannot begin with a number.
  • Certain words are reserved by JavaScript and cannot be used as variable names, such as let, const, function, if, else, etc.
  • It's a common practice to use camelCase for variable names, where each word except the first starts with a capital letter (e.g., myVariableName).

Here are some examples of valid and invalid variable names:

Valid:

let myVariable = 10;
let _name = "John";
let $count = 5;

Invalid:

let 1stPlace = "Invalid"; // Cannot start with a number
let my-variable = 3; // Cannot contain hyphens
let if = true; // Cannot use reserved words

Following these naming conventions makes your code more readable and helps avoid potential errors.

challenge icon

Challenge

Beginner

Create two variables:

  1. firstName and assign it the value "John"
  2. lastName and assign it the value "Doe"

Follow the JavaScript naming conventions.

Cheat sheet

JavaScript variable naming rules:

  • Variable names are case-sensitive
  • Can only contain letters, numbers, underscores (_), and dollar signs ($)
  • Must start with a letter, underscore, or dollar sign (not a number)
  • Cannot use reserved words like let, const, function, if, else
  • Use camelCase convention (e.g., myVariableName)

Valid variable names:

let myVariable = 10;
let _name = "John";
let $count = 5;

Invalid variable names:

let 1stPlace = "Invalid"; // Cannot start with a number
let my-variable = 3; // Cannot contain hyphens
let if = true; // Cannot use reserved words

Try it yourself

// Type your code below


// Don't change the line below
console.log(`firstName = ${firstName}, lastName = ${lastName}`)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals