1<!DOCTYPE html>
2<html lang="ko">
3<head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <title>JavaScript ๊ธฐ์ด ์์ </title>
7 <style>
8 body {
9 font-family: 'Segoe UI', Tahoma, sans-serif;
10 max-width: 800px;
11 margin: 0 auto;
12 padding: 20px;
13 background-color: #f5f5f5;
14 }
15 h1 { text-align: center; color: #333; }
16 h2 { color: #2c3e50; border-bottom: 2px solid #f39c12; padding-bottom: 10px; }
17 .section {
18 background: white;
19 padding: 20px;
20 margin: 20px 0;
21 border-radius: 8px;
22 box-shadow: 0 2px 5px rgba(0,0,0,0.1);
23 }
24 .output {
25 background: #2c3e50;
26 color: #2ecc71;
27 padding: 15px;
28 border-radius: 4px;
29 font-family: monospace;
30 white-space: pre-wrap;
31 max-height: 300px;
32 overflow-y: auto;
33 }
34 button {
35 background: #3498db;
36 color: white;
37 border: none;
38 padding: 10px 20px;
39 border-radius: 4px;
40 cursor: pointer;
41 margin: 5px;
42 }
43 button:hover { background: #2980b9; }
44 code {
45 background: #ecf0f1;
46 padding: 2px 6px;
47 border-radius: 3px;
48 }
49 pre {
50 background: #ecf0f1;
51 padding: 15px;
52 border-radius: 4px;
53 overflow-x: auto;
54 }
55 </style>
56</head>
57<body>
58 <h1>JavaScript ๊ธฐ์ด ์์ </h1>
59 <p style="text-align: center;">F12๋ฅผ ๋๋ฌ ๊ฐ๋ฐ์ ๋๊ตฌ์ ์ฝ์์ ํ์ธํ์ธ์.</p>
60
61 <!-- ๊ฒฐ๊ณผ ์ถ๋ ฅ ์์ญ -->
62 <div class="section">
63 <h2>์ถ๋ ฅ ๊ฒฐ๊ณผ</h2>
64 <div class="output" id="output">๊ฒฐ๊ณผ๊ฐ ์ฌ๊ธฐ์ ํ์๋ฉ๋๋ค.</div>
65 <button onclick="clearOutput()">์ด๊ธฐํ</button>
66 </div>
67
68 <!-- 1. ๋ณ์ -->
69 <div class="section">
70 <h2>1. ๋ณ์ (var, let, const)</h2>
71 <button onclick="runVariables()">์คํ</button>
72 <pre>
73// var - ํจ์ ์ค์ฝํ (๋น๊ถ์ฅ)
74var oldVar = "I'm var";
75
76// let - ๋ธ๋ก ์ค์ฝํ, ์ฌํ ๋น ๊ฐ๋ฅ
77let count = 0;
78count = 1;
79
80// const - ๋ธ๋ก ์ค์ฝํ, ์ฌํ ๋น ๋ถ๊ฐ
81const PI = 3.14159;
82const person = { name: "ํ๊ธธ๋" };
83person.name = "๊น์ฒ ์"; // ๊ฐ์ฒด ์์ฑ์ ๋ณ๊ฒฝ ๊ฐ๋ฅ
84 </pre>
85 </div>
86
87 <!-- 2. ๋ฐ์ดํฐ ํ์
-->
88 <div class="section">
89 <h2>2. ๋ฐ์ดํฐ ํ์
</h2>
90 <button onclick="runDataTypes()">์คํ</button>
91 <pre>
92// ๊ธฐ๋ณธ ํ์
(Primitive)
93let str = "๋ฌธ์์ด"; // String
94let num = 42; // Number
95let float = 3.14; // Number (์ ์์ ๊ตฌ๋ถ ์์)
96let bool = true; // Boolean
97let empty = null; // Null
98let notDefined; // Undefined
99let sym = Symbol("id"); // Symbol
100let big = 9007199254740991n; // BigInt
101
102// ์ฐธ์กฐ ํ์
(Reference)
103let obj = { key: "value" }; // Object
104let arr = [1, 2, 3]; // Array
105let func = function() {}; // Function
106 </pre>
107 </div>
108
109 <!-- 3. ์ฐ์ฐ์ -->
110 <div class="section">
111 <h2>3. ์ฐ์ฐ์</h2>
112 <button onclick="runOperators()">์คํ</button>
113 <pre>
114// ์ฐ์ ์ฐ์ฐ์
115let a = 10, b = 3;
116// +, -, *, /, %, **
117
118// ๋น๊ต ์ฐ์ฐ์
119// ==, ===, !=, !==, <, >, <=, >=
120// == vs === (ํ์
๋น๊ต ์ฐจ์ด)
121
122// ๋
ผ๋ฆฌ ์ฐ์ฐ์
123// && (AND), || (OR), ! (NOT)
124
125// ์ผํญ ์ฐ์ฐ์
126let result = a > b ? "ํฌ๋ค" : "์๋ค";
127
128// Nullish coalescing (??)
129let value = null ?? "๊ธฐ๋ณธ๊ฐ";
130
131// Optional chaining (?.)
132let obj = { nested: { value: 42 } };
133let safe = obj?.nested?.value;
134 </pre>
135 </div>
136
137 <!-- 4. ์กฐ๊ฑด๋ฌธ -->
138 <div class="section">
139 <h2>4. ์กฐ๊ฑด๋ฌธ</h2>
140 <button onclick="runConditions()">์คํ</button>
141 <pre>
142// if-else
143if (์กฐ๊ฑด1) {
144 // ์คํ
145} else if (์กฐ๊ฑด2) {
146 // ์คํ
147} else {
148 // ์คํ
149}
150
151// switch
152switch (๊ฐ) {
153 case 'A':
154 break;
155 case 'B':
156 break;
157 default:
158 break;
159}
160 </pre>
161 </div>
162
163 <!-- 5. ๋ฐ๋ณต๋ฌธ -->
164 <div class="section">
165 <h2>5. ๋ฐ๋ณต๋ฌธ</h2>
166 <button onclick="runLoops()">์คํ</button>
167 <pre>
168// for
169for (let i = 0; i < 5; i++) { }
170
171// while
172while (์กฐ๊ฑด) { }
173
174// do-while
175do { } while (์กฐ๊ฑด);
176
177// for...of (๋ฐฐ์ด ์ํ)
178for (const item of array) { }
179
180// for...in (๊ฐ์ฒด ํค ์ํ)
181for (const key in object) { }
182
183// forEach, map, filter, reduce
184 </pre>
185 </div>
186
187 <!-- 6. ํจ์ -->
188 <div class="section">
189 <h2>6. ํจ์</h2>
190 <button onclick="runFunctions()">์คํ</button>
191 <pre>
192// ํจ์ ์ ์ธ์
193function greet(name) {
194 return `Hello, ${name}!`;
195}
196
197// ํจ์ ํํ์
198const greet = function(name) {
199 return `Hello, ${name}!`;
200};
201
202// ํ์ดํ ํจ์
203const greet = (name) => `Hello, ${name}!`;
204
205// ๊ธฐ๋ณธ ๋งค๊ฐ๋ณ์
206const greet = (name = "Guest") => `Hello, ${name}!`;
207
208// Rest ํ๋ผ๋ฏธํฐ
209const sum = (...numbers) => numbers.reduce((a, b) => a + b, 0);
210
211// ์ฝ๋ฐฑ ํจ์
212setTimeout(() => console.log("1์ด ํ"), 1000);
213 </pre>
214 </div>
215
216 <!-- 7. ๋ฐฐ์ด -->
217 <div class="section">
218 <h2>7. ๋ฐฐ์ด</h2>
219 <button onclick="runArrays()">์คํ</button>
220 <pre>
221const fruits = ["์ฌ๊ณผ", "๋ฐ๋๋", "์ค๋ ์ง"];
222
223// ๊ธฐ๋ณธ ๋ฉ์๋
224fruits.push("ํฌ๋"); // ๋์ ์ถ๊ฐ
225fruits.pop(); // ๋์์ ์ ๊ฑฐ
226fruits.unshift("๋ธ๊ธฐ"); // ์์ ์ถ๊ฐ
227fruits.shift(); // ์์์ ์ ๊ฑฐ
228
229// ๊ณ ์ฐจ ํจ์
230const numbers = [1, 2, 3, 4, 5];
231numbers.map(n => n * 2); // [2, 4, 6, 8, 10]
232numbers.filter(n => n > 2); // [3, 4, 5]
233numbers.find(n => n > 2); // 3
234numbers.reduce((acc, n) => acc + n, 0); // 15
235numbers.every(n => n > 0); // true
236numbers.some(n => n > 4); // true
237 </pre>
238 </div>
239
240 <!-- 8. ๊ฐ์ฒด -->
241 <div class="section">
242 <h2>8. ๊ฐ์ฒด</h2>
243 <button onclick="runObjects()">์คํ</button>
244 <pre>
245// ๊ฐ์ฒด ๋ฆฌํฐ๋ด
246const person = {
247 name: "ํ๊ธธ๋",
248 age: 25,
249 greet() {
250 return `์๋
ํ์ธ์, ${this.name}์
๋๋ค.`;
251 }
252};
253
254// ์์ฑ ์ ๊ทผ
255person.name; // ์ ํ๊ธฐ๋ฒ
256person["age"]; // ๋๊ดํธ ํ๊ธฐ๋ฒ
257
258// ๊ตฌ์กฐ ๋ถํด ํ ๋น
259const { name, age } = person;
260
261// ์คํ๋ ๋ ์ฐ์ฐ์
262const newPerson = { ...person, city: "์์ธ" };
263
264// Object ๋ฉ์๋
265Object.keys(person); // ["name", "age", "greet"]
266Object.values(person); // ["ํ๊ธธ๋", 25, ...]
267Object.entries(person); // [["name", "ํ๊ธธ๋"], ...]
268 </pre>
269 </div>
270
271 <!-- 9. ํด๋์ค -->
272 <div class="section">
273 <h2>9. ํด๋์ค (ES6)</h2>
274 <button onclick="runClasses()">์คํ</button>
275 <pre>
276class Animal {
277 constructor(name) {
278 this.name = name;
279 }
280
281 speak() {
282 return `${this.name} makes a sound.`;
283 }
284}
285
286class Dog extends Animal {
287 constructor(name, breed) {
288 super(name);
289 this.breed = breed;
290 }
291
292 speak() {
293 return `${this.name} barks!`;
294 }
295}
296
297const dog = new Dog("๋ฉ๋ฉ์ด", "์ง๋๊ฐ");
298 </pre>
299 </div>
300
301 <!-- 10. ๋ชจ๋ -->
302 <div class="section">
303 <h2>10. ๋ชจ๋ (ES6)</h2>
304 <pre>
305// math.js
306export const PI = 3.14159;
307export function add(a, b) { return a + b; }
308export default class Calculator { }
309
310// main.js
311import Calculator, { PI, add } from './math.js';
312import * as math from './math.js';
313 </pre>
314 <p><em>๋ชจ๋์ ์๋ฒ ํ๊ฒฝ์์๋ง ๋์ํฉ๋๋ค.</em></p>
315 </div>
316
317 <script src="script.js"></script>
318</body>
319</html>