목록전체 글 (796)
코드 그라데이션
단락회로 평가 논리 연산에서 첫 번째 피연샂나의 값만으로 해당 식의 결과가 확실할 때, 두 번째 값은 평가하지 않는 것. truthy와 falsy에서 const getName = (person) => { if(!person) { return "객체가 아닙니다." } return person.getName; }; let person;// undefined로 할당했으나 저 분기에 걸려서 에러 안 났음. const name = getName(person); console.log(name); 이 코드는 "객체가 아닙니다." 출력됨. const getName = (person) => { return person && person.name; // person이 undefined기 때문에 뒤의 값을 고려할 필요가 x..
ex. 기분에 따라 하는 행동 출력하기 function mood(state) { if (state === "good") { // 기분 좋을 때 하는 동작 sing(); } else { // 기분 나쁠 때 하는 동작 cry(); } } function cry() { console.log("액션 : CRY"); } function sing() { console.log("액션 : SING"); } function dance() { console.log("액션 : DANCING"); } mood("good"); 상황에 맞게 하나의 함수로 만드는 게 가능해진다.
1. 순환 함수 (forEach()) 이거를 const arr = [1, 2, 3, 4]; for (let i = 0; i console.log(elm)); 이렇게 바꾸는 거 가능. 그냥함수 형태로 바꾸면 arr.forEach(function (elm) { console.log(elm); }); 출력 결과는 arr.forEach(function (elm) { console.log(elm * 2); }); 하면 2. map() - 원본 배열의 모든 원소를 순회하면서 연산을 해서 반환할 수 있게 만들어주는 함수. 새로운 배열을 리턴함. 응용 2. ..