console.log(): ๊ฐ๋ฐ์๋๊ตฌ Console์ ์ถ๋ ฅalert(): ์๋ฆผ ํ์
(ํ์ธ ์ ๊น์ง ๋ค์ ์ฝ๋๊ฐ ๋ฉ์ถค)
const console1 = () => {
console.log("console1 ์ด๋ฒคํธ ํธ๋ค๋ฌ ์คํ");
};
[]๋ก ์์ฑ, ๋ค๋ฅธ ํ์
๋ ์์ด์ ๋ด์ ์ ์์push, pop: ๋ค์ ์ถ๊ฐ, ์ญ์ unshift, shift: ์์ ์ถ๊ฐ, ์ญ์ forEach: ๊ฐ ์์ ์ํ
const console2 = () => {
let arr = ["a", 10];
console.log("arr ===", arr);
for (let i = 0; i < 5; i++) arr[i] = i;
arr.push("hello");
console.log("arr(push ํ) ===", arr);
arr.forEach(v => console.log("forEach ===", v));
}
const console3 = () => {
const arr = [10, 20, 30, 40];
let total = 0;
for (let i = 0; i < arr.length; i++) {
total += arr[i];
}
alert(`๋ฐฐ์ด: ${arr.join(", ")} / ๋์ ํฉ === ${total}`);
}
const arrFunction = () => {
const test = () => console.log("test ํจ์ ์คํ");
const arr = [10, "20", test];
const arr2 = [10, "20", () => console.log("๋ฆฌํฐ๋ด ์ธํ
ํจ์ ์คํ")];
arr[2](); // test ์คํ
arr2[2](); // ๋ฆฌํฐ๋ด ํจ์ ์คํ
}
// var: ํจ์ ์ค์ฝํ(์ฌ์ ์ธ ๊ฐ๋ฅ) โ ์์ฆ์ ์ง์ํ๋ ํธ
const varExample = () => {
var a = 10;
var a = 20; // ์ฌ์ ์ธ ๊ฐ๋ฅ
console.log("var a ===", a);
}
// let: ๋ธ๋ก ์ค์ฝํ, ์ฌํ ๋น ๊ฐ๋ฅ(์ฌ์ ์ธ ๋ถ๊ฐ)
const letExample = () => {
let a = 10;
a = 20;
console.log("let a ===", a);
}
// const: ๋ธ๋ก ์ค์ฝํ, ์ฌํ ๋น ๋ถ๊ฐ
const constExample = () => {
const a = 10;
// a = 20; // ์ฌํ ๋น ๋ถ๊ฐ(์๋ฌ)
console.log("const a ===", a);
}
// getElementById: id๋ก 1๊ฐ ์ ํ
const changeText1 = () => {
const elem = document.getElementById("change1");
elem.innerHTML = "hello java";
}
// getElementsByClassName: class๋ก ์ฌ๋ฌ ๊ฐ ์ ํ(HTMLCollection)
const changeText2 = () => {
const elems = document.getElementsByClassName("change2");
for (let i = 0; i < elems.length; i++) {
elems[i].innerHTML = `${i}๋ฒ ์ธ๋ฑ์ค === hello python`;
}
}
// getElementsByTagName: ํ๊ทธ๋ก ์ฌ๋ฌ ๊ฐ ์ ํ
const changeText3 = () => {
const elems = document.getElementsByTagName("li");
for (let i = 0; i < elems.length; i++) {
elems[i].innerHTML = `${i}๋ฒ ์ธ๋ฑ์ค === hello C++`;
}
}
hello world
hello world
hello world
hello world
hello world