DEV Community

loading...

Discussion on: What programming best practice do you disagree with?

Collapse
eileenmccall profile image
Eileen McCall

I have a lot of opinions about JS style that might a bit uncommon or idiosyncratic

var > let > const

{
  let x = 1; // let for block level vars
}
const pi = 3.14; // const for string/num constants
var y = 3; // var for everything else

Double quotes for strings, always

var yes = "good";
var no = 'bad';

Space before function parens in declaration, no space before invocation

function foo () { }
foo();

ONLY use arrow function when you require lexical this, and NEVER (well, almost never) use anonymous functions. Also, no arrow functions without curlies (I make exception for simple pluck operations because I'm lazy).

[1,2,3].map(function timesTwo (num) { return num * 2; });

var timesMultiplier = (num) => { return num * this.multiplier; };
[1,2,3].map(timesMultiplier);

userAPI.getUser().then(usr => usr.id); // this is fine I guess