01 / 20
aB

Naming & Formatting
Conventions

Master camelCase and consistent formatting to write JavaScript that is readable, professional, and bug-resistant.

JavaScript

What Are Naming Conventions?

A naming convention is an agreed-upon set of rules for choosing the names of variables, functions, constants, and other identifiers in your code. Think of it as a shared language that every developer on a team speaks fluently.

Why Conventions Matter

Readability
Consistent names let you scan code quickly without decoding each identifier.
Collaboration
Teams can merge code seamlessly when everyone follows the same style.
Fewer Bugs
Descriptive names reduce confusion, making logic errors easier to spot.

Formatting vs Naming

Formatting
How your code looks: indentation, spacing, brace placement, line length. Makes structure visible at a glance.
Naming
What you call things: variable names, function names, constants. Communicates meaning and intent.

The camelCase Rule

getUserName 1st word lowercase 2nd word Capital start 3rd word Capital start starts lowercase

JavaScript's Four Naming Styles

Style Example Used For
camelCase myVariable Variables, functions, methods
PascalCase MyClass Classes, constructors
SCREAMING_SNAKE MAX_SIZE Constants
snake_case my_var Not standard in JS

camelCase vs PascalCase

camelCase userName getTotalPrice isLoggedIn variables, functions PascalCase UserProfile ShoppingCart EventHandler classes, constructors vs

Formatting Essentials

1
Use 2-space indentation (or 4 spaces): pick one and stick with it throughout your project.
2
End statements with semicolons: while optional in JS, they prevent ambiguous parsing.
3
Place opening braces on the same line as the statement (if (x) {).
4
Add spaces around operators: write x + y, not x+y.
5
Keep lines under 80 characters: break long expressions for readability.

How to Apply the Rules

1
Describe the purpose: Think of what the variable or function does in plain English words.
2
Split into words: Break that description into distinct words (e.g., "total item count").
3
Lowercase the first word: "total" stays lowercase.
4
Capitalize subsequent words: "item" becomes "Item", "count" becomes "Count".
5
Join them together: totalItemCount. No spaces, no underscores.

Example: Variable Naming

// Bad: vague, inconsistent let x = 25; let user_age = 17; let TotalPrice = 49.99; // Good: descriptive, camelCase let studentAge = 25; let userAge = 17; let totalPrice = 49.99;

Example: Function Naming

// Bad function calc(a, b) { return a * b; } // Good: verb + noun, camelCase function calculateArea(width, height) { return width * height; }

Booleans & Constants

// Booleans: start with is, has, can, should let isLoggedIn = true; let hasPermission = false; let canEdit = true; // Constants: SCREAMING_SNAKE_CASE const MAX_RETRY_COUNT = 3; const API_BASE_URL = "https://api.example.com"; const DEFAULT_TIMEOUT = 5000;

Putting It All Together

const TAX_RATE = 0.08; function calculateTotalWithTax(itemPrice, quantity) { const subtotal = itemPrice * quantity; const taxAmount = subtotal * TAX_RATE; const totalPrice = subtotal + taxAmount; return totalPrice; }

Common Mistakes

✕ Single-Letter Names
Using x, d, or t outside of short loops. These tell the reader nothing about meaning.
✕ Mixing Styles
Writing user_name on one line and userAge on the next. Pick camelCase and use it everywhere.
✕ Misleading Names
Naming a variable userList when it holds a single object, or temp that persists for 200 lines.

Quick Reference Table

What Convention Example Anti-Example
Variable camelCase itemCount ItemCount
Function camelCase getUser get_user
Boolean is/has + camelCase isActive active
Constant SCREAMING_SNAKE MAX_SIZE maxSize
Class PascalCase UserProfile userProfile

Question 1 of 5

Which of the following is the correct camelCase name for a variable that stores a user's first name?

Question 2 of 5

You want to define a constant that holds the maximum number of login attempts. What is the correct way?

Question 3 of 5

Which function name follows JavaScript conventions for a function that checks if a user is an adult?

Question 4 of 5

You see the name ShoppingCart in a JavaScript file. What does the PascalCase convention tell you about it?

Question 5 of 5

Which of the following code snippets demonstrates correct formatting according to JavaScript conventions?