Skip to main content

Command Palette

Search for a command to run...

JavaScript naming conventions

Published
โ€ข3 min readโ€ขView as Markdown
JavaScript naming conventions

JavaScript has a standard naming convention when naming variables, functions, and classes. Naming conventions improve code readability and make code maintenance much easier. When writing a program, you need to ask yourself whether your program will make sense tomorrow? Let's look at how we can name our programs below:

Variables

A JavaSript variable should be self-descriptive. You don't necessarily need to add a comment to document the variable. Variable names start with letters.

// variables should be self-descriptive

// bad
let value = Tracy;

// good
let firstName = Tracy;

We use camelCase when declaring variables where the leading character is in lowercase for example:

// variables should be written in camelCase

// bad
let firstname = Tracy;

// bad
let first_name = Tracy;

// bad 
let FIRSTNAME = Tracy;

// bad
let FIRST_NAME = Tracy;

// good
let firstName = Tracy;

Remember, JavaScript variables are case-sensitive.

Variables should not only be self-descriptive or human-readable but should also be concise for example:

// bad
let recordingPlayerScore = 0;

// good
let playerScore = 0;

Booleans

Booleans are distinguished when we use prefixes like is, are, or has.

// bad
let married = false;

// good
let isMarried = false;

Functions

For functions, use a verb as a prefix when declaring a function name. Examples of verbs are; post, push, add, fetch, get, etc. Using a verb makes the function self-descriptive. Functions are also written in camelCase.

// bad
function user(firstName, lastName) {
    return `${firstName} ${lastName}`;
}

// good
function getUser(firstName, lastName) {
    return `${firstName} ${lastName}`;
}

Class

Unlike other data structures, a JavaScript class is declared with a PascalCase:

class ZuriIntern {
    constructor(name, track, course) {
        this.name = name;
        this.track = track;
        this.course = course;
    }
}

let TracyCodes = new ZuriIntern('Tracy Nuwagaba', 'Backend', 'NodeJS');

Methods

Methods in JavaScript are also declared in camelCase. Just like functions, we add verbs as a prefix to make methods more descriptive.

class JavascriptDeveloper {
    constructor(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    getName() {
        return `${this.firstName} ${this.lastName}`;
    }
}

let TracyCodes = new JavascriptDeveloper('Tracy', 'Nuwagaba');

console.log(TracyCodes.getName());
// "Tracy Nuwagaba"

Constants

Constants are variables that do not change. Constants are written in UPPERCASE for example:

const PI = 3.14159265359;

An underscore is used if a variable has more than one word in its declaration.

const HOURS_IN_A_DAY = 24;

Private

If you find an underscore(_) in front of a variable, function, or method in JavaScript, then it is intended to be private. That means the private variable or method should be used internally and not outside of the file.

let creditCard = {
    _name: "Tracy",
    get name() {
        return this._name;
    },
    set name(value) {
        this._name = value;
    }
};

console.log(creditCard.name);
// "Tracy"

Conclusion

You are responsible for the majority of your names in your program. Make your program be easily read and understood by you and people in the future reading the code. Make sure you use meaningful descriptive names and be concise as possible if you can.

I hope you find this useful. Thanks for reading!!

Buy Me A Coffee

P

Nice article, Tracy Nuwagaba!

I think you truly capture the essence of the importance of naming when you mention we must make it easy for us and others to understand what the code is all about.

In that regard, I would advise people to look for style guides that are widely adopted, like this one by Google.

And I have my own contribution about how enums should be named. I hope it helps people to write better code, one variable name at a time ๐Ÿ˜‰

1
T

Thank you Phillippe

H

Thanks for writing this article.

T

Thanks Mehadi

F

Thanks for sharing Tracy!

1
T

Thanks Jerome

1
A

Nice article Tracy Nuwagaba, thanks for sharing

1
T

Thanks Samuel

1

More from this blog

Tracy's blog

27 posts

I am a software developer, passionate about technology and i enjoy writing about things i learn.