[개념] 자바스크립트 / 비 구조화 할당(구조분해 할당) Destructuring assignment

Eun-jeong Park·2023년 7월 17일

javascript

목록 보기
4/6
post-thumbnail

비 구조화 할당(구조 분해 할당) 구문은 배열이나 객체의 속성을 해체하여 그 값을 개별 변수에 담을 수 있게 하는 JavaScript 표현식입니다.

let arr = ["one", "two", "three"];

let [one, two, three] = arr;
console.log(one, two, three);

swap

let a = 10;
let b = 20;

[a, b] = [b, a];
console.log(a, b);

객체일 때는

let object = { one: "one", two: "two", three: "three", name: "손흥민" };

let { name: myName, one, two, three. abc = "four" } = object;
console.log(one, two, three, myName, abc);

결과

one two three 손흥민 four

=> 순서가 아닌 키 값을 기준으로 할당한다.

profile
지구를 사랑하는 개발자

1개의 댓글

comment-user-thumbnail
2023년 7월 18일

소중한 정보 감사드립니다!

답글 달기