📌 원시 타입의 데이터

Untitled

let a = 1;
let b = a;

console.log(a); //1
console.log(b); //1
let a = 1;
let b = a;

b = 2;

console.log(a); //1
console.log(b); //2

→ Copy by Value

→ 원시 타입의 데이터는 값 자체가 메모리에 저장되기 때문에 재할당시,

재할당 된 값 자체가 저장되므로, 이전의 값에는 영향이 없다.

→ 복사한 값과 복사된 값이 서로 다른 메모리에 할당되기 때문에 서로 영향이 없다.

→ immutable value(변경 불가능 한 값)

📌 객체 타입의 데이터

Untitled

let apple = {
  name: "사과",
};

let orange = apple;

orange.name = "오렌지";

console.log(orange.name); //오렌지
console.log(apple.name); //오렌지

→ Copy by Reference

→ 객체 타입의 데이터는 값이 아닌 참조 값(=메모리주소, =레퍼런스)가 복사되어 전달되므로

재할당시, 재할당 된 객체의 값은 변경된다.

직접 대상 배열을 변경

⇒얕은 복사(shallow copy)라고 칭한다.