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
myVariableandmyvariableare 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 wordsFollowing these naming conventions makes your code more readable and helps avoid potential errors.
Challenge
BeginnerCreate two variables:
firstNameand assign it the value "John"lastNameand 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 wordsTry it yourself
// Type your code below
// Don't change the line below
console.log(`firstName = ${firstName}, lastName = ${lastName}`)This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Type Coercion7Bill Split Calculator
Welcome MessageCalculating The Tip And Total2Variables
NumbersStringBooleanNaming ConventionsEmpty VariablesRecap - Initialize VariablesConstants3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsComparison OperatorsStrict vs Loose EqualityRecap - Simple Math6Basic IO
OutputOutput with VariablesType Conversion - Part 1Type Conversion - Part 2Recap - Till 120Recap - True or False