development:js:code_examples

JavaScript: Best practices

Logging
console.log('%c Test', 'color:orange');
console.log({var1, var2, var3});
console.table([var1, var2, var3]);
 
 
console.time('timerName');
... code ...
console.timeEnd('timerName');
 
console.trace('comment');
Strings
var a = varb + ' pero ' + varc; // Bad
var a = '${varb} pero ${varc}';  // Good
 
function test({prop1, prop2 prop3}) {
    return '${prop1}, ${prop2}, ${prop3}';
}
//or
function test(array) {
    const {prop1, prop2 prop3} = array;
    return '${prop1}, ${prop2}, ${prop3}';
}
 
//usage
test(array); //array with field names as above
 
 
const var1 = var2 > 5 ? 'text1' : 'text2';
Arrays
test = ['d','e','f'];
 
//push
test = [...test,'g','h','i'];
 
//shift
test = ['a','b','c',...test];
 
//insert
test = ['1','2',...test,'3','4'];
 
 
const high = array.filter(v => v > 100);
 
const tax = array.map(v => v * 1.25);
  • development/js/code_examples.txt
  • Last modified: 2019/10/31 09:04
  • by 127.0.0.1