Eureka Moment Wiki

One eureka moment at the time

User Tools

Site Tools


development:js:code_examples

JavaScript good 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: 2018/10/12 08:23 by tplecko