여러개의 조건문에서 처리를 한다면 if/else문 또는 switch/case 문 을 떠오를 것이다. if/else 문, switch/case 문 없이 조건문을 작성할 수 있을까? true/false 로 리턴시키는 형태로는 가능할 거 같다. 그렇지만 조건문의 길이가 너무 길고, 읽기가 불편한다. // BAD CODE function isAnimal(text){ return ( text === '고양이' || text === '개' || text === '거북이' || text === '너구리' ); } 조건문을 간결하게 줄일 수 없을까? 배열의 내장함수 includes 를 사용한다. function isAnimal(text){ const animals = [ '고양이', '개', '너구리', '거북이', ..
만약에 r이 undefined/ NaN / null 과 같은 falsy한 값일 때 기본값을 설정해서 구하게끔하고 싶다면 어떻게 해야할까? 이전에 배운 truthy/falsy 논리를 사용하여 정의를 한다면, 아래와 같다. console.clear(); function calculateCircleArea(r) { const radius = r || 1; return Math.PI * radius * radius; } const area = calculateCircleArea(); console.log(area); // 3.141592653589793 그런데 ES6 문법에서는 falsy한 값이 파라미터를 통해 전달될 때 설정된 기본값으로 변경할 수 있도록 작성할 수 있고, 더 간결하다. console.clea..
AND 연산 (연산자: && ) 맨앞이 truthy 한 값이라면 맨 뒤의 데이터를 결과값으로 한다. 반면, 맨앞이 falsy한 값이라면 맨앞의 데이터를 결과값으로 한다. // truthy한 값과 AND 연산 console.log(true && 'hello')// hello console.log('hello' && true)// true console.log('hello' && 'bye')// bye console.log(1 && 2)// 2 console.log(2 && 1 && 3)// 3 // falsy한 값과 AND 연산 console.log(false && 'hello')// false console.log(null && 'hello')// null console.log(undefined && '..
Truthy : 값이 존재한다. // truthy console.log(!3); // false console.log(!"hello"); // false console.log(![]); // false console.log(!{}); // false Falsy: 값이 존재하지 않거나, 비어있는 것 undefined null 0 "" , '' (비어있는 문자열) NaN (Not a Number) // falsy console.log(!undefined); //true console.log(!null); //true console.log(!0); //true console.log(!""); //true console.log(!NaN); //true (NaN: Not a Number) 특히 아래 코드의 if ..
삼항연산자를 사용하는 경우 한 줄로 값을 정의할 수 있다. console.clear(); const array = [1, "a", [1, 2, 3]]; let text = array.length === 0 ? "배열이 비었음" : "배열이 안 비었음."; console.log(text);// 배열이 안 비었음. 삼항연산자를 사용하지 않은 경우 console.clear(); const array = [1, "a", [1, 2, 3]]; let text = ""; if (array.length === 0) { text = "배열이 비었음."; } else { text = "배열이 비어있지 않음."; } console.log(text); // 배열이 비어있지 않음. 삼항연산자는 조건에 대응되는 값을 초기화할 ..
객체 생성자 function Animal( type, name, sound ) { this.type = type; this.name = name; this.sound = sound; this.say = function() { console.log(this.sound); }; } const dog = new Animal('개', '슈슈', '슈슝~'); const cat = new Animal('고양이', '줍줍', '줍줍이~'); dog.say();//슈슝~ cat.say();//줍줍이~ prototype prototype 은 동일한 클래스인 객체들끼리 값이나 함수를 공유할 수 있다. function Animal( type, name, sound ) { this.type = type; this.name ..
reduce - 배열의 모든원소를 활용하여 한줄로 연산이 가능하다. - 한꺼번에 처리할 수 있어서 매우 유용한 연산자다. 알면좋다. reduce 예제 1 - sum 연산으로 forEach문과 비교 forEach 반복문을 사용했을 때 console.clear(); const numbers = [ 1,2,3,4,5 ]; let sum = 0; numbers.forEach( n => { sum += n; }); console.log(sum); // 15 reduce를 사용했을 때 - 아주 간단하다 3줄이 한줄로 줄여진다. - arrayName.reduce( (누적값, 현재값) => 내부연산 , 초기 누적값 ); 으로 구성되어 있다. acc는 accumulator의 약자로, 누적값을 의미한다. curr은 cur..
shift 와 unshift - shift, unshift 함수 모두 배열의 원본이 변한다. shift - 배열의 맨앞의 원소값을 추출 const numbers = [10, 20, 30, 40]; const value = numbers.shift(); console.log(value);// 10 console.log(numbers);// [20, 30, 40] unshift - 배열의 맨앞에 원소값을 추가 const numbers = [10, 20, 30, 40]; numbers.unshift(0); console.log(numbers);// [0, 10, 20, 30, 40] pop 과 push - pop, push 함수 모두 배열의 원본이 변한다 pop - 배열의 맨뒤의 원소값을 추출 const nu..
- Total
- Today
- Yesterday
- TDD
- OS
- 스마트폰중독
- MySQL
- 갓생살자
- 개발용어
- Jekyll
- 참고
- typeORM
- git
- 미완
- 디지털디톡스
- node.js
- jest
- vscode
- 한달독서
- MongoDB
- TypeScript
- 습관개선
- Mongoose
- RDBMS
- gem
- Nest.js
- 나도 할 수 있다
- nestjs jest
- 클린아키텍쳐
- 바이트디그리
- 한달어스
- nestjs
- IT용어
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |