JS Shorthands in a Clean and Easy way
Better way to return
Instead of this
const meaningFullName = (element) => {
return element.values
}
use this
const meaningFullName = (element) => element.values
Arrays Naming convention
Reasons for using a naming convention include the following:
- To reduce the effort needed to read and understand source code.
- To enable code reviews to focus on more important issues than arguing over syntax and naming standards.
- To enable code quality review tools to focus their reporting mainly on significant issues other than syntax and style preferences.
const students = [‘Rowan ’, ‘Lisa’, ‘Katsa’, ‘Aelin’]
The above variable example will usually hold a list of items; therefore, append an ’s’ to your variable name.
Better way to log the objects (console.log…)
Instead of this
console.log(obj1);
console.log(obj2);
use this
console.log({obj1, obj2})
Strong type checks
Use === instead of ==, Becasue we want the value and the type to be equal below is the example.
const val = “xyz”;
if (val === xyz) {
console.log(val);
// it cannot not be reached
}
if (val === “xyz”) {
console.log(val);
// it can be reached
}
Better way to log conditionally
instead of this way
if(condition ){
console.log(‘output’);
}
use the following way
condition && console.log(‘output’);
Boolean
Simply start with is or has to be close to natural language. Reason for using is or has because we are either expecting true or false depends upon the logic being used.
const isAvailable= true // OR false
Assigning Alternate Value for null variables
instead of this way
const meaningFullName(val){
if(val == null ){
val=11;
}
}
use the following way
const meaningFullName(val=50){
// Your Code
}