Historical Context & Motivation
From the earliest days of computing, programmers realized that code is read far more often than it is written. When teams of developers work on the same project, inconsistent naming turns collaboration into a frustrating puzzle. Naming conventions — agreed-upon rules for how you name variables, functions, and other identifiers — emerged as a solution. In JavaScript specifically, the convention known as camelCase became the dominant standard, shaping how millions of developers write code every day.
ITMPRC. Readability suffered enormously, and programmers began seeking better approaches.max_value). This made multi-word names readable for the first time.getElementById and addEventListener) cemented the pattern.The central question this lesson addresses is straightforward but critical: How should you name things in JavaScript so that your code is clear, consistent, and easy for others (and your future self) to understand? The answer lies in mastering a small set of conventions that the entire JavaScript community follows.
Core Principles of Naming & Formatting
Good naming is not about following arbitrary rules — it is about communicating intent. When you read let x = 5; versus let studentAge = 5;, the second version tells you what the data means. The following principles guide every naming decision you will make in JavaScript.
Be Descriptive
totalPrice instead of tp. A reader should understand what a variable holds without hunting for context.Use camelCase for Variables & Functions
firstName, calculateGrade. This is the standard JavaScript convention for most identifiers.Use PascalCase for Classes
ShoppingCart, UserProfile. This visually distinguishes constructors from regular functions.Use SCREAMING_SNAKE_CASE for Constants
MAX_ATTEMPTS, API_BASE_URL. This signals "do not change this value."Be Consistent Throughout
user_name and userName in the same project confuses readers and introduces bugs.Visual Guide to Naming Styles
The diagram below shows how the same multi-word phrase — "get user age" — gets formatted under each major naming convention used in programming. Notice how camelCase visually creates "humps" like a camel's back, which is where the name comes from.
Notice that each convention has a specific purpose. The style you choose immediately tells a reader what kind of identifier they are looking at. A variable called shoppingCart is clearly an instance or a plain value, while ShoppingCart signals a class that you would instantiate with new. This visual distinction makes code self-documenting.
How camelCase Works — Rules & Patterns
The mechanics of camelCase are simple, but the details matter. Understanding the precise rules helps you apply them automatically in every line of code you write.
Rule 1: Start with a Lowercase Letter
The first word of a camelCase identifier is always entirely lowercase. This distinguishes it from PascalCase, where the first letter is capitalized. For example, totalScore starts with a lowercase t, not an uppercase T.
Rule 2: Capitalize the First Letter of Each Subsequent Word
Every word after the first begins with a capital letter, and the remaining letters are lowercase. The phrase "number of students" becomes numberOfStudents. No spaces, hyphens, or underscores separate the words — the capital letters serve as the visual word boundaries.
Rule 3: Choose Meaningful Words
Conventions dictate the format, but you still choose the words. A well-named variable answers the question "what does this hold?" without requiring extra comments. Compare let d = new Date(); with let currentDate = new Date(); — the second version needs no explanation.
Common Patterns for Different Identifier Types
| Identifier Type | Convention | Example | Tip |
|---|---|---|---|
| Variable | camelCase (noun) | studentName | Use nouns that describe the stored data |
| Function | camelCase (verb + noun) | calculateAverage | Start with a verb: get, set, calculate, is, has |
| Boolean | camelCase (is/has prefix) | isLoggedIn | Prefix with is, has, can, or should |
| Array | camelCase (plural noun) | userNames | Use plural form to signal a collection |
| Class | PascalCase (noun) | GameCharacter | Capitalize every word including the first |
| Constant | SCREAMING_SNAKE_CASE | MAX_LIVES | All uppercase, underscores between words |
const prevents reassignment, but not all const values use SCREAMING_SNAKE_CASE. Reserve the screaming style for hard-coded configuration values that are truly constant across your entire program, like const API_KEY = "abc123". Regular const variables that just happen not to be reassigned (like const user = getUser()) should still use camelCase.Formatting Conventions Beyond Names
Naming is only half the story. Formatting conventions govern how your code is visually structured: indentation, spacing, brace placement, and line length. Consistent formatting makes code scannable at a glance, just like how consistent paragraph formatting makes an essay easier to read.
Key Formatting Rules in JavaScript
- Indentation: Use 2 spaces (most common in JavaScript) or 4 spaces — but pick one and use it everywhere. Never mix tabs and spaces.
- Semicolons: Always include semicolons at the end of statements. While JavaScript has automatic semicolon insertion, relying on it can cause subtle bugs.
- Braces: Place the opening brace on the same line as the statement (not on a new line). Always use braces with
if,for, andwhile— even for single-line bodies. - Spacing: Add spaces around operators (
x + y, notx+y), after commas, and after keywords likeifandfor. - Line length: Keep lines under 80–100 characters. Break long expressions across multiple lines for readability.
Worked Example: Refactoring Bad Names
Let's walk through refactoring a piece of poorly named and formatted code into clean, conventional JavaScript. Imagine a classmate hands you this code and asks you to figure out what it does.
function f(a,b,c){var d=(a+b+c)/3;if(d>=90){return 'A'}else if(d>=80){return 'B'}else{return 'C'}}. This is a working function, but it is nearly impossible to understand at a glance. The function name f tells you nothing, and parameters a, b, c could be anything.f to calculateLetterGrade. This follows camelCase (starts lowercase, capitalizes subsequent words) and reveals the function's purpose.function calculateLetterGrade(...)testOneScore, testTwoScore, and testThreeScore. Each name clearly describes the data it holds.calculateLetterGrade(testOneScore, testTwoScore, testThreeScore)d holds the average. Rename it to averageScore and change var to const since the value is never reassigned.const averageScore = (testOneScore + testTwoScore + testThreeScore) / 3;function calculateLetterGrade(testOneScore, testTwoScore, testThreeScore) { const averageScore = (testOneScore + testTwoScore + testThreeScore) / 3; if (averageScore >= 90) { return "A"; } else if (averageScore >= 80) { return "B"; } else { return "C"; } }Naming Conventions Across Languages
Different programming languages prefer different naming conventions. Understanding these differences helps you switch between languages without carrying incorrect habits. The table below shows the preferred convention for variables and functions in several popular languages.
| Language | Variable/Function Style | Example |
|---|---|---|
| JavaScript | camelCase | getUserName |
| Python | snake_case | get_user_name |
| Java | camelCase | getUserName |
| C# | PascalCase (methods) | GetUserName |
| Ruby | snake_case | get_user_name |
| CSS | kebab-case | user-name-input |
my-variable) in CSS class names and HTML attributes. JavaScript does not allow hyphens in identifier names because the minus sign (-) is interpreted as subtraction. So my-variable would be read as "my minus variable" — a syntax error or unexpected math operation.Linters & Formatters — Automating Conventions
As your projects grow, manually enforcing conventions becomes impractical. This is where linters and formatters come in. These automated tools check your code for convention violations (linting) and automatically fix formatting issues (formatting), just like a spell-checker and grammar-checker work together in a word processor.
| Feature | ESLint (Linter) | Prettier (Formatter) |
|---|---|---|
| Purpose | Catches naming issues, logic bugs, and style violations | Automatically reformats code (indentation, spacing, line breaks) |
| camelCase enforcement | Yes — the camelcase rule flags violations | No — Prettier does not check names |
| Fixes automatically? | Some rules auto-fix; naming requires manual changes | Yes — reformats on save |
| Best used | For code quality, naming rules, and catching bugs | For consistent visual formatting across a team |
In professional development teams, ESLint and Prettier are typically used together. ESLint handles naming and logic rules, while Prettier handles visual formatting. Many code editors, like VS Code, integrate both tools so that your code is automatically checked and formatted every time you save a file. As you move into more advanced courses or begin contributing to open-source projects, you will encounter style guides — comprehensive documents (like the Airbnb JavaScript Style Guide or Google's JavaScript Style Guide) that define every convention a team follows. These style guides are then enforced through linter configurations.
.eslintrc.json file in your project root with {"rules": {"camelcase": "error"}} and ESLint will underline any non-camelCase variable names in red — instant feedback as you code!Practice Problems
x is generally a poor choice in JavaScript. Under what specific circumstance might a single-letter variable name be acceptable?const Max_Speed = 120; var user_name = "Ada"; function GetAge(birth_year){ let Current_Year = 2025; return Current_Year-birth_year }Lesson Summary
JavaScript naming and formatting conventions form the foundation of readable, professional code. The primary convention is camelCase — starting with a lowercase letter and capitalizing each subsequent word — used for variables, functions, methods, and parameters. Classes and constructors use PascalCase, while true constants use SCREAMING_SNAKE_CASE. Good names are descriptive — they reveal intent without requiring comments.
Beyond naming, formatting conventions include consistent indentation (2 spaces), spacing around operators, same-line braces, and semicolons at statement ends. Tools like ESLint and Prettier automate enforcement, catching violations as you write. Mastering these conventions is not about memorizing rules — it is about building the habit of writing code that communicates clearly to every developer who reads it.