camelCase vs PascalCase: What's the Difference and When to Use Each
If you've ever wondered whether to name a variable getUserData or GetUserData — you're choosing between camelCase and PascalCase. Both are widely used in programming, but they apply to very different contexts. Here's a complete breakdown of when to use each, with real-world examples from major languages and frameworks.
The Core Difference
camelCase (lowerCamelCase)
- First word lowercase
- Each subsequent word capitalized
- Example:
getUserName - Common for: variables, functions
PascalCase (UpperCamelCase)
- Every word capitalized
- Including the first word
- Example:
GetUserName - Common for: classes, components
const userName = "Alice";
function getUserById(id) { ... }
let isLoggedIn = true;
// PascalCase — every word capitalized
class UserProfile { ... }
interface ApiResponse { ... }
type ButtonProps = { ... }
Language-by-Language Conventions
JavaScript and TypeScript
JavaScript has arguably the most nuanced conventions of any popular language:
- camelCase: variables, functions, object properties, parameters
- PascalCase: classes, constructor functions, React components, TypeScript interfaces and types
- SCREAMING_SNAKE_CASE: constants (e.g.,
MAX_RETRY_COUNT)
const maxRetries = 3; // camelCase variable
function fetchUserData(userId) { ... } // camelCase function
class UserService { ... } // PascalCase class
const Button = () => <button>...</button>; // PascalCase component
interface UserData { id: number; name: string; } // PascalCase interface
Python
Python's PEP 8 style guide is explicit:
- snake_case: variables, functions, methods, modules
- PascalCase: classes
- SCREAMING_SNAKE_CASE: constants
Python does not use camelCase by convention — it's considered non-Pythonic.
user_name = "Alice" # snake_case
def get_user_by_id(user_id): # snake_case
class UserProfile: # PascalCase
Java and C#
Both Java and C# share similar conventions:
- camelCase: variables, method parameters, local variables
- PascalCase: classes, methods (in C#), interfaces, enums
Note: Java uses camelCase for method names (getUserName()) while C# uses PascalCase (GetUserName()).
Go
Go uses capitalization for a different purpose — to control visibility (exported vs. unexported):
- PascalCase: exported (public) identifiers — functions, types, variables accessible outside the package
- camelCase: unexported (private) identifiers — accessible only within the package
This is a unique and elegant use of case conventions — capitalization literally encodes access control.
All Four Naming Conventions Compared
| Convention | Example | Used For | Languages |
|---|---|---|---|
camelCase | getUserData | Variables, functions | JS, Java, Swift, Kotlin |
PascalCase | UserProfile | Classes, types, components | C#, JS/TS, Java, Go (public) |
snake_case | user_name | Variables, functions, DB columns | Python, Ruby, SQL, PHP |
kebab-case | user-name | CSS classes, HTML attrs, URLs | CSS, HTML, REST APIs |
React and Modern Frontend: A Special Case
In React, the naming convention choice directly affects functionality:
- PascalCase components: React requires component names to start with a capital letter.
<UserCard />works;<userCard />is treated as an HTML element, not a component. - camelCase props: All prop names use camelCase:
onClick,className,onChange,onSubmit - camelCase event handlers:
handleClick,handleSubmit,onUserSelect - PascalCase context/hooks (custom):
UserContext,useUserData(note: hooks start with "use" + camelCase)
API Design and JSON
In REST API design and JSON payloads, camelCase is the dominant convention in JavaScript ecosystems, while snake_case is common in Python and Ruby backends. This mismatch is why serialization libraries often handle automatic conversion.
- JSON response (camelCase):
{ "userId": 1, "firstName": "Alice" } - JSON response (snake_case):
{ "user_id": 1, "first_name": "Alice" }
GraphQL typically uses camelCase for field names following JavaScript conventions.
Quick Decision Guide
- Naming a JavaScript/TypeScript variable or function? → camelCase
- Naming a class, interface, or React component? → PascalCase
- Writing Python code? → snake_case (not camelCase)
- Writing CSS class names? → kebab-case
- Writing a URL slug or HTML attribute? → kebab-case
- Writing a database column name? → snake_case
- Writing a constant value? → SCREAMING_SNAKE_CASE