JavaScript ๊ธฐ์ด
JavaScript ๊ธฐ์ด¶
๊ฐ์¶
JavaScript๋ ์น ํ์ด์ง์ ๋์ ์ธ ๊ธฐ๋ฅ์ ์ถ๊ฐํ๋ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ ๋๋ค. HTML์ด ๊ตฌ์กฐ, CSS๊ฐ ์คํ์ผ์ด๋ผ๋ฉด, JavaScript๋ ๋์์ ๋ด๋นํฉ๋๋ค.
์ ์ ์ง์: HTML, CSS ๊ธฐ์ด
๋ชฉ์ฐจ¶
- JavaScript ์์ํ๊ธฐ
- ๋ณ์์ ์์
- ์๋ฃํ
- ์ฐ์ฐ์
- ์กฐ๊ฑด๋ฌธ
- ๋ฐ๋ณต๋ฌธ
- ํจ์
- ๋ฐฐ์ด
- ๊ฐ์ฒด
- ES6+ ๋ฌธ๋ฒ
JavaScript ์์ํ๊ธฐ¶
HTML์์ JavaScript ์ฌ์ฉํ๊ธฐ¶
<!-- ๋ฐฉ๋ฒ 1: ๋ด๋ถ ์คํฌ๋ฆฝํธ -->
<script>
console.log('Hello, World!');
</script>
<!-- ๋ฐฉ๋ฒ 2: ์ธ๋ถ ์คํฌ๋ฆฝํธ (๊ถ์ฅ) -->
<script src="main.js"></script>
<!-- ๋ฐฉ๋ฒ 3: ์ธ๋ผ์ธ ์ด๋ฒคํธ (๋น๊ถ์ฅ) -->
<button onclick="alert('ํด๋ฆญ!')">ํด๋ฆญ</button>
์คํฌ๋ฆฝํธ ์์น¶
<!DOCTYPE html>
<html>
<head>
<!-- head์ ๋ฃ์ผ๋ฉด HTML ํ์ฑ ์ฐจ๋จ -->
<script src="blocking.js"></script>
<!-- defer: HTML ํ์ฑ ์๋ฃ ํ ์คํ -->
<script src="main.js" defer></script>
<!-- async: ๋ค์ด๋ก๋ ์๋ฃ ์ฆ์ ์คํ (์์ ๋ณด์ฅ X) -->
<script src="analytics.js" async></script>
</head>
<body>
<h1>Hello</h1>
<!-- body ๋์ ๋ฃ์ผ๋ฉด DOM ์ค๋น ํ ์คํ -->
<script src="main.js"></script>
</body>
</html>
| ์์ฑ | ์คํ ์์ | ์์ ๋ณด์ฅ | ์ฉ๋ |
|---|---|---|---|
| (์์) | ์ฆ์ | O | - |
defer |
DOM ํ์ฑ ํ | O | ์ผ๋ฐ ์คํฌ๋ฆฝํธ |
async |
๋ค์ด๋ก๋ ํ | X | ๋ถ์, ๊ด๊ณ |
๊ฐ๋ฐ์ ๋๊ตฌ ์ฝ์¶
๋ธ๋ผ์ฐ์ ์์ F12 โ Console ํญ์์ JavaScript๋ฅผ ๋ฐ๋ก ์คํํ ์ ์์ต๋๋ค.
console.log('์ผ๋ฐ ์ถ๋ ฅ');
console.warn('๊ฒฝ๊ณ ');
console.error('์๋ฌ');
console.table([{a: 1}, {a: 2}]); // ํ ํํ
๋ณ์์ ์์¶
let (๋ณ์)¶
๊ฐ์ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.
let name = 'ํ๊ธธ๋';
name = '๊น์ฒ ์'; // ์ฌํ ๋น ๊ฐ๋ฅ
let count; // ์ ์ธ๋ง (undefined)
count = 0; // ๋์ค์ ํ ๋น
const (์์)¶
๊ฐ์ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.
const PI = 3.14159;
// PI = 3.14; // ์๋ฌ! ์ฌํ ๋น ๋ถ๊ฐ
const user = { name: 'ํ๊ธธ๋' };
user.name = '๊น์ฒ ์'; // ๊ฐ์ฒด ๋ด๋ถ ์์ฑ์ ๋ณ๊ฒฝ ๊ฐ๋ฅ
// user = {}; // ์๋ฌ! ๊ฐ์ฒด ์์ฒด ์ฌํ ๋น ๋ถ๊ฐ
var (๋ ๊ฑฐ์, ์ฌ์ฉ ์ง์)¶
var old = '์ค๋๋ ๋ฐฉ์'; // ํจ์ ์ค์ฝํ, ํธ์ด์คํ
๋ฌธ์
๋ณ์ ๋ช ๋ช ๊ท์น¶
// ์ฌ์ฉ ๊ฐ๋ฅ
let userName = 'kim'; // camelCase (๊ถ์ฅ)
let user_name = 'kim'; // snake_case
let _private = 'secret'; // ์ธ๋์ค์ฝ์ด ์์
let $element = 'dom'; // ๋ฌ๋ฌ ์์
let ํ๊ธ๋ณ์ = '๊ฐ๋ฅ'; // ์ ๋์ฝ๋ (๋น๊ถ์ฅ)
// ์ฌ์ฉ ๋ถ๊ฐ
// let 1name = 'x'; // ์ซ์๋ก ์์ X
// let user-name = 'x'; // ํ์ดํ X
// let let = 'x'; // ์์ฝ์ด X
์ค์ฝํ¶
// ๋ธ๋ก ์ค์ฝํ (let, const)
{
let blockVar = '๋ธ๋ก ๋ด๋ถ';
const blockConst = '๋ธ๋ก ๋ด๋ถ';
}
// console.log(blockVar); // ์๋ฌ! ์ ๊ทผ ๋ถ๊ฐ
// ํจ์ ์ค์ฝํ (var)
function test() {
var funcVar = 'ํจ์ ๋ด๋ถ';
}
// console.log(funcVar); // ์๋ฌ!
// ์ ์ญ ์ค์ฝํ
let globalVar = '์ด๋์๋ ์ ๊ทผ ๊ฐ๋ฅ';
์๋ฃํ¶
์์ ํ์ (Primitive)¶
// String (๋ฌธ์์ด)
let str1 = '์์๋ฐ์ดํ';
let str2 = "ํฐ๋ฐ์ดํ";
let str3 = `ํ
ํ๋ฆฟ ๋ฆฌํฐ๋ด ${str1}`;
// Number (์ซ์)
let int = 42;
let float = 3.14;
let negative = -10;
let infinity = Infinity;
let notANumber = NaN;
// BigInt (ํฐ ์ ์)
let big = 9007199254740991n;
// Boolean (์ฐธ/๊ฑฐ์ง)
let isTrue = true;
let isFalse = false;
// undefined (๊ฐ ์์)
let nothing;
console.log(nothing); // undefined
// null (์๋์ ๋น ๊ฐ)
let empty = null;
// Symbol (๊ณ ์ ์๋ณ์)
let sym = Symbol('description');
์ฐธ์กฐ ํ์ (Reference)¶
// Object (๊ฐ์ฒด)
let obj = { name: 'ํ๊ธธ๋', age: 30 };
// Array (๋ฐฐ์ด)
let arr = [1, 2, 3, 4, 5];
// Function (ํจ์)
let func = function() { return 'hello'; };
ํ์ ํ์ธ¶
typeof 'hello' // "string"
typeof 42 // "number"
typeof true // "boolean"
typeof undefined // "undefined"
typeof null // "object" (์ญ์ฌ์ ๋ฒ๊ทธ)
typeof {} // "object"
typeof [] // "object"
typeof function(){} // "function"
// ๋ฐฐ์ด ํ์ธ
Array.isArray([1, 2, 3]) // true
Array.isArray({}) // false
ํ์ ๋ณํ¶
// ๋ฌธ์์ด๋ก ๋ณํ
String(123) // "123"
(123).toString() // "123"
123 + '' // "123"
// ์ซ์๋ก ๋ณํ
Number('123') // 123
Number('abc') // NaN
parseInt('42px') // 42
parseFloat('3.14') // 3.14
+'123' // 123
// ๋ถ๋ฆฌ์ธ์ผ๋ก ๋ณํ
Boolean(1) // true
Boolean(0) // false
Boolean('') // false
Boolean('hello') // true
!!1 // true
// Falsy ๊ฐ (false๋ก ๋ณํ๋๋ ๊ฐ)
// false, 0, -0, '', null, undefined, NaN
์ฐ์ฐ์¶
์ฐ์ ์ฐ์ฐ์¶
10 + 3 // 13 (๋ง์
)
10 - 3 // 7 (๋บ์
)
10 * 3 // 30 (๊ณฑ์
)
10 / 3 // 3.333... (๋๋์
)
10 % 3 // 1 (๋๋จธ์ง)
10 ** 3 // 1000 (๊ฑฐ๋ญ์ ๊ณฑ)
// ์ฆ๊ฐ ์ฐ์ฐ์
let a = 5;
a++ // ํ์: ์ฌ์ฉ ํ ์ฆ๊ฐ
++a // ์ ์: ์ฆ๊ฐ ํ ์ฌ์ฉ
a--
--a
๋น๊ต ์ฐ์ฐ์¶
// ๋๋ฑ ๋น๊ต (ํ์
๋ณํ O)
5 == '5' // true
0 == false // true
null == undefined // true
// ์ผ์น ๋น๊ต (ํ์
๋ณํ X) - ๊ถ์ฅ!
5 === '5' // false
0 === false // false
// ๋ถ๋ฑ ๋น๊ต
5 != '5' // false (ํ์
๋ณํ)
5 !== '5' // true (ํ์
๋น๊ต)
// ํฌ๊ธฐ ๋น๊ต
5 > 3 // true
5 >= 5 // true
5 < 3 // false
5 <= 5 // true
๋ ผ๋ฆฌ ์ฐ์ฐ์¶
// AND (๋ ๋ค true๋ฉด true)
true && true // true
true && false // false
// OR (ํ๋๋ผ๋ true๋ฉด true)
true || false // true
false || false // false
// NOT (๋ฐ์ )
!true // false
!false // true
// ๋จ๋ฝ ํ๊ฐ (Short-circuit)
const name = user && user.name; // user๊ฐ ์์ผ๋ฉด undefined
const value = input || '๊ธฐ๋ณธ๊ฐ'; // input์ด falsy๋ฉด '๊ธฐ๋ณธ๊ฐ'
Nullish ์ฐ์ฐ์¶
// ?? (null/undefined์ผ ๋๋ง ์ฐ์ธก ๊ฐ)
null ?? '๊ธฐ๋ณธ๊ฐ' // '๊ธฐ๋ณธ๊ฐ'
undefined ?? '๊ธฐ๋ณธ๊ฐ' // '๊ธฐ๋ณธ๊ฐ'
0 ?? '๊ธฐ๋ณธ๊ฐ' // 0
'' ?? '๊ธฐ๋ณธ๊ฐ' // ''
// || ์ ๋น๊ต
0 || '๊ธฐ๋ณธ๊ฐ' // '๊ธฐ๋ณธ๊ฐ' (0์ falsy)
'' || '๊ธฐ๋ณธ๊ฐ' // '๊ธฐ๋ณธ๊ฐ' (''์ falsy)
ํ ๋น ์ฐ์ฐ์¶
let x = 10;
x += 5; // x = x + 5 โ 15
x -= 3; // x = x - 3 โ 12
x *= 2; // x = x * 2 โ 24
x /= 4; // x = x / 4 โ 6
x %= 4; // x = x % 4 โ 2
x **= 2; // x = x ** 2 โ 4
// ๋
ผ๋ฆฌ ํ ๋น (ES2021)
x ||= 10; // x = x || 10
x &&= 5; // x = x && 5
x ??= 0; // x = x ?? 0
์ผํญ ์ฐ์ฐ์¶
const result = ์กฐ๊ฑด ? ์ฐธ์ผ๋ : ๊ฑฐ์ง์ผ๋;
const age = 20;
const status = age >= 18 ? '์ฑ์ธ' : '๋ฏธ์ฑ๋
';
// ์ค์ฒฉ (๊ฐ๋
์ฑ ์ฃผ์)
const grade = score >= 90 ? 'A'
: score >= 80 ? 'B'
: score >= 70 ? 'C'
: 'F';
์กฐ๊ฑด๋ฌธ¶
if...else¶
const age = 20;
if (age >= 18) {
console.log('์ฑ์ธ');
} else if (age >= 13) {
console.log('์ฒญ์๋
');
} else {
console.log('์ด๋ฆฐ์ด');
}
switch¶
const day = '์';
switch (day) {
case '์':
case 'ํ':
case '์':
case '๋ชฉ':
case '๊ธ':
console.log('ํ์ผ');
break;
case 'ํ ':
case '์ผ':
console.log('์ฃผ๋ง');
break;
default:
console.log('์ ์ ์์');
}
์กฐ๊ฑด๋ถ ์คํ¶
// ์กฐ๊ฑด && ์คํ๋ฌธ
isLoggedIn && showDashboard();
// ์กฐ๊ฑด || ์คํ๋ฌธ
data || fetchData();
๋ฐ๋ณต๋ฌธ¶
for¶
for (let i = 0; i < 5; i++) {
console.log(i); // 0, 1, 2, 3, 4
}
// ์ญ์
for (let i = 4; i >= 0; i--) {
console.log(i); // 4, 3, 2, 1, 0
}
for...of (๋ฐฐ์ด)¶
const fruits = ['์ฌ๊ณผ', '๋ฐ๋๋', '์ค๋ ์ง'];
for (const fruit of fruits) {
console.log(fruit);
}
for...in (๊ฐ์ฒด)¶
const user = { name: 'ํ๊ธธ๋', age: 30 };
for (const key in user) {
console.log(key, user[key]);
// name ํ๊ธธ๋
// age 30
}
while¶
let count = 0;
while (count < 5) {
console.log(count);
count++;
}
do...while¶
let num = 0;
do {
console.log(num); // ์ต์ 1๋ฒ์ ์คํ
num++;
} while (num < 5);
break์ continue¶
// break: ๋ฐ๋ณต๋ฌธ ์ข
๋ฃ
for (let i = 0; i < 10; i++) {
if (i === 5) break;
console.log(i); // 0, 1, 2, 3, 4
}
// continue: ํ์ฌ ๋ฐ๋ณต ๊ฑด๋๋ฐ๊ธฐ
for (let i = 0; i < 5; i++) {
if (i === 2) continue;
console.log(i); // 0, 1, 3, 4
}
ํจ์¶
ํจ์ ์ ์ธ์¶
function greet(name) {
return `์๋
, ${name}!`;
}
greet('ํ๊ธธ๋'); // "์๋
, ํ๊ธธ๋!"
ํจ์ ํํ์¶
const greet = function(name) {
return `์๋
, ${name}!`;
};
greet('ํ๊ธธ๋');
ํ์ดํ ํจ์ (Arrow Function)¶
// ๊ธฐ๋ณธ ํํ
const greet = (name) => {
return `์๋
, ${name}!`;
};
// ๋งค๊ฐ๋ณ์ 1๊ฐ: ๊ดํธ ์๋ต ๊ฐ๋ฅ
const greet = name => {
return `์๋
, ${name}!`;
};
// ํ ์ค์ด๋ฉด ์ค๊ดํธ์ return ์๋ต
const greet = name => `์๋
, ${name}!`;
// ๋งค๊ฐ๋ณ์ ์์
const sayHello = () => '์๋
!';
// ๊ฐ์ฒด ๋ฐํ (๊ดํธ ํ์)
const createUser = name => ({ name, created: Date.now() });
๋งค๊ฐ๋ณ์¶
// ๊ธฐ๋ณธ๊ฐ
function greet(name = '์๋') {
return `์๋
, ${name}!`;
}
greet(); // "์๋
, ์๋!"
greet('์ฒ ์'); // "์๋
, ์ฒ ์!"
// ๋๋จธ์ง ๋งค๊ฐ๋ณ์
function sum(...numbers) {
return numbers.reduce((a, b) => a + b, 0);
}
sum(1, 2, 3, 4); // 10
// ๊ตฌ์กฐ ๋ถํด
function printUser({ name, age }) {
console.log(`${name}์ ${age}์ด`);
}
printUser({ name: 'ํ๊ธธ๋', age: 30 });
์ฝ๋ฐฑ ํจ์¶
function processData(data, callback) {
// ๋ฐ์ดํฐ ์ฒ๋ฆฌ
const result = data.toUpperCase();
callback(result);
}
processData('hello', function(result) {
console.log(result); // "HELLO"
});
// ํ์ดํ ํจ์๋ก
processData('hello', result => console.log(result));
์ฆ์ ์คํ ํจ์ (IIFE)¶
(function() {
const private = '์ธ๋ถ์์ ์ ๊ทผ ๋ถ๊ฐ';
console.log('์ฆ์ ์คํ');
})();
// ํ์ดํ ํจ์
(() => {
console.log('์ฆ์ ์คํ');
})();
๋ฐฐ์ด¶
๋ฐฐ์ด ์์ฑ¶
// ๋ฆฌํฐ๋ด (๊ถ์ฅ)
const arr1 = [1, 2, 3];
// ์์ฑ์
const arr2 = new Array(3); // [empty ร 3]
const arr3 = Array.of(1, 2, 3); // [1, 2, 3]
const arr4 = Array.from('abc'); // ['a', 'b', 'c']
๋ฐฐ์ด ์ ๊ทผ¶
const fruits = ['์ฌ๊ณผ', '๋ฐ๋๋', '์ค๋ ์ง'];
fruits[0] // '์ฌ๊ณผ'
fruits[2] // '์ค๋ ์ง'
fruits.length // 3
fruits.at(-1) // '์ค๋ ์ง' (๋ง์ง๋ง ์์)
fruits.at(-2) // '๋ฐ๋๋'
๋ฐฐ์ด ์์ ¶
const arr = [1, 2, 3];
// ์ถ๊ฐ
arr.push(4); // ๋์ ์ถ๊ฐ: [1, 2, 3, 4]
arr.unshift(0); // ์์ ์ถ๊ฐ: [0, 1, 2, 3, 4]
// ์ ๊ฑฐ
arr.pop(); // ๋์์ ์ ๊ฑฐ: [0, 1, 2, 3]
arr.shift(); // ์์์ ์ ๊ฑฐ: [1, 2, 3]
// ํน์ ์์น ์์
arr.splice(1, 1); // ์ธ๋ฑ์ค 1์์ 1๊ฐ ์ ๊ฑฐ: [1, 3]
arr.splice(1, 0, 'a', 'b'); // ์ธ๋ฑ์ค 1์ ์ถ๊ฐ: [1, 'a', 'b', 3]
arr.splice(1, 1, 'x'); // ์ธ๋ฑ์ค 1์ ๊ต์ฒด: [1, 'x', 'b', 3]
๋ฐฐ์ด ๊ฒ์¶
const arr = [1, 2, 3, 2, 1];
arr.indexOf(2) // 1 (์ฒซ ๋ฒ์งธ ์ธ๋ฑ์ค)
arr.lastIndexOf(2) // 3 (๋ง์ง๋ง ์ธ๋ฑ์ค)
arr.includes(2) // true (ํฌํจ ์ฌ๋ถ)
// ์กฐ๊ฑด์ผ๋ก ๊ฒ์
const users = [
{ id: 1, name: 'Kim' },
{ id: 2, name: 'Lee' }
];
users.find(u => u.id === 1); // { id: 1, name: 'Kim' }
users.findIndex(u => u.id === 1); // 0
๋ฐฐ์ด ๋ฐ๋ณต ๋ฉ์๋¶
const numbers = [1, 2, 3, 4, 5];
// forEach: ๊ฐ ์์ ์คํ (๋ฐํ๊ฐ ์์)
numbers.forEach((num, index) => {
console.log(index, num);
});
// map: ๋ณํ๋ ์ ๋ฐฐ์ด ๋ฐํ
const doubled = numbers.map(n => n * 2);
// [2, 4, 6, 8, 10]
// filter: ์กฐ๊ฑด์ ๋ง๋ ์์๋ง
const evens = numbers.filter(n => n % 2 === 0);
// [2, 4]
// reduce: ๋์ ๊ณ์ฐ
const sum = numbers.reduce((acc, cur) => acc + cur, 0);
// 15
// every: ๋ชจ๋ ์์๊ฐ ์กฐ๊ฑด ์ถฉ์กฑ?
numbers.every(n => n > 0); // true
// some: ํ๋๋ผ๋ ์กฐ๊ฑด ์ถฉ์กฑ?
numbers.some(n => n > 4); // true
๋ฐฐ์ด ์ ๋ ฌ¶
const arr = [3, 1, 4, 1, 5];
// ์ค๋ฆ์ฐจ์
arr.sort((a, b) => a - b); // [1, 1, 3, 4, 5]
// ๋ด๋ฆผ์ฐจ์
arr.sort((a, b) => b - a); // [5, 4, 3, 1, 1]
// ๋ฌธ์์ด ์ ๋ ฌ
const names = ['ํ๊ธธ๋', '๊น์ฒ ์', '์ด์ํฌ'];
names.sort(); // ['๊น์ฒ ์', '์ด์ํฌ', 'ํ๊ธธ๋']
// ์ญ์
arr.reverse(); // [1, 1, 4, 3, 5]
๋ฐฐ์ด ๋ณํ¶
const arr = [1, 2, 3];
// ๋ณต์ฌ
const copy = [...arr]; // ์คํ๋ ๋
const copy2 = arr.slice(); // slice
// ํฉ์น๊ธฐ
const merged = [...arr, ...[4, 5]]; // [1, 2, 3, 4, 5]
const merged2 = arr.concat([4, 5]);
// ํํํ
const nested = [1, [2, [3, 4]]];
nested.flat(); // [1, 2, [3, 4]]
nested.flat(2); // [1, 2, 3, 4]
nested.flat(Infinity); // ์์ ํํํ
// ๋ฌธ์์ด๋ก
[1, 2, 3].join('-'); // "1-2-3"
๊ฐ์ฒด¶
๊ฐ์ฒด ์์ฑ¶
// ๋ฆฌํฐ๋ด (๊ถ์ฅ)
const user = {
name: 'ํ๊ธธ๋',
age: 30,
email: 'hong@example.com'
};
// ์์ฑ์
const obj = new Object();
obj.name = 'ํ๊ธธ๋';
๊ฐ์ฒด ์ ๊ทผ¶
const user = { name: 'ํ๊ธธ๋', age: 30 };
// ์ ํ๊ธฐ๋ฒ
user.name // 'ํ๊ธธ๋'
user.age // 30
// ๋๊ดํธ ํ๊ธฐ๋ฒ (๋์ ํค)
user['name'] // 'ํ๊ธธ๋'
const key = 'age';
user[key] // 30
๊ฐ์ฒด ์์ ¶
const user = { name: 'ํ๊ธธ๋' };
// ์ถ๊ฐ/์์
user.age = 30;
user['email'] = 'hong@example.com';
// ์ญ์
delete user.email;
// ์์ฑ ์กด์ฌ ํ์ธ
'name' in user // true
user.hasOwnProperty('name') // true
๊ฐ์ฒด ์ํ¶
const user = { name: 'ํ๊ธธ๋', age: 30 };
// for...in
for (const key in user) {
console.log(key, user[key]);
}
// Object ๋ฉ์๋
Object.keys(user) // ['name', 'age']
Object.values(user) // ['ํ๊ธธ๋', 30]
Object.entries(user) // [['name', 'ํ๊ธธ๋'], ['age', 30]]
// entries๋ก ์ํ
for (const [key, value] of Object.entries(user)) {
console.log(key, value);
}
๊ฐ์ฒด ๋ณต์ฌ/๋ณํฉ¶
const user = { name: 'ํ๊ธธ๋', age: 30 };
// ์์ ๋ณต์ฌ
const copy1 = { ...user }; // ์คํ๋ ๋
const copy2 = Object.assign({}, user);
// ๋ณํฉ
const merged = { ...user, city: '์์ธ' };
const merged2 = Object.assign({}, user, { city: '์์ธ' });
// ๊น์ ๋ณต์ฌ (์ค์ฒฉ ๊ฐ์ฒด)
const deep = JSON.parse(JSON.stringify(user));
const deep2 = structuredClone(user); // ์ต์ ๋ธ๋ผ์ฐ์
๋จ์ถ ์์ฑ¶
const name = 'ํ๊ธธ๋';
const age = 30;
// ๊ธฐ์กด ๋ฐฉ์
const user1 = { name: name, age: age };
// ๋จ์ถ ์์ฑ
const user2 = { name, age };
๊ณ์ฐ๋ ์์ฑ๋ช ¶
const key = 'email';
const user = {
name: 'ํ๊ธธ๋',
[key]: 'hong@example.com',
['get' + 'Age']() { return 30; }
};
user.email // 'hong@example.com'
user.getAge() // 30
๋ฉ์๋¶
const user = {
name: 'ํ๊ธธ๋',
// ๋ฉ์๋
greet() {
return `์๋
, ${this.name}!`;
},
// ํ์ดํ ํจ์ (this ์ฃผ์!)
// this๊ฐ ์์ ์ค์ฝํ๋ฅผ ์ฐธ์กฐ
badGreet: () => {
return `์๋
, ${this.name}!`; // this๋ user๊ฐ ์๋!
}
};
user.greet(); // "์๋
, ํ๊ธธ๋!"
ES6+ ๋ฌธ๋ฒ¶
ํ ํ๋ฆฟ ๋ฆฌํฐ๋ด¶
const name = 'ํ๊ธธ๋';
const age = 30;
// ๊ธฐ์กด ๋ฐฉ์
const msg1 = '์ด๋ฆ: ' + name + ', ๋์ด: ' + age;
// ํ
ํ๋ฆฟ ๋ฆฌํฐ๋ด
const msg2 = `์ด๋ฆ: ${name}, ๋์ด: ${age}`;
// ์ฌ๋ฌ ์ค
const html = `
<div>
<h1>${name}</h1>
<p>${age}์ด</p>
</div>
`;
// ํํ์ ์ฌ์ฉ
const result = `ํฉ๊ณ: ${10 + 20}`;
const status = `์ํ: ${age >= 18 ? '์ฑ์ธ' : '๋ฏธ์ฑ๋
'}`;
๊ตฌ์กฐ ๋ถํด ํ ๋น¶
// ๋ฐฐ์ด ๊ตฌ์กฐ ๋ถํด
const [a, b, c] = [1, 2, 3];
const [first, , third] = [1, 2, 3]; // ๊ฑด๋๋ฐ๊ธฐ
const [x, ...rest] = [1, 2, 3, 4]; // ๋๋จธ์ง
// ๊ฐ์ฒด ๊ตฌ์กฐ ๋ถํด
const { name, age } = { name: 'ํ๊ธธ๋', age: 30 };
// ๊ธฐ๋ณธ๊ฐ
const { name, city = '์์ธ' } = { name: 'ํ๊ธธ๋' };
// ์ด๋ฆ ๋ณ๊ฒฝ
const { name: userName, age: userAge } = user;
// ์ค์ฒฉ
const { address: { city } } = {
address: { city: '์์ธ' }
};
// ํจ์ ๋งค๊ฐ๋ณ์
function greet({ name, age = 0 }) {
console.log(`${name}, ${age}์ด`);
}
์คํ๋ ๋ ์ฐ์ฐ์¶
// ๋ฐฐ์ด
const arr1 = [1, 2, 3];
const arr2 = [...arr1, 4, 5]; // [1, 2, 3, 4, 5]
// ๊ฐ์ฒด
const obj1 = { a: 1, b: 2 };
const obj2 = { ...obj1, c: 3 }; // { a: 1, b: 2, c: 3 }
// ํจ์ ํธ์ถ
const numbers = [1, 2, 3];
Math.max(...numbers); // 3
// ๋ฌธ์์ด โ ๋ฐฐ์ด
[...'hello'] // ['h', 'e', 'l', 'l', 'o']
Optional Chaining (?.)¶
const user = {
name: 'ํ๊ธธ๋',
address: {
city: '์์ธ'
}
};
// ๊ธฐ์กด ๋ฐฉ์
const city1 = user && user.address && user.address.city;
// Optional Chaining
const city2 = user?.address?.city;
// ๋ฐฐ์ด
const first = arr?.[0];
// ํจ์
const result = obj.method?.();
Nullish Coalescing (??)¶
// || ์ ๋ค๋ฅด๊ฒ null/undefined๋ง ์ฒดํฌ
const value1 = null ?? '๊ธฐ๋ณธ๊ฐ'; // '๊ธฐ๋ณธ๊ฐ'
const value2 = undefined ?? '๊ธฐ๋ณธ๊ฐ'; // '๊ธฐ๋ณธ๊ฐ'
const value3 = 0 ?? '๊ธฐ๋ณธ๊ฐ'; // 0
const value4 = '' ?? '๊ธฐ๋ณธ๊ฐ'; // ''
const value5 = false ?? '๊ธฐ๋ณธ๊ฐ'; // false
์ฐ์ต ๋ฌธ์ ¶
๋ฌธ์ 1: ๋ณ์์ ์กฐ๊ฑด๋ฌธ¶
๋์ด๋ฅผ ๋ฐ์ ๊ตฌ๋ถํ๋ ํจ์๋ฅผ ์์ฑํ์ธ์. - 0-12: "์ด๋ฆฐ์ด" - 13-19: "์ฒญ์๋ " - 20-64: "์ฑ์ธ" - 65+: "๋ ธ์ธ"
์ ๋ต ๋ณด๊ธฐ
function getAgeGroup(age) {
if (age < 0) return '์๋ชป๋ ๋์ด';
if (age <= 12) return '์ด๋ฆฐ์ด';
if (age <= 19) return '์ฒญ์๋
';
if (age <= 64) return '์ฑ์ธ';
return '๋
ธ์ธ';
}
๋ฌธ์ 2: ๋ฐฐ์ด ๋ฉ์๋¶
์ซ์ ๋ฐฐ์ด์์ ์ง์๋ง ๊ณจ๋ผ ์ ๊ณฑํ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ์ธ์.
// ์
๋ ฅ: [1, 2, 3, 4, 5, 6]
// ์ถ๋ ฅ: [4, 16, 36]
์ ๋ต ๋ณด๊ธฐ
const numbers = [1, 2, 3, 4, 5, 6];
const result = numbers
.filter(n => n % 2 === 0)
.map(n => n ** 2);
console.log(result); // [4, 16, 36]
๋ฌธ์ 3: ๊ฐ์ฒด ๋ค๋ฃจ๊ธฐ¶
์ฌ์ฉ์ ๋ฐฐ์ด์์ ํน์ ์กฐ๊ฑด์ ์ฌ์ฉ์๋ฅผ ์ฐพ๊ณ ์ ๋ณด๋ฅผ ์ถ๋ ฅํ์ธ์.
const users = [
{ id: 1, name: 'Kim', age: 25 },
{ id: 2, name: 'Lee', age: 30 },
{ id: 3, name: 'Park', age: 28 }
];
// ๋์ด๊ฐ 28 ์ด์์ธ ์ฌ์ฉ์์ ์ด๋ฆ๋ง ๋ฐฐ์ด๋ก ๋ฐํ
์ ๋ต ๋ณด๊ธฐ
const result = users
.filter(user => user.age >= 28)
.map(user => user.name);
console.log(result); // ['Lee', 'Park']
๋ค์ ๋จ๊ณ¶
- 07_JS_Events_DOM.md - DOM ์กฐ์๊ณผ ์ด๋ฒคํธ ํธ๋ค๋ง