#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JavaScript Fundamentals

Coding convention

A coding convention is an agreed set of rules for how code is written: how names are formed, how code is indented, where semicolons go and how files are organised. Conventions do not change what a JavaScript program does, but they make code easier to read, review and fix, especially when several people work on the same project or when you return to your own code months later.

Naming things clearly

JavaScript has a few widely followed naming patterns:

KindStyleExample
Variables and functionscamelCasetotalPrice, getUserName()
Classes and constructorsPascalCaseShoppingCart, new Date()
Fixed configuration constantsUPPER_SNAKE_CASEMAX_RETRIES, API_URL
BooleansQuestion-like prefixisActive, hasAccess, canEdit
File nameskebab-case or matching the exportuser-profile.js

Names should say what a value means, not how it is stored. daysUntilExpiry tells a reader far more than d or tempNum. Functions usually start with a verb, such as calculate, fetch or format, because they do something.

Before and after

// Hard to read
var x=function(a,b){if(a>b){return a}else{return b}}
var Data=[1,2,3];var flag=true

// Following common conventions
const getLarger = (first, second) => {
  if (first > second) {
    return first;
  }
  return second;
};

const scores = [1, 2, 3];
let isVisible = true;

The second version uses const and let instead of var, descriptive names, spaces around operators, consistent indentation, braces on every if and semicolons at the end of statements.

Layout and formatting habits

  • Indent consistently, usually two spaces in JavaScript projects. Never mix tabs and spaces.
  • Use const by default and let only when a variable is reassigned. Avoid var, whose function scope causes confusing bugs.
  • Always use braces for if, else and loops, even for one line, so adding a second line later does not silently change the logic.
  • Use strict equality === and !== to avoid surprising type conversions.
  • Pick one quote style, single or double, and use template literals for strings that include variables.
  • Keep lines short, roughly 80 to 100 characters, so code fits side by side in reviews.

Comments that earn their place

Good comments explain why, not what. // add 1 to i next to i++ adds noise. // API counts pages from 1, not 0 saves the next developer an hour. If you feel a block of code needs a long explanation of what it does, consider renaming variables or splitting it into a well-named function instead.

The trap of inconsistency

The real danger is not choosing the "wrong" style; it is mixing styles. A file where half the functions use camelCase and half use snake_case, or where indentation jumps between two and four spaces, makes every reader slow down. Another subtle trap is leaving out semicolons without knowing the rules for automatic semicolon insertion. A line starting with ( or [ can join the previous line and break the code. If you skip semicolons, learn those rules; otherwise, use them everywhere.

Let tools enforce the rules

Arguing about spaces in code reviews wastes time. Formatters such as Prettier rewrite code into one consistent layout every time you save. Linters such as ESLint flag problems like unused variables, accidental globals or == comparisons. Many teams adopt a published style guide and let these tools apply it automatically.

Habits to build now

Use meaningful camelCase names, const first, strict equality and braces on every block. Write comments that explain reasons. Above all, stay consistent within a project, and set up a formatter and linter so the conventions are applied without effort.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course