기본형
- Number
- String
- Boolean
- Null
- Undefined
=> 변수 = 값
let x = 3;
let y = x;
console.log(x); // 3
console.log(y); // 5
y = 5;
console.log(x); // 3
console.log(y); // 5
참조형
- Object
- 객체의 경우 x와 y이 길이 열리기 때문에 y에서 값을 주면 x도 함께 값을 받음
=> 변수 = 주소값
let x = {name: 'Codeit'};
let y = x;
console.log(x); // name: 'Codeit'
console.log(y); // name: 'Codeit'
y.birth = 2017;
console.log(x); // name: 'Codeit', birth: 2017
console.log(y); // name: 'Codeit', birth: 2017
let x = [1, 2, 3];
let y = x;
console.log(x); // [1, 2, 3]
console.log(y); // [1, 2, 3]
y[2] = 4;
console.log(x); // [1, 2, 4]
console.log(y); // [1, 2, 4]
QUIZ 01
다음 코드를 실행했을 때 출력되는 내용을 고르시오.
let x = 'Codeit';
let y = x;
y = x + '!'; // y = Codeit!
x = y.toLowerCase(); // x = codeit
console.log(y); // Codeit!
QUIZ 02
다음 코드를 실행했을 때 출력되는 내용을 고르세요.
let x = ['Kim', 'Na', 'Park', 'Lee'];
let y = x; // ['Kim', 'Na', 'Park', 'Lee']
y.push('Lim'); ['Kim', 'Na', 'Park', 'Lee', 'Lim']
x[4] = 'Sung'; // ['Kim', 'Na', 'Park', 'Lee', 'Sung']
console.log(y); // ['Kim', 'Na', 'Park', 'Lee', 'Sung']
QUIZ 03
다음 코드를 실행했을 때 출력되는 내용을 고르세요.
let x = {
numbers: [1, 2, 3, 4],
title: 'Codeit'
};
let y = x.numbers; // [1, 2, 3, 4]
let z = x.title; // Codeit
x.numbers.unshift(5); [5, 1, 2, 3, 4]
x.title = 'Hello'; // Hello
console.log(y); // [5, 1, 2, 3, 4]
console.log(z); // Codeit'Codeit > JavaScript' 카테고리의 다른 글
| const, 변수와 상수 사이 (0) | 2024.08.08 |
|---|---|
| 참조형 복사하기 (0) | 2024.08.08 |
| 문자열 심화 (2) | 2024.08.08 |
| 다양한 숫자 표기법 (0) | 2024.08.07 |
| 다차원 배열 (0) | 2024.08.07 |