장황한 보일러플레이트 패턴을 대체하는 현대 JavaScript의 관용적 단축 표현식 모음입니다.
핵심 단축 패턴
널 병합 연산자 (??) 및 기본값 대체
// ??는 null 또는 undefined만 엄격하게 검사 (0과 false 값은 유지)
const port = process.env.PORT ?? 3000;
// ||는 모든 falsy 값(0, "", false, NaN)에서 대체값으로 전환
const title = customTitle || 'Untitled';
옵셔널 체이닝 (?.)
// TypeError 없이 깊게 중첩된 객체 프로퍼티에 안전하게 접근
const street = user?.address?.street;
const firstTag = user?.tags?.[0];
const result = user?.getProfile?.();
단락 평가 실행 (&&)
// 조건이 참일 때만 함수 실행
isLoggedIn && showDashboard();
객체 프로퍼티 및 메서드 단축 구문
const name = 'Alice';
const age = 30;
// 객체 리터럴 단축 표기법
const user = {
name,
age,
greet() {
return `Hello ${this.name}`;
}
};
배열 및 객체 구조 분해 할당
const { name, age, role = 'member' } = user;
const [first, second, ...rest] = [1, 2, 3, 4, 5];