####JavaScript Learning
- ๋ธ๋ผ์ฐ์ -> HTML -> CSS, JS
- ๋ธ๋ผ์ฐ์ ๋ HTML์ ์คํ์ํค๊ณ HTML์ CSS, JS๋ฅผ ๋ถ๋ฌ์ด
- alert(""); //์๋ฆผ์ฐฝ ๋์ฐ๊ธฐ
CSS, JS ํ์ผ ๋ถ๋ฌ์ค๊ธฐ :
<link rel="stylesheet" href="css ํ์ผ๋ช
" /> // css ํ์ผ ๋ธ๋ฌ์ค๊ธฐ
// ๋จ์ถ์ด link:css - head์ ์ฐ๊ธฐ
<script src="js ํ์ผ๋ช
"></script>
// JS ํ์ผ ๋ธ๋ฌ์ค๊ธฐ - body์ ์ฐ๊ธฐ์์ :
alert("hi"); //์๋ฆผ์ฐฝ์ hi ๋์ฐ๊ธฐ-
์๋ฐ์คํฌ๋ฆฝํธ๋ ๋ฐ์ดํฐ ํ์ ์ ์์์ ์ธ์ํจ
-
๋ฐ์ดํฐ ํ์ ์๋ number(integer, float), string ๋ฑ์ด ์์
-
console.log(); // ์ฝ์์ ๊ฐ์ด๋ ์ฐ์ฐ ๋์ฐ๊ธฐ
-
const ๋ณ์๋ช = ๊ฐ; // variable, ๋ณํ์ง ์๋ ๊ฐ, ๋ณ์ ์ ์ธ์ ์ฌ์ฉ, ์ฌ์ ์ ๋ถ๊ฐ๋ฅ
-
๊ฐ != "๊ฐ" // number ๋๋ float != string ๊ฐ์ง์๋ค
-
const veryLongVariableName = 0; //๋ณ์๋ช ์ ๊ณต๋ฐฑ์ด ํ์ํ ๋์๋ ๋๋ฌธ์๋ก ๋์ฒด
- const very_long_variable_name = 0; //ํ์ด์ฌ์ ๋ฐ์ค
์์ :
console.log(54545454); // ์ฝ์์ 54545454 ์ถ๋ ฅ, number(integer)
console.log("lalalalalala"); //์ฝ์์ lalalalalala ์ถ๋ ฅ, string
const a = 5; // a ๋ 5 ๋ผ๋ ๋ณ์ ์ ์ธ
const b = 2; // b ๋ 2 ๋ผ๋ ๋ณ์ ์ ์ธ
const veryLongVariableName = 0; // veryLongVariableName ๋ 0 ๋ณ์ ์ ์ธ
const myName = "mayu"; // myName ์ "mayu" ๋ผ๋ ๋ณ์ ์ ์ธ
console.log(a + b); // ๋ณ์ a(=5) ์ b(=2) ์ฐ์ฐ ๊ฒฐ๊ณผ ์ถ๋ ฅ
console.log(a \* b); // ๋ณ์ a(=5) ์ b(=2) ์ฐ์ฐ ๊ฒฐ๊ณผ ์ถ๋ ฅ
console.log(a / b); // ๋ณ์ a(=5) ์ b(=2) ์ฐ์ฐ ๊ฒฐ๊ณผ ์ถ๋ ฅ
console.log("hello " + myName); // "hello" ์ myName(="mayu") ์ ํฉ ์ถ๋ ฅ-
๋ณ์ ์ ์ธ(variable)
-
let ๋ณ์๋ช = ๊ฐ;
- ๊ฐ ์ฌ์ ์ ๊ฐ๋ฅ
- ๋ฐ์ดํฐ ์ฌ์ ์๊ฐ ํ์ํ ๋์๋ง ์ฌ์ฉ
- ๋ฐ์ดํฐ๊ฐ ๋ฐ๋ ๊ฒ์ด๋ผ ์์ ํ ์ ์์
-
const ๋ณ์๋ช = ๊ฐ;
- ๋ณํ์ง ์๋ ๊ฐ ์ ์ธ์ ์ฌ์ฉ
- ๋ฐ์ดํฐ ์ฌ์ ์ ๋ถ๊ฐ๋ฅ
- ๋ฐ์ดํฐ๊ฐ ๋ฐ๋์ง ์์ ๊ฒ์ด๋ผ๊ณ ์์ ํ ์ ์์
- ๋๋ถ๋ถ const ์ฌ์ฉ
-
var ๋ณ์๋ช = ๊ฐ;
- variable, ์ ํ์ ์์ง๋ง ๋ณดํธ๋ฐ์ ์ ์์
- ์ฌ์ฉํ์ง ๋ง ๊ฒ
- ์์ ์๋ฐ์คํฌ๋ฆฝํธ ์ธ์ด
-
์์ :
const a = 5; // a ๋ 5 ๋ผ๋ ๋ณ์ ์ ์ธ, ์ฌ์ ์ ๋ถ๊ฐ๋ฅ
const b = 2; // b ๋ 2 ๋ผ๋ ๋ณ์ ์ ์ธ, ์ฌ์ ์ ๋ถ๊ฐ๋ฅ
let myName = "mayu"; // myName ์ "mayu" ๋ผ๋ ๋ณ์ ์ ์ธ, ์ฌ์ ์ ๊ฐ๋ฅ
console.log("hello " + myName); //"hello" ์ myName(="mayu") ํฉ์น ๊ฒฐ๊ณผ ์ถ๋ ฅ
myName = "kimmayu"; // myName ์ ๋ฐ์ดํฐ๋ฅผ "kimmayu" ๋ก ์ฌ์ ์
console.log("your new name is " + myName);
// "your new name is " ์ myName(="kimmayu") ํฉ์น ๊ฒฐ๊ณผ ์ถ๋ ฅ-
๋ฐ์ดํฐ ํ์
-
Boolean
- ์ฐธ๊ณผ ๊ฑฐ์ง์ ๊ฐ์ง(true, false / 1, 0)
const ๋ณ์๋ช = true; // ๋ณ์์ ์ฐธ์ ์ ์ํจ const ๋ณ์๋ช = false; // ๋ณ์์ ๊ฑฐ์ง์ ์ ์ํจ
- ์ฐธ๊ณผ ๊ฑฐ์ง์ ๊ฐ์ง(true, false / 1, 0)
-
null
- ์๋์ ์ผ๋ก ๊ฐ์ ๋น์๋์ ์ํ, ๊ฐ์ ์ ์ํ์ง ์์
- ์์ฐ์ ์ผ๋ก ๋ฐ์ํ์ง ์์
- ํ์ด์ฌ์์๋ None, go์์๋ Nil
-
undefined
- ๋ณ์๋ ์์ง๋ง ์ ์๋์ง ์์, ๊ณต๊ฐ์ ์๋๋ฐ ๊ฐ์ด ๋ค์ด๊ฐ์ง ์์
- ์์ฐ์ ์ผ๋ก ๋ฐ์ํจ
- ๋ณ์๋ฅผ ์ ์ธํ ๋์ ๊ฐ์ ์ง์ ํ์ง ์์ผ๋ฉด undefined ๊ฐ ์ ์ธ๋จ
-
์์ :
const amIFat = true; // ๋ณ์ amIFat ์ ์ฐธ์ผ๋ก ์ ์ํจ
const amIFat = false; // ๋ณ์ amIFat ์ ์ฐธ์ผ๋ก ์ ์ํจ
const amIFat = null; // ๋ณ์ amIFat ์ null(๊ฐ์ ๋น์๋์ ์ํ) ์ผ๋ก ์ ์ํจ
let something; // ๋ณ์ something ์ ๊ฐ ์์ด ์ ์ํจ
console.log(something); // something ์ถ๋ ฅ, ๊ฐ์ด ์์ผ๋ฏ๋ก undefined ์ถ๋ ฅ-
const ๋ฆฌ์คํธ๋ช = [์์๋ค]; //๋ฆฌ์คํธ, array, ๋ฐฐ์ด : ๊ฐ ์์๋ค์ ๋ฐ์ดํฐ ํ์ ์๊ด์์ด ์ ์ ๊ฐ๋ฅ, ๋๊ดํธ ์ฌ์ฉ, ์์๋ค์ ์ผํ๋ก ๋ถ๋ฆฌ
-
console.log(๋ฆฌ์คํธ๋ช ); // ๋ฆฌ์คํธ๋ฅผ ์ถ๋ ฅํจ
- console.log(๋ฆฌ์คํธ๋ช [์ถ๋ ฅํ๋ ค๋ ๋ฐ์ดํฐ์ ์์๊ฐ]); // ๋ฆฌ์คํธ ์์ ์์๊ฐ ์์์ ๊ฐ์ ์ถ๋ ฅํจ, ์์๊ฐ์ ์ ์๋ ์์์ ๋ฐ์ดํฐ๊ฐ ์์ ๊ฒฝ์ฐ undefined ๊ฐ ์ถ๋ ฅ๋จ
-
๋ฆฌ์คํธ๋ช .push(๋ฐ์ดํฐ); // ๋ฆฌ์คํธ์ ๋ฐ์ดํฐ ์ถ๊ฐ
์์ :
const mon = "mon"; //๋ณ์ mon ์ "mon" ์ ์ ์
const tue = "tue"; //๋ณ์ tue ์ "tue" ์ ์ ์
const wed = "wed"; //๋ณ์ wed ์ "wed" ์ ์ ์
const thu = "thu"; //๋ณ์ thu ์ "thu" ์ ์ ์
const fri = "fri"; //๋ณ์ fri ์ "fri" ์ ์ ์
const sat = "sat"; //๋ณ์ sat ์ "sat" ์ ์ ์
const sun = "sun"; //๋ณ์ sun ์ "sun" ์ ์ ์
const dayOfWeek = mon + tue + wed + thu + fri + sat + sun;
// dayOfWeek ์ "montuewedthufrisatsun" ๋ฅผ ์ ์ํจ, ์์ ๋ณต์กํ ์ ์ธ์๋ณด๋ค ํจ์จ์ ์ด์ง๋ง ๊ฐ ์์๋ค์ ๋ถ๋ฌ์ฌ ์ ์๋ค๋ ๋จ์ ์ด ์์
const dayOfWeek = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];
// ๋ฆฌ์คํธ dayOfWeek ์ "mon", "tue", "wed", "thu", "fri", "sat", "sun" ์ ์ ์, ์์ ๋ณต์กํ ์ ์ธ์๋ณด๋ค ํจ์จ์ ์
const nonsense = [1, 2, "hello", false, null, true, undefined, "mayu"]; // ๋ฆฌ์คํธ nonsense ์ 1, 2, "hello" , false , null , true , undefined , "mayu" ๋ฅผ ์ ์
console.log(dayOfWeek[4]); // ๋ฆฌ์คํธ dayOfWeek ์ 4๋ฒ์งธ ํจ์ซ๊ฐ์ธ "fri"๊ฐ ์ถ๋ ฅ๋จ
dayOfWeek.push("sun"); // ๋ฆฌ์คํธ dayOfWeek ์ "sun" ์ ์ถ๊ฐํจ
console.log(dayOfWeek); // ๋ฆฌ์คํธ dayOfWeek ์"sun" ์ด ์ถ๊ฐ๋ ["mon", "tue", "wed", "thu", "fri", "sat", "sun", "sun"] ์ด ์ถ๋ ฅ๋จ
const toBuy = ["potato", "tomato", "pizza"]; // ๋ฆฌ์คํธ toBuy ์ "potato", "tomato", "pizza" ์ ์ ์
toBuy.push("kimbab"); // ๋ฆฌ์คํธ toBuy ์ "kimbab" ์ ์ถ๊ฐํจobject
const ์ค๋ธ์ ํธ๋ช = { ํน์ฑ : ๊ฐ, ํน์ฑ : ๊ฐ, ํน์ฑ : ๊ฐ, };
- ๊ฐ ์์์ ํน์ฑ ๋ถ์ฌํจ
- = ๋์ : ์ ์ฌ์ฉํจ
- ์์ ์ฌ์ ์, ์ถ๊ฐ ๊ฐ๋ฅ
- ๊ฐ ์์(ํน์ฑ : ๊ฐ) ๋ property ๋ผ ๋ถ๋ฆฌ๋ฉฐ ์ฝค๋ง(,) ๋ก ๊ตฌ๋ถ console.log ๋ ํน์ฑ : ๊ฐ ๊ณผ ๊ฐ์ ๊ตฌ์กฐ์ด๋ฏ๋ก ์ค๋ธ์ ํธ(console)์ ํน์ฑ ์ค ํ๋๋ก ๋ณผ ์ ์์ (ํน์ฑ : ๊ฐ) ์ (ํน์ฑ[๊ฐ]) ์ผ๋ก๋ ํํ ๊ฐ๋ฅ
const playerName = "mayu"; // playerName ์ "mayu" ์ ์
const playerPoints = 123456; // playerPoints ์ 123456 ์ ์
const playerHandsome = true; // playerHandsome ์ true ์ ์
const playerFat = "little bit"; // playerFat ์ "little bit" ์ ์
// ์ํ๋ ๊ฒฐ๊ณผ์ด์ง๋ง ์ฝ๋๊ฐ ๋นํจ์จ์ ์
const playerList = ["mayu", 123456, true, "little bit"];
const player = {
name: "mayu",
points: 10,
fat: true,
};
console.log(player);
console.log(player.name);
console.log(player["name"]);
player.fat = false;
player.lastName = "potato";function ํจ์๋ช (){ ๋ช ๋ น์ด; } //function, ํจ์ : ๊ณ์ ๋ฐ๋ณตํด์ ์ธ ์ ์๋ ์ฝ๋, ์ฝ๋ ์บก์ํ ์ฌ๋ฌ๋ฒ ์คํ //argument : function์ ์คํํ๋ ๋์ function์๊ฒ ์ ๋ณด๋ฅผ ๋ณด๋ผ ์ ์๋ ๋ฐฉ๋ฒ
ํจ์๋ช (๋ฐ์ดํฐ); // function์ ์คํ์ํค๋ ๋ฐฉ๋ฒ, function์ ๋ฐ์ดํฐ ๋ณด๋ด๊ธฐ
function sayHello() {
console.log("Hello my name is");
}
sayHello("mayu");// ๊ดํธ ์์ ๋ณ์๋ช ์ ๋ฃ์ด ์ ๋ณด๋ฅผ ๋ฐ์, ์ด ๋ณ์๋ค์ ํจ์ ์์๋ง ์กด์ฌ, ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ด์ง ์์ผ๋ฉด undefined๊ฐ ์ ์๋จ, ์ ์ธ๋ ๋ฐ์ดํฐ ์์๋๋ก ๋ณ์์ ์ ์๋จ
function sayHello(nameOfPerson, age) {
console.log("Hello my name is " + nameOfPerson + " and I'm " + age);
}
sayHello("mayu", 18);
sayHello("nico", 28);
sayHello("lynn", 21);
function plus(first, second) {
console.log(first, second);
}
console.log(first); // ์๋ฌ
function divide(a, b) {
console.log(a / b);
}
plus(60, 8);
divide(98, 20);
const player = {
sayHello: function (otherName) {
console.log("Hello " + otherName + " nice to meet you");
},
};
player.sayHello("lynn");
player.sayHello("mayu");๋ฐ์ดํฐ ํ์ ์๋ number(integer, float), string ๋ฑ์ด ์์
console.log(); //์ฝ์์ ๊ฐ์ด๋ ์ฐ์ฐ ๋์ฐ๊ธฐ
const ๋ณ์๋ช = ๊ฐ // variable, ๋ณํ์ง ์๋ ๊ฐ, ๋ณ์ ์ ์ธ์ ์ฌ์ฉ, ์ฌ์ ์ ๋ถ๊ฐ๋ฅ
let ๋ณ์๋ช = ๊ฐ //๋ณ์ ์ ์ธ์ ์ฌ์ฉ, ๊ฐ ์ฌ์ ์ ๊ฐ๋ฅ != const
var ๋ณ์๋ช = ๊ฐ // ์ ํ์ ์์ง๋ง ๋ณดํธ๋ฐ์ ์ ์์, ์์ ์๋ฐ์คํฌ๋ฆฝํธ ์ธ์ด, ์ฌ์ฉํ์ง ๋ง ๊ฒ
Boolean // ์ฐธ๊ณผ ๊ฑฐ์ง์ ๊ฐ์ง(1, 0 / true, false)
null // ์ ์๋์ง ์์, ๊ฐ์ด ์กด์ฌํ์ง ์์, ์์ฐ์ ์ผ๋ก ๋ฐ์ํ์ง ์์, ๊ฐ์ ๋น์๋์ ์ํ, ์๋์ , ๋ฐ์ดํฐ ํ์ (ํ์ด์ฌ์์๋ None, go์์๋ nil)
undefined // ๋ณ์๋ ์์ง๋ง ์ ์๋์ง ์์, ๊ณต๊ฐ์ ์๋๋ฐ ๊ฐ์ด ๋ค์ด๊ฐ์ง ์์, ์์ฐ์ ์ผ๋ก ๋ฐ์ํจ, ๋ฐ์ดํฐ ํ์
const ๋ฆฌ์คํธ๋ช = [์์๋ค]; //๋ฆฌ์คํธ, array, ๋ฐฐ์ด : ๊ฐ ์์๋ค์ ๋ฐ์ดํฐ ํ์ ์๊ด์์ด ์ ์ ๊ฐ๋ฅ, ๋๊ดํธ ์ฌ์ฉ, ์์๋ค์ ์ผํ๋ก ๋ถ๋ฆฌ
console.log(๋ฆฌ์คํธ๋ช ); // ๋ฆฌ์คํธ๋ฅผ ์ถ๋ ฅํจ
console.log(๋ฆฌ์คํธ๋ช [์ถ๋ ฅํ๋ ค๋ ๋ฐ์ดํฐ์ ์์๊ฐ]); // ๋ฆฌ์คํธ ์์ ์์๊ฐ ์์์ ๊ฐ์ ์ถ๋ ฅํจ, ์์๊ฐ์ ์ ์๋ ์์์ ๋ฐ์ดํฐ๊ฐ ์์ ๊ฒฝ์ฐ undefined ๊ฐ ์ถ๋ ฅ๋จ
๋ฆฌ์คํธ๋ช .push(๋ฐ์ดํฐ); // ๋ฆฌ์คํธ์ ๋ฐ์ดํฐ ์ถ๊ฐ
const ์ค๋ธ์ ํธ๋ช = { ํน์ฑ : ๊ฐ, ํน์ฑ : ๊ฐ, ํน์ฑ : ๊ฐ, }; //object : ๊ฐ ์์์ ํน์ฑ ๋ถ์ฌํจ = ๋์ : ์ ์ฌ์ฉํจ ๊ฐ ์์(ํน์ฑ : ๊ฐ) ๋ property ๋ก ์ฝค๋ง(,) ๋ก ๊ตฌ๋ถ console.log ๋ ํน์ฑ : ๊ฐ ์ ๊ตฌ์กฐ์ด๋ฏ๋ก ์ค๋ธ์ ํธ(console)์ ์์ ์ค ํ๋๋ก ๋ณผ ์ ์์ (ํน์ฑ : ๊ฐ) ์ (ํน์ฑ[๊ฐ]) ์ผ๋ก๋ ํํ ๊ฐ๋ฅ ์์ ์ฌ์ ์, ์ถ๊ฐ ๊ฐ๋ฅ
console.log(console); // js ๋ช
๋ น์ด ๋ณด๊ธฐ(console์ ์์๋ค)function ํจ์๋ช (๋ณ์๋ช ){ ๋ช ๋ น์ด; } //function, ํจ์ : ๊ณ์ ๋ฐ๋ณตํด์ ์ธ ์ ์๋ ์ฝ๋, ์ฝ๋ ์บก์ํ ์ฌ๋ฌ๋ฒ ์คํ //argument : function์ ์คํํ๋ ๋์ function์๊ฒ ์ ๋ณด๋ฅผ ๋ณด๋ผ ์ ์๋ ๋ฐฉ๋ฒ, ๊ดํธ ์์ ๋ณ์๋ช ์ ๋ฃ์ด ์ ๋ณด๋ฅผ ๋ฐ์, ์ด ๋ณ์๋ค์ ํจ์ ์์๋ง ์กด์ฌ, ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ด์ง ์์ผ๋ฉด undefined๊ฐ ์ ์๋จ, ์ ์ธ๋ ๋ฐ์ดํฐ ์์๋๋ก ๋ณ์์ ์ ์๋จ
ํจ์๋ช (๋ฐ์ดํฐ); // function์ ์คํ์ํค๋ ๋ฐฉ๋ฒ, function์ ๋ฐ์ดํฐ ๋ณด๋ด๊ธฐ
const calculator = {
plus: function (a, b) {
console.log(a + b);
},
minus: function (a, b) {
console.log(a - b);
},
times: function (a, b) {
console.log(a * b);
},
divide: function (a, b) {
console.log(a / b);
},
power: function (a, b) {
console.log(a ** b);
},
};
calculator.plus(2, 3);
calculator.minus(2, 3);
calculator.times(2, 3);
calculator.divide(2, 3);
calculator.power(2, 3);console.log ์ alert ๋ฑ์ ๊ฒฐ๊ณผ๊ฐ์ ๋จ๊ธฐ์ง ์์, return (๊ฐ์ ๋ฐํ๋ฐ์) ์ฌ์ฉ
const age = 96;
function calculateKrAge(ofAge) {
return ofAge + 2;
}
const KrAge = calculateKrAge(age);
console.log(KrAge);
const calculator = {
plus: function (a, b) {
return a + b;
},
minus: function (a, b) {
return a - b;
},
times: function (a, b) {
return a * b;
},
divide: function (a, b) {
return a / b;
},
power: function (a, b) {
return a ** b;
},
};
const pResult = calculator.plus(2, 3);
const mResult = calculator.minus(pResult, 10);
const tResult = calculator.times(10, mResult);
const dResult = calculator.divide(tResult, pResult);
const pwResult = calculator.power(dResult, mResult);
console.log(pwResult);return ํ์๋ function๋ฌธ์ด ์ข ๋ฃ๋จ, ๋ค์ ์ฝ๋๋ ์คํ๋์ง ์์ ํท๊ฐ๋ฆฌ๋ฉด ์ฐจ๊ทผ์ฐจ๊ทผ ๋์ฒด ํด ๋ณด๊ธฐ // ์๋ฐ ๋ฌธ๋ฒ
const age = prompt("How old are you?");
//prompt ๊ฐ์ ์
๋ ฅ๋ฐ์ ๋ ๊น์ง JS๋ฅผ ์ ์ง์ํด, ์ฌ์ฉํ์ง ์์, CSS ์ ์ฉ ๋ถ๊ฐ, string์ผ๋ก ๋ฐ์
// type of ํ์
์ถ๋ ฅ ๋ช
๋ น
//parseInt string number๋ก ๋ฐ๊พธ๊ธฐ, stringdl ๋ฌธ์๊ฐ ์
๋ ฅ๋๋ฉด NaN, ํ์
๋ณํ
console.log(age, parseInt(age));
const age = parseInt(prompt("How old are you?"));
console.log(age);conditional
if(์กฐ๊ฑด๋ฌธ){ ์กฐ๊ฑด์ด ์ฐธ์ผ๋ ์คํํ ์ฝ๋ } else{ ์กฐ๊ฑด๋ฌธ์ด ๊ฑฐ์ง์ผ๋ ์คํํ ์ฝ๋ }
- ์กฐ๊ฑด๋ฌธ
const age = parseInt(prompt("How old are you?"));
//isNaN ์ฃผ์ด์ง ์ธ์๊ฐ NaN์ธ์ง ํ๋ณํจ, ๊ฒฐ๊ณผ๋ boolean์ ๋ฐํํจ, Not a Number
if (isNaN(age)) {
console.log("Please write a number");
} else {
console.log("Thank you for Writing your age.");
}const age = parseInt(prompt("How old are you?"));
if (isNaN(age) || age < 0) {
console.log("Please write a real positive number"); // ์ฐ์ฐ์ || ํ๋ ์ด์ ์ฐธ์ด๋ฉด ์ฐธ
} else if (age < 18) {
//if๋ฌธ์ ์กฐ๊ฑด๋ฌธ์ด ๊ฑฐ์ง์ผ ๋ ํ์ธํ๋ ๋ค์ ์กฐ๊ฑด๋ฌธ
console.log("You are too young"); // ์ฐธ์ผ๋ ์คํํ ์ฝ๋
} else if (age >= 18 && age <= 50) {
// ์ฐ์ฐ์ && ๋๋ค ์ฐธ์ด์ด์ผ ์ฐธ
// ๋ชจ๋ ์กฐ๊ฑด์ด ๊ฑฐ์ง์ผ ๋ ์คํ
console.log("You can drink"); // ์คํํ ์ฝ๋
} else if (age > 50 && age <= 80) {
// ๋ชจ๋ ์กฐ๊ฑด์ด ๊ฑฐ์ง์ผ ๋ ์คํ
console.log("You should exercise"); // ์คํํ ์ฝ๋
} else if (age > 80) {
// ๋ชจ๋ ์กฐ๊ฑด์ด ๊ฑฐ์ง์ผ ๋ ์คํ
console.log("You can do whatever you want"); // ์คํํ ์ฝ๋
}// 2.16
=== // ์ข์ฐ ๊ฐ์ด ๊ฐ์๊ฐ๋ฅผ ํ๋จ ==์ ๋ฐ์ดํฐ์ ๊ฐ๋ง ๋ณด๊ณ ๋น๊ต๋ฅผ ํ์ง๋ง ===์ ๋ฐ์ดํฐ์ ํ์ ๋ ๋ด, ๋์ฑ ์๊ฒฉํ ๋น๊ต
!== // ์ข์ฐ ๊ฐ์ด ๊ฐ์ง ์์๊ฐ๋ฅผ ํ๋จ
const age = parseInt(prompt("How old are you?"));
if (isNaN(age) || age < 0) {
console.log("Please write a real positive number");
} else if (age < 18) {
console.log("You are too young");
} else if (age >= 18 && age <= 50) {
console.log("You can drink");
} else if (age > 50 && age <= 80) {
console.log("You should exercise");
} else if (age === 100) {
console.log("wow you are wise");
} else if (age > 80) {
console.log("You can do whatever you want");
}
if ((a && b) || (c && d) || x || y) {
}์๋ฐ์คํฌ๋ฆฝํธ๋ html์ ์ ๊ทผํ๊ณ ์ฝ์ ์ ์๋ค
document.title = "hi"; // JS๋ html๊ณผ ์ฐ๊ฒฐ๋์ด์์
// classname ์ "hello", id ๋ "title"const title = document.getElementById("title"); //html์์ id ๊ฐ title์ธ element ๊ฐ์ ๊ฐ์ ธ์ ๋ณ์ title ์ ์ ์ํ๋ค
console.dir(title); //title ์ ์ ์ฒด ์์ ์ถ๋ ฅ
title.innerText = "Got you!"; //html ์์ id ๊ฐ title์ธ ๊ฐ์ฒด์ ์ด๋ํ ์คํธ ์์
console.dir(title.id); //title ์ id ์ถ๋ ฅ console.dir(title.className); //title ์ classname ์ถ๋ ฅ
๊ฐ์ฒด ์ด๋ฆ์ ์ ์ ํ ๋๋ ๋ณดํต classname ์ ์ฐ๊ฑฐ๋ title ๊ณผ ๊ฐ์ด ์ด๋ค
//3.2
const hellos = document.getElementsByClassName("hello"); // classname ์ด hello์ธ element ๊ฐ์ ๊ฐ์ ธ์ ๋ณ์ title ์ ์ ์ํ๋ค
console.log(hellos);
const title = document.getElementsByTagName("h1"); // tagname ์ด h1์ธ element ๊ฐ์ ๊ฐ์ ธ์ ๋ณ์ title ์ ์ ์ํ๋ค
console.log(title);
const title = document.querySelector(".hello h1");//css ๋ฐฉ์, ๊ฐ์ด ์์ผ๋ฉด null, ๋ถํฉํ๋ ๊ฐ์ฒด๊ฐ ์ฌ๋ฌ๊ฐ๋ฉด ๊ทธ์ค ์ฒซ๋ฒ์งธ ๊ฒ์ ๊ฐ์ ธ์ด
querySelectorAll ์ ์กฐ๊ฑด์ ๋ถํฉํ๋ ๋ชจ๋ ๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์ด
console.log(title);
document.querySelector("#hello");-- ๊ฐ์ ์๋ฏธ document.getElementById("hello");--
on~ : event
const title = document.querySelector("div.hello:first-child h1");
console.dir(title);
title.Style.color = "blue"; // title ์์ ์ค์
JS์์๋ ์ฃผ๋ก event๋ฅผ ์ฒ๋ฆฌ ํจ
click event listen
const title = document.querySelector("div.hello:first-child h1");
function handleTitleClick() { console.log("title was clicked!"); }
title.addEventListener("click", handleTitleClick);
const title = document.querySelector("div.hello:first-child h1");
function handleTitleClick() { //ํ์ดํ์ด ํด๋ฆญ๋๋ฉด title.style.color = "blue"; //๊ธ์ ์์ ์ค์ , JS์์ ์คํ์ผ์ ๋ณ๊ฒฝํ ์ ์์ง๋ง CSS์์ ๋ณ๊ฒฝํ๋ ๊ฒ์ด ์ข์ }
function handleMouseEnter() { //ํ์ดํ๋ก ๋ง์ฐ์ค๊ฐ ๋ค์ด์ค๋ฉด title.innerText = "mouse is here!"; // ์ด๋ ํ ์คํธ ๋ณ๊ฒฝ }
function handleMouseLeave() { //๋ง์ฐ์ค๊ฐ ๋๊ฐ๋ฉด title.innerText = "mouse is gone!"; // ์ด๋ ํ ์คํธ ๋ณ๊ฒฝ }
title.addEventListener("click", handleTitleClick); // ํ ์คํธ๊ฐ ํด๋ฆญ๋๋ฉด handleTitleClick ์คํ title.addEventListener("mouseenter", handleMouseEnter); // ์ด๋ฒคํธ๊ฐ ๊ฐ์ง๋๋ฉด handleMouseEnter ์คํ title.addEventListener("mouseleave", handleMouseLeave); // ์ด๋ฒคํธ๊ฐ ๊ฐ์ง๋๋ฉด handleMouseLeave ์คํ
const h1 = document.querySelector("div.hello:first-child h1");
function handleTitleClick() { h1.style.color = "blue"; }
function handleMouseEnter() { h1.innerText = "mouse is here!"; }
function handleMouseLeave() { h1.innerText = "mouse is gone!"; }
function handleWindowResize() { document.body.style.backgroundColor = "tomato"; }
function handleWindowCopy() { alert("copier!"); }
function handleWindowOffline() { alert("SOS no WIFI"); }
function handleWindowOnline() { alert("good WIFI"); }
h1.addEventListener("click", handleTitleClick); // ํ ์คํธ๊ฐ ํด๋ฆญ๋๋ฉด handleTitleClick ์คํ h1.addEventListener("mouseenter", handleMouseEnter); h1.addEventListener("mouseleaee", handleMouseLeave); // ์ด๋ฒคํธ ๋ฆฌ์ค๋๋ ํ์ ๋ฆฌ๋ฌด๋ธ๊ฐ ๊ฐ๋ฅ
window.addEventListener("resize", handleWindowResize); // document์ body, head, title ๋ฑ์ ์ค์ํ๊ธฐ ๋๋ฌธ์ ์ฝ์์์ ๋ถ๋ฌ์ฌ ์ ์์ง๋ง ๋ค๋ฅธ element๋ค์ querySelector ์ด๋ getElementByld ๋ฑ์ ์ฌ์ฉํด์ผ ํ๋ค window.addEventListener("copy", handleWindowCopy); window.addEventListener("offline", handleWindowOffline); window.addEventListener("online", handleWindowOnline);
const h1 = document.querySelector("div.hello:first-child h1");
function handleTitleClick() { const currentColor = h1.style.color; let newColor; if (currentColor === "blue") { newColor = "tomato"; } else { newColor = "blue"; } h1.style.color = newColor; }
h1.addEventListener("click", handleTitleClick); // ํ ์คํธ๊ฐ ํด๋ฆญ๋๋ฉด handleTitleClick ์คํ
//css๋ styleํ์ผ์์ ์์ฑ const h1 = document.querySelector("div.hello:first-child h1");
function handleTitleClick() { const clickedClass = "clicked"; if (h1.className === "clickedClass") { h1.className = ""; } else { h1.className = "clickedClass"; // classname ์์ฑ } }
h1.addEventListener("click", handleTitleClick);
const h1 = document.querySelector("div.hello:first-child h1");
function handleTitleClick() { const clickedClass = "clicked"; if (h1.classList.contains(clickedClass)) { // classname ์ผ๋ก ์์ ํ ์ ์๊ฒ ํด์ค h1.classList.remove(clickedClass); } else { h1.classList.add(clickedClass); // classname ์์ฑ } }
h1.addEventListener("click", handleTitleClick);