JavaScript naming rules every programmer needs to know
Consistently following naming rules is key to maintaining code simplicity, readability, and maintainability in complex JavaScript project management. The following naming rules will help you improve the readability and maintainability of your JavaScript code .
Variables, booleans, functions, constants, classes, components, methods, private functions, global variables, and files are among the JavaScript elements that require a consistent naming convention. You can improve code comprehension and organization by implementing standardized naming conventions across all of these components, saving you time and effort in the long run.
Variable naming
In JavaScript, data is stored as a variable. You need to choose a name that describes the variable that accurately reflects its function. For example, you can substitute userName or totalPrice for the name of a variable, instead of x.
A good way to name variables is as follows:
let totalPrice = 100; let userName = "John";
Improved code readability can be achieved by using descriptive variable names.
Naming Boolean
A variable with only two values, true or false, is called a Boolean. It's important that you choose the right name for your boolean variable, clearly stating their purpose.
For example, instead of choosing isTrue variable name, you should choose isValid or hasValue.
Consider the following example:
let isValid = true; let hasValue = false;
In this example, descriptive boolean variable names make it clear what they represent.
Function naming
A function in JavaScript refers to a standalone unit of code that performs a specific task. It is a block of code that can be called by other parts of the code and acts as a standalone component.
To name functions effectively, use names that describe their purpose. For example, instead of creating the function foo, choose more specific names like validateUserInput or calculateTotalPrice .
For example:
function calculateTotalPrice(price, quantity) { return price * quantity; } function validateUserInput(input) { return input !== undefined && input !== null; }
Naming constants
Constants are non-reassignable variables. When naming constants, it's important to use all capital letters and underscores to separate words.
For example:
const MAX_PRICE = 1000; const MIN_PRICE = 0;
In this example, all capital letters and underscores have been used to separate words in constant names.
Name the class
In JavaScript, objects can be created with a blueprint of the class name. To achieve perfect naming methods, you need to run PascalCase, a naming rule that requires the first letter of each word to be capitalized.
For example:
class ShoppingCart { constructor(make, model) { this.make = make; this.model = model; } }
In this example, the ShoppingCart class has been named with PascalCase, so the first letter of every word in the class name will be capitalized with no spaces or dashes between words.
Conforming to naming conventions is a basic knowledge every JavaScript programmer must know. Hope this article helps you better understand this issue.
You should read it
- What is Currying in Javascript? How to use Currying in JavaScript
- Things to know about 'this' in JavaScript
- Udemy's top 5 JavaScript courses
- Top site with many good JavaScript exercises to practice
- What is JavaScript?
- ! = and! == What is the difference in JavaScript?
- Learn about ES6 in Javascript
- Summary of JavaScript exercises with sample code
May be interested
- What is JavaScript?javascript is a programming language of html and web. it is lightweight and most commonly used as part of web pages, but their implementation allows client-side scripts to interact with users and create dynamic websites. it is an interpreted programming language with object-oriented capabilities.
- How to Debug Javascriptthe ability to debug code is an essential tool in any programmer's arsenal. debugging will give you inside information into how your program is functioning, in real-time, as it runs. javascript offers a unique but familiar method of...
- ! = and! == What is the difference in JavaScript?javascript includes operators like in other languages. an operator performs some operations on one or more operands (data values) and produces a result. today's article will help readers learn about 2! = and! == operators in javascript.
- Types of vehicles move in Rules of Survivalmoving and fighting vehicles in rules of survival are an integral part of the game, helping you to protect yourself or even attack your opponent.
- How to turn on tilt mode in Rules of Survivalthe tilt feature has been added to the new rules of survival, increasing the player's flexibility while shooting.
- Why should Apple use a year-based hardware naming system?apple has overhauled the naming system for all of its operating systems to use consistent yearly version numbers. for those who dread deciphering model numbers, apple should do the same for its devices.
- Learn about ES6 in Javascriptes6 refers to version 6 of the ecma script programming language. ecma script is the standard name for javascript and version 6 is the next version after version 5, released in 2011.
- How to change the player's name on Rules of Survivalif you don't want to use the old character name in rules of survival, you can create a new name right in the game's setup interface.
- What to do when being sniped remotely in Rules of Survival?when you're in the middle of a loot, the player is easily attacked by snipers from above. so what to do when a sniper is found in rules of survival?
- How to survive long when playing Solo in Rules of Survivalplaying the solo mode in rules of survival will not have any support teammates, you must attack the enemy and handle the situation correctly.