if else를 사용하여 조건에 따라 변수 값을 넣을 때, 한줄로 줄여주는 방법.
[longhand]
const target;
if (x > 0) {
target = true;
} else {
target = false;
}
[shorthand]
const target = x > 0 ? ture : false;
const target = x > 0 ? true : x
기존 변수를 다른 변수에 할당하고자 할경우, 기존 변수가 null,undefined 또는 empty 값이 아닌 것을 확인해야 함.
(위 3가지의 경우 에러 발생)
이를 해결하기 위해 긴 if문을 작성하거나 축약코딩으로 한줄에 끝낼 수 있음.
[longhand]
if (variable1 !== null || variable1 !== undefined || variable1 !== '') {
let variable2 == variable1;
}
[shorthand]
const variable2 = variable1 || 'new';
함수를 시작하기 전 먼저 필요한 변수들을 선언.
[longhand]
let x;
let y;
let z = 1;
[shorthand]
let x, y, z=1;
For문 바닐라 자바스크립트 사용 시.
(*바닐라 자바스크립트란? 플러그인이나, 라이브러리를 사용하지 않은 순수 자바스크립를 이야기함.
즉, 바닐라 자바스크립트 = 자바스크립트)
[longhand]
for (let i = 0; i < allImgs.length; i++)
[shorthand]
for (let index of allImgs)
[Array.forEach short]
function logArrayElements(element, index, array) {
console.log("a[" + index + "] = " + element);
}
[2, 5, 9].forEach(logArrayElements);
// logs:
// a[0] = 2
// a[1] = 5
// a[2] = 9
*이상 더 많지만, 여기까지..