Bad
const yyyymmdd = moment().format("YYYY/MM/DD");Good
const currentDate = moment().format("YYYY/MM/DD");Bad
getUserInfo();
getClientData();
getCustomerRecord();Good
getUser();Bad
setTimeout(blastOff, 86400000);Good
const MILLISECONDS_IN_A_DAY = 86400000;
setTimeout(blastOff, MILLISECONDS_IN_A_DAY);Bad
const address = "One Infinite Loop, Cupertino 95014";
const cityZipCodeRegex = /^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/;
saveCityZipCode(
address.match(cityZipCodeRegex)[1],
address.match(cityZipCodeRegex)[2]
);Good
const address = "One Infinite Loop, Cupertino 95014";
const cityZipCodeRegex = /^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/;
const [, city, zipCode] = address.match(cityZipCodeRegex) || [];
saveCityZipCode(city, zipCode);Bad
const locations = ["서울", "성남", "수원"];
locations.forEach((l) => {
doStuff();
doSomeOtherStuff();
dispatch(l);
});Good
const locations = ["서울", "성남", "수원"];
locations.forEach((location) => {
doStuff();
doSomeOtherStuff();
dispatch(location);
});Bad
const Car = {
carMake: "BMW",
carModel: "M3",
carColor: "blue",
};
function paintCar(car) {
car.carColor = "red";
}Good
const Car = {
make: "BMW",
model: "M3",
color: "blue",
};
function paintCar(car) {
car.color = "red";
}기본 매개변수는 종종 short circuiting 트릭보다 깔끔합니다. 기본 매개변수는 매개변수가 undefined 일때만 적용됩니다. '', "", false, null, 0, NaN 같은 falsy한 값들은 기본 매개변수가 적용되지 않습니다.
Bad
function createMicrobrewery(name) {
const breweryName = name || "Hipster Brew Co.";
// ...
}Good
function createMicrobrewery(name = "Hipster Brew Co.") {
// ...
}