Trythy & Falsy
: JavaScript에서는 참, 거짓이 아닌 값도 참, 거짓으로 평가한
: 이를 이용하면 조건문을 간결하게 만들 수 있음
// 1. Falsy한 값
let f1 = undefined;
let f2 = null;
let f3 = 0;
let f4 = -0;
let f5 = NaN;
let f6 = "";
let f7 = 0n;
if(!f1){
console.log("falsy");
}
// 2. Truthy 한 값 -> 7가지 Falsy 한 값들 제외한 나머지 모든 값
let a1 = "hello";
let a2 = 123;
let a3 = [];
let a4 = {};
let a5 = () => {};
if (a4){console.log("Truthy")};
// 3. 활용 사례
function printName(person){
if (person === undefined || person === null){
console.log("person의 값이 없음");
return;
}
console.log(person.name);
}
// let person = {name : "토순이"};
let person;
printName(person);
구조 분해 할당
구조 분해 할당(Desctructuring Assignment)은 JavaScript에서 배열이나 객체를 해체하여 변수에 할당하는 기능입니다. 이를 통해 배열이나 객체의 값을 각각의 변수에 쉽게 할당할 수 있습니다.
예를 들어, 배열의 값을 변수에 할당하는 간단한 예제를 살펴보겠습니다.
const numbers = [1, 2, 3, 4, 5];
// 기존 방법
const first = numbers[0];
const second = numbers[1];
const third = numbers[2];
// ...
// 구조 분해 할당을 이용한 방법
const [first, second, third] = numbers;
console.log(first); // 1
console.log(second); // 2
console.log(third); // 3
const person = { name: 'John', age: 30, city: 'New York' }; // 기존 방법
const name = person.name;
const age = person.age;
const city = person.city;
// 구조 분해 할당을 이용한 방법
const { name, age, city } = person;
console.log(name); // 'John'
console.log(age); // 30
console.log(city); // 'New York'
728x90
'DEVELOPE > 앱개발시작하기' 카테고리의 다른 글
[20240505] 앱개발 시작하기 #9 | 날짜 시간 출력 코드 (0) | 2024.05.05 |
---|---|
[20240505] 앱개발 시작하기 #8 | 배열 (0) | 2024.05.05 |
[20240502] 앱개발 시작하기 #6 (0) | 2024.05.03 |
[20240502] 앱개발 시작하기 #5 (0) | 2024.05.02 |
[20240502] 앱개발 시작하기 #4 (0) | 2024.05.02 |