Back to JavaScript Fundamentals

JavaScript Naming Conventions

Naming variables, functions, classes, and constants in JavaScript can be a bit tricky. In this guide, we'll explore the best practices for naming variables, functions, classes, and constants in JavaScript.
5 min readLogan FordLogan Ford

Introduction to JavaScript Naming Conventions

Have you ever looked at someone else's code and felt completely lost? Or maybe you've returned to your own code after a few months and struggled to understand what's going on? The culprit might be poor naming conventions. In this guide, we'll explore how to write clearer, more maintainable JavaScript code through proper naming practices.

Why Naming Conventions Matter

Naming conventions are like the grammar rules of programming. They help us:

  • Make code more readable and self-documenting
  • Reduce cognitive load when reading code
  • Maintain consistency across projects
  • Collaborate more effectively with other developers

Variable Naming Best Practices

1. Use Descriptive Names

1// Bad examples
2const x = 'John Smith';
3const arr = ['apple', 'banana', 'orange'];
4const flag = true;
5
6// Good examples
7const fullName = 'John Smith';
8const fruits = ['apple', 'banana', 'orange'];
9const isActive = true;

2. Follow camelCase Convention

In JavaScript, we typically use camelCase for variables and functions:

1// Correct camelCase usage
2const firstName = 'John';
3const userAge = 25;
4const calculateTotalPrice = () => {
5// function body
6};
7
8// Avoid these patterns
9const first_name = 'John'; // snake_case
10const FirstName = 'John'; // PascalCase (reserved for classes)
11const firstname = 'John'; // lowercase

3. Boolean Variables

Boolean variables should ask a question:

1// Good boolean names
2const isLoggedIn = true;
3const hasPermission = false;
4const canEdit = true;
5
6// Poor boolean names
7const login = true;
8const status = false;
9const edit = true;

Function Naming Conventions

Functions should be named using verbs that describe their action:

1// Good function names
2function getUserData() { }
3function validateEmail() { }
4function calculateTotal() { }
5
6// Poor function names
7function data() { }
8function email() { }
9function total() { }

Event Handler Naming

For event handlers, use the prefix 'handle' or 'on':

1// React/Event handler naming
2const handleSubmit = (event) => {
3// handle form submission
4};
5
6const onUserClick = () => {
7// handle user click
8};

Class and Constructor Naming

Classes use PascalCase and should be nouns:

1// Good class names
2class UserProfile {
3constructor(name, age) {
4 this.name = name;
5 this.age = age;
6}
7}
8
9class ShoppingCart {
10constructor() {
11 this.items = [];
12}
13}
14
15// Poor class names
16class processData { // should be ProcessData
17// ...
18}
19
20class shopping_cart { // should be ShoppingCart
21// ...
22}

Common Naming Patterns

1. Constants

Use UPPERCASE with underscores for constants:

1const MAX_ITEMS = 100;
2const API_BASE_URL = 'https://api.example.com';
3const DEFAULT_THEME = 'light';

2. Private Variables

Use an underscore prefix for private variables:

1class User {
2constructor() {
3 this._privateData = 'sensitive';
4 this.publicData = 'visible';
5}
6}

3. Array Names

Use plural nouns for arrays:

1// Good array names
2const colors = ['red', 'blue', 'green'];
3const userNames = ['John', 'Jane', 'Bob'];
4const activeUsers = users.filter(user => user.isActive);
5
6// Poor array names
7const color = ['red', 'blue', 'green'];
8const userName = ['John', 'Jane', 'Bob'];

Practice Examples

Let's test your understanding with some practical examples:

1// Refactor these names to follow conventions
2const n = 'John Smith';
3const a = 25;
4const p = true;
5function calc(x, y) {
6return x + y;
7}
8
9// Solution
10const fullName = 'John Smith';
11const age = 25;
12const isActive = true;
13function calculateSum(firstNumber, secondNumber) {
14return firstNumber + secondNumber;
15}

Remember, good naming conventions are a skill that develops with practice. They might seem tedious at first, but they'll save you and your team countless hours of confusion and debugging in the long run.


Ready to master JavaScript and write cleaner, more professional code? Join TechBlitz for daily coding challenges, personalized learning paths, and a supportive community of developers. Start your journey today at TechBlitz.

Learn to code, faster

Join 810+ developers who are accelerating their coding skills with TechBlitz.

Share this article