JavaScript Style Guide - References

Jang Seok Woo·2022년 8월 11일
0

실무

목록 보기
99/136

2. References

2.1 Use const for all of your references

avoid using var.
eslint: prefer-const, no-const-assign

Why? This ensures that you can’t reassign your references, which can lead to bugs and difficult to comprehend code.

// bad
var a = 1;
var b = 2;

// good
const a = 1;
const b = 2;

2.2 If you must reassign references, use let instead of var.

eslint: no-var
var 쓸거면 let을 쓰라네요 그 이유는,

Why? let is block-scoped rather than function-scoped like var.

// bad
var count = 1;
if (true) {
  count += 1;
}

// good, use the let.
let count = 1;
if (true) {
  count += 1;
}

2.3 Note that both let and const are block-scoped, whereas var is function-scoped.

// const and let only exist in the blocks they are defined in.
{
  let a = 1;
  const b = 1;
  var c = 1;
}
console.log(a); // ReferenceError
console.log(b); // ReferenceError
console.log(c); // Prints 1

출처 : https://github.com/airbnb/javascript

profile
https://github.com/jsw4215

0개의 댓글