Use camelCase for variable name.
Variable name should be noun.
let productPrice ;
let customerName ;
let bookTitle ;
let carMake ;
let employeeID ;
Add Unit In Your Variable Name
The practice of adding units to your variable names is a good coding convention that helps improve code readability and maintainability.
// Weight
let weightInKilograms ;
let weightInPounds ;
// Length
let lengthInMeters ;
let lengthInCentimeters ;
let lengthInInches ;
// Temperature
let temperatureInCelsius ;
let temperatureInFahrenheit ;
// Volume
let volumeInLiters ;
let volumeInGallons ;
// Speed
let speedInKilometersPerHour ;
let speedInMilesPerHour ;
// Time
let timeInSeconds ;
let timeInMinutes ;
let timeInHours ;
// Money
let moneyInDollars ;
let moneyInEuros ;
How to name a global variable?
Use prefix global
at start.
Use camelCase for variable name.
let globalProductPrice ;
let globalCustomerName ;
let globalBookTitle ;
let globalCarMake ;
let globalEmployeeID ;
How to name a private variable?
Use prefix private
at start.
Use camelCase for variable name.
class MyClass {
constructor ( ) {
this . _privateProductPrice = 0 ;
this . _privateCustomerName = "" ;
this . _privateBookTitle = "" ;
this . _privateCarMake = "" ;
this . _privateEmployeeID = "" ;
}
}
Use UPPER_CLASS for constant names.
Use _
Underscore for space between constant names.
const MAX_VALUE = 100 ;
const PI = 3.14159 ;
const API_ENDPOINT = "https://api.example.com/data" ;
const DEFAULT_TIMEOUT = 5000 ;
const COPYRIGHT_YEAR = 2022 ;
How to name a Global Constant?
Use GLOBAL
as prefix at start.
Use UPPER_CLASS for constant names.
Use _
Underscore for space between constant names.
const GLOBAL_API_KEY = "your_api_key_here" ;
const GLOBAL_BASE_URL = "https://api.example.com" ;
const GLOBAL_TIMEOUT_DURATION = 5000 ;
const GLOBAL_MAX_RETRY_ATTEMPTS = 3 ;
const GLOBAL_APP_VERSION = "1.0.0" ;
How to name a private constant?
Use _PRIVATE
as prefix at start.
Use UPPER_CLASS for constant names.
Use _
Underscore for space between constant names.
const _PRIVATE_MAX_VALUE = 100 ;
const _PRIVATE_PI = 3.14159 ;
const _PRIVATE_API_ENDPOINT = "https://api.example.com/data" ;
const _PRIVATE_DEFAULT_TIMEOUT = 5000 ;
const _PRIVATE_MAX_RETRY_ATTEMPTS = 3 ;
Use a clear and concise name that describes the variable's purpose or meaning.
Start the variable name with a verb (e.g., "is", "has", "can", "should") to indicate its boolean nature.
Use camelCase for variable names.
Avoid using generic names like "flag" or "status" unless they provide additional context.
Avoid using names that are already used for other types of variables.
// Example 1: Describing the variable's purpose
let isNumberEven ;
// Example 2: Using "is" to indicate a condition or state
let isUserLoggedIn ;
// Example 3: Using "has" to indicate the presence or existence of something
let hasValidEmail ;
// Example 4: Using "can" to indicate the ability or possibility of something
let canEditProfile ;
// Example 5: Using "should" to indicate a recommendation or expectation
let shouldDisplayWarning ;
Resource
How to write better function name?
Use nouns (plural): Arrays typically store collections of related data.
Start with a verb (optional): If the array undergoes transformations.
How to write better array name?
const shoppingCartItems = [ "apple" , "banana" , "milk" ] ;
const productPrices = [ 1.99 , 2.5 , 3.99 ] ;
const customerReviews = [
{ name : "Alice" , rating : 5 , comment : "Great product!" } ,
] ;
const monthlySalesData = [ 1000 , 1200 , 1500 ] ;
const filteredSearchResults = [ "shirt" , "jeans" , "jacket" ] ;
How to write better object name?
Context-Specific
Consider the object's usage within your application.
Example: const product = { id: 123, name: "T-Shirt", price: 19.99 };
Singular Nouns (for Single Objects)
Use singular nouns to represent a single entity.
Example: const user = { username: "bob", email: "[email protected] " };
Plural Nouns (for Collections)
Use plural nouns for objects that represent collections of similar items.
Example: const customers = [{ name: "Alice", id: 1 }, { name: "Bob", id: 2 }];
const shoppingCart = {
items : [ ] ,
totalPrice : 0 ,
addItem ( item ) {
this . items . push ( item ) ;
this . totalPrice += item . price ;
} ,
removeItem ( itemId ) { } ,
} ;
const product = {
id : 123 ,
name : "T-Shirt" ,
price : 19.99 ,
description : "A comfortable and stylish T-shirt" ,
stock : 10 ,
addReview ( review ) { } ,
} ;
const user = {
username : "alice" ,
email : "[email protected] " ,
firstName : "Alice" ,
lastName : "Smith" ,
getFullName ( ) {
return `${ this . firstName } ${ this . lastName } ` ;
} ,
} ;
const customer = {
id : 456 ,
userId : 123 ,
shippingAddress : { } ,
billingAddress : { } ,
} ;
const post = {
id : 789 ,
userId : 345 ,
content : "This is a social media post!" ,
createdAt : new Date ( ) ,
comments : [ ] ,
addComment ( comment ) {
this . comments . push ( comment ) ;
} ,
} ;