Naming & Formatting
Conventions
Master camelCase and consistent formatting to write JavaScript that is readable, professional, and bug-resistant.
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
Formatting vs Naming
The camelCase Rule
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
Formatting Essentials
How to Apply the Rules
Example: Variable Naming
Example: Function Naming
Booleans & Constants
Putting It All Together
Common Mistakes
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?