-
Value Types and Reference TypesLanguage/JavaScript 2022. 9. 22. 18:02
(EP 03.) 자바스크립트 개발자라면 알아야하는 핵심 컨셉 33개 | #3. Value Types and Reference Types
* 간단요약
1. Value Types : string, number, boolean, NaN, undefined, null
let a = 50; let b = a; a = 10; console.log(b); //50
value 데이터 타입을 새로운 변수에 할당(=)하는 경우 변수에 복사된 새로운 value는 기존 value를 참조하지 않는다.
- Reference Types : array, object, function
const sexy = ['kimchi', 'potato']; const pretty = sexy; sexy.push('hello'); console.log(pretty); // [kimchi', 'potato', 'hello']
배열을 새로운 변수에 할당(=)하는 경우 변수에 할당된 새로운 배열은 기존 배열을 참조한다.
const x = { a: 'hello' } const b = x; b.a = 'lalaal' console.log(x); // {a: 'lallaal'}
객체 또한 새로운 변수에 할당(=)하는 경우 변수에 할당된 새로운 객체는 기존 객체를 참조한다.
console.log(10 === 10) // true -> 두 값(value)은 같으므로 true console.log([10] === [10]) // false -> 두 배열은 다른 메모리에 위치한 다른 객체이므로 false
'Language > JavaScript' 카테고리의 다른 글
JavaScript 100제 1부 오답노트 (1~20) (0) 2022.09.30 삼항연산자(조건부연산자, ?) (0) 2022.09.22 문자열을 배열로 변환, 배열을 문자열로 변환, Spread Operator (0) 2022.09.22 단축 평가 값(short circuit evaluation) (0) 2022.09.05 JavaScript 세미콜론 가이드 (1) 2022.09.02