
TML๊ณผ CSS๋ก ๋ง๋ค์ด์ง ์น ํ์ด์ง์ ์๋๊ฐ์ ๋ถ์ด๋ฃ๊ธฐ ์ํด ๋ง๋ค์ด์ง ๊ฐ์ฒด๊ธฐ๋ฐ์ ์คํฌ๋ฆฝํธ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ด๋ค.
์คํฌ๋ฆฝํธ ์ธ์ด๋ ์ด๋ฏธ ์กด์ฌํ๋ ํ๋ก๊ทธ๋จ์ ์ ์ดํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ ์ธ์ด๋ฅผ ๋งํ๋ค.
HTML์ด ์นํ์ด์ง์ ๊ธฐ๋ณธ ๊ตฌ์กฐ, CSS์ด ์นํ์ด์ง์ ๋์์ธ, JavaScript๊ฐ ์นํ์ด์ง์ ๋์์ ๋ด๋นํ๋ค.
๋ฐ๋ก C, C++์ฒ๋ผ ์ปดํ์ผ ์์ด ํ์ค ํ์ค ํด์ํ๋ฉฐ ๋ฐ๋ก ๋ช ๋ น์ด๋ฅผ ์คํํ๋ ์ธํฐํ๋ฆฌํฐ ์ธ์ด HTML์ ํน์ ์์๋ฅผ ์ ํํ์ฌ ๋ค์ํ ์ด๋ฒคํธ ( ๋ง์ฐ์ค ํด๋ฆญ, ํค๋ณด๋ ์ ๋ ฅ ๋ฑ )์ ๋ฐ๋ผ ์ด๋ค ๋์์ ํ๋๋ก ๊ธฐ๋ฅ์ ๋ฃ์ ์ ์๋ค.
์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋๋ ๏ผscript๏ผํ๊ทธ๋ฅผ HTML๋ฌธ์์์ ๋ฃ์ด์ ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
HTML๋ฌธ์์ ์ด๋์์น์์๋ ์ฌ์ฉ ๊ฐ๋ฅํ๋ฐ ๋ณดํต HTML๋ฌธ์๊ฐ ์ ๋ถ ๋ก๋ ๋๊ณ ๋์์ธ ๏ผbody๏ผ ์๋์ ์ฌ์ฉํ๋ค. ( ์ฃผ๋ก ๊ฐ๋จํ ์คํฌ๋ฆฝํธ๋ ํ ์คํธ์ฉ์ผ๋ก ์ฌ์ฉํ๋ค. )
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
<script>
์๋ฐ ์คํฌ๋ฆฝํธ ์ฝ๋
</script>
</html>
์๋ฐ์คํฌ๋ฆฝํธ ํ์ผ์ HTML๊ณผ ๋ณ๊ฐ์ธ .js ํ์ฅ์์ ํ์ผ๋ก ์ ์ฅํ ํ ๋ถ๋ฌ์ฌ ์ ์๋ค.
์ธ๋ถ ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด ๏ผscript๏ผ์ src ์์ฑ์ผ๋ก HTML ๋ฌธ์ ์์น์ ๊ธฐ์ค ํ์ผ ๊ฒฝ๋ก๋ฅผ ์ ๋ ฅํด์ฃผ๋ฉด ๋๋ค.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
<script src="./index.js"></script>
</html>
1) ์ ์ (Integer)
1, 2, 3, 4, ..........
2) ์์ (float)
1.555, 2.3443323......, .........
"Hello, " + "My name is ์ฒ ์"ใใใใใใใใใใ// "Hello, My name is ์ฒ ์"
const a = 10;
const b = 5;
const myName = "์ฒ ์";
console.log(a + b); // 15 ์ถ๋ ฅ
console.log(a - b); // 5 ์ถ๋ ฅ
console.log(a * b); // 50 ์ถ๋ ฅ
console.log(a / b); // 2 ์ถ๋ ฅ
console.log("Hello, " + myName); // "Hello, ์ฒ ์" ์ถ๋ ฅ
veryLongVariableName
very_long_variable_name
let a = b;
let a = c;
// ์๋ฌ (์ฌ์ ์ธ ๊ธ์ง)
let a = b;
a = c;
// ์ฌํ ๋น ๊ฐ๋ฅ
let a = b;
let a = c;
// ์๋ฌ (์ฌ์ ์ธ ๊ธ์ง)
let a = b;
a = c;
// ์๋ฌ (์ฌํ ๋น ๊ธ์ง)
let a = b;
let a = c;
// ์ฌ์ ์ธ ๊ฐ๋ฅ
let a = b;
a = c;
// ์ฌํ ๋น ๊ฐ๋ฅ
const daysOfWeek = ["์", "ํ", "์", "๋ชฉ", "๊ธ", "ํ ", "์ผ"];
console.log(daysOfWeek[4]); // "๊ธ" ์ถ๋ ฅ -> ์ปดํจํฐ๋ ์ซ์๋ฅผ 0๋ถํฐ ์ธ๊ธฐ ๋๋ฌธ์, "์"์ 0๋ฒ์งธ๋ผ๊ณ ์๊ฐํ๋ค.
// ์ถ๊ฐ
daysOfWeek.push("์์ผ"); // "์์ผ" ์ถ๊ฐ
console.log(daysOfWeek); // ["์", "ํ", "์", "๋ชฉ", "๊ธ", "ํ ", "์ผ", "์์ผ"] ์ถ๋ ฅ
// ๋ณ๊ฒฝ
daysOfWeek[4] = "๋ถ๊ธ"; // "๊ธ"์์ "๋ถ๊ธ"์ผ๋ก ๋ณ๊ฒฝ
console.log(daysOfWeek); // ["์", "ํ", "์", "๋ชฉ", "๋ถ๊ธ", "ํ ", "์ผ", "์์ผ"] ์ถ๋ ฅ
const๋ก ์ ์ธ๋ ๋ฐฐ์ด์ ์ฌํ ๋น์ ๋ถ๊ฐ๋ฅํ์ง๋ง, ๋ฐฐ์ด์ ๋ด์ฉ(๋ฐฐ์ด์ ์์)์ ์์ ํ ์ ์๋ค. ์ด๊ฒ์ Javascript์์ ๋ณ์์ ๊ฐ์ ์ฐจ์ด์ ๋ํ ์ดํด์ ๊ด๋ จ์ด ์๋ค.
const๋ก ์ ์ธ๋ ๋ณ์๋ ์ฌํ ๋น์ด ๋ถ๊ฐ๋ฅํ์ง๋ง, ๊ฐ์ด ๋ณ๊ฒฝ๋์ง ์๋ ๊ฒ์ ์๋๋ค. ๋ฐ๋ผ์ ๋ฐฐ์ด์ ๋ด์ฉ์ ์์ ํ ์ ์๊ณ , ๋ฐฐ์ด์ ๋ด์ฉ์ ๋ณ๊ฒฝํ๋ ๊ฒ์ ๋ฐฐ์ด์ ์์๋ฅผ ์์ , ์ถ๊ฐ ๋๋ ์ญ์ ํ๋ ๊ฒ์ ์๋ฏธํ๋ค. ํ์ง๋ง ๋ฐฐ์ด ์์ฒด๋ฅผ ๋ค๋ฅธ ๋ฐฐ์ด๋ก ์ฌํ ๋นํ๋ ๊ฒ์ const๋ก ์ ์ธ๋ ๋ฐฐ์ด์์๋ ํ์ฉ๋์ง ์๋๋ค.
const numbers = [1, 2, 3];
numbers[0] = 0; // ๊ฐ๋ฅ - ๋ฐฐ์ด์ ๋ด์ฉ์ ์์ ํ๋ ๊ฒ์ด๋ฏ๋ก ํ์ฉ๋จ
console.log(numbers); // [0, 2, 3] ์ถ๋ ฅ
numbers.push(4); // ๊ฐ๋ฅ -> ๋ฐฐ์ด์ ๋ด์ฉ์ ์์ ํ๋ ๊ฒ์ด๋ฏ๋ก ํ์ฉ๋จ
console.log(numbers); // [0, 2, 3, 4] ์ถ๋ ฅ
numbers = [5, 6, 7]; // ๋ถ๊ฐ๋ฅ -> ๋ณ์์ ์๋ก์ด ๋ฐฐ์ด์ ํ ๋นํ๋ ค๊ณ ํ๊ธฐ ๋๋ฌธ์ ์๋ฌ๊ฐ ๋ฐ์ํจ
const player = {
name : "์ฒ ์",
points : 10,
fat : true,
};
console.log(player); // {name : "์ฒ ์", points : 10, fat : true} ์ถ๋ ฅ
console.log(player.name); // "์ฒ ์" ์ถ๋ ฅ
console.log(player["name"]); // "์ฒ ์" ์ถ๋ ฅ
// ๋ณ๊ฒฝ
player.fat = false; // fat ๊ธฐ์กด ๊ฐ์ false๋ก ๋ณ๊ฒฝ
player.points = player.points + 10; // ๊ธฐ์กด ํฌ์ธํธ์์ 10 ๋ํ๊ณ ๋ณ๊ฒฝ
console.log(player); // {name : "์ฒ ์", points : 20, fat : false} ์ถ๋ ฅ -> fat๊ฐ true์์ false๋ก, points๋ 10์์ 20์ผ๋ก ๋ณ๊ฒฝ๋๋ค.
// ์ถ๊ฐ
player.lastName = "๊น"; // lastName = "๊น" ์ถ๊ฐ
console.log(player); // {name : "์ฒ ์", points : 10, fat : false, lastName : "๊น"} ์ถ๋ ฅ -> object์ property(ํ๋กํผํฐ) ์ถ๊ฐ ํ ์ ์๋ค.
// ์ ์ธ
function ํจ์๋ช
(){
์คํํ ์ฝ๋
}
// ์คํ
ํจ์๋ช
();
function sayHello(nameOfPerson) {
console.log(nameOfPerson);
}
sayHello("์ฒ ์"); // "์ฒ ์" ์ถ๋ ฅ
sayHello("์ ๋ฆฌ"); // "์ ๋ฆฌ" ์ถ๋ ฅ
sayHello("๋งน๊ตฌ"); // "๋งน๊ตฌ" ์ถ๋ ฅ
// ์๊ท๋จผํธ(argument) ์ฌ๋ฌ ๊ฐ ๋ฐ์ ๋
function sayHello(nameOfPerson , age) {
console.log(nameOfPerson);
}
sayHello("์ฒ ์" , 12); // "์ฒ ์",12 ์ถ๋ ฅ
sayHello("์ ๋ฆฌ" , 20); // "์ ๋ฆฌ",20 ์ถ๋ ฅ
sayHello("๋งน๊ตฌ" , 23); // "๋งน๊ตฌ",23 ์ถ๋ ฅ
// ๊ฐ์ฒด(Object) ์์์ ๋งค๊ฐ๋ณ์๊ฐ ์๊ท๋จผํธ(argument)๋ฅผ ๋ฐ๋ ๋ฐฉ์
const player = {
name : function(Name) {
console.log("๋ด ์ด๋ฆ์ " + Name + "์
๋๋ค.");
} ,
sayHello : function(Age) {
console.log("๋ด ๋์ด๋ " + Age + "์
๋๋ค." );
}
}
player.sayHello(14); // "๋ด ๋์ด๋ 14์
๋๋ค." ์ถ๋ ฅ
player.name("์ ์์ค"); // "๋ด ์ด๋ฆ์ ์ ์์ค์
๋๋ค." ์ถ๋ ฅ
console.log๋ console์ ๋ฌด์ธ๊ฐ๋ฅผ log ํ๋ ๊ฒ์ด๋ค. ํ์ง๋ง ์ฐ๋ฆฌ๋ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ ์ฌ์ฉํ๊ฑฐ๋ ์ฝ์์ด ์๋ ํ๋ฉด์ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๊ณ ์ถ์ดํ๊ธฐ ๋๋ฌธ์ return ์ฌ์ฉํ๋ค.
return์ ์นดํ๋ ์๋น์ ํ ์ดํฌ์์ ๊ฐ๋ ์ธ ๊ฒ ๊ฐ๋ค. ํจ์์์ ๋ง๋ค์ด์ ๋ด๋ณด๋ธ ๊ฒฐ๊ณผ๊ฐ์ ํจ์ ๋ฐ์ผ๋ก ๋ค๊ณ ๋๊ฐ์ ๋ค๋ฅธ๋ฐ์ ๊ทธ๋๋ก ์ถ๋ ฅํ๊ฑด ๋ค๋ฅธ ๊ฑฐ๋ ์์ด์ ๋น๋ฒผ๋จน๊ฑด ๋ณถ์๋จน๊ฑด ์ฌ์ฉํ ์ ์๊ฒ ํด์ค๋ค.
ํ๋ฒ return์ ํ๋ฉด ํด๋น ํจ์๋ ๋๋๋ค. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ return๋ค์ ์์ ์ ํ๋ค๋ฉด ์ถ๋ ฅ๋์ง ์๋๋ค! (๏ผreturn ์์ ์์ ์ ํ๋ค๋ฉด ์ถ๋ ฅ๋๋ค)
return์ ๋ง ์์ฒด๋ก ๋ค์ ๋๋ ค์ค๋ค๋ผ๋ ์๋ฏธ๋กํด๋น ํจ์์ ๋ณ์๊ฐ์ ์ง์ ํด์ฃผ๋ฉด ํด๋น code๋ฅผ ์์์ ์คํํด์ ๊ทธ ๊ฒฐ๊ณผ๊ฐ์ ํด๋น ํจ์์ ๋ณ์๊ฐ์ ์ ์ธํ ๋ณ์ํํ ๋๋ ค์ฃผ๋ ๊ฒ์ด๋ค.
function fun(a, b) {
console.log("Hi");
return a + b; // ํจ์ ์คํ์ ์ข
๋ฃํ๊ณ , ์ฃผ์ด์ง ๊ฐ์ ํธ์ถ ์ง์ ์ผ๋ก ๋ฐํํ๋ค.
console.log("bye"); // return์ ํ๋ฉด ๊ฑฐ๊ธฐ์ ํจ์๊ฐ ๋๋๊ธฐ ๋๋ฌธ์ ๊ทธ ๋ค์ ์ฝ๋๋ ์คํ ์๋๋ค.
}
const result = fun(2, 5) // "Hi" ์ถ๋ ฅ
console.log(result); // "Hi" 5 ์ถ๋ ฅ
if (์กฐ๊ฑด์) {
// statement1
} else if(์กฐ๊ฑด์) {
// statement2
} else {
// statement3
}
1)ย false
2)ย undefined
3)ย null
4)ย 0
5)ย NaN
6)ย theย emptyย stringย ("")
const score = 96;
const lecture = "sports";
if("sports" == lecture){
if (score >= 70){
console.log("์ ์ : pass");
}
else{
console.log("์ ์ : fail");
}
}else{
if (score >= 90){
console.log("์ ์ : A");
}else if (score >= 80){
console.log("์ ์ : B");
}else if (score >= 70){
console.log("์ ์ : C");
}else if (score >= 60){
console.log("์ ์ : D");
}else {
console.log("์ ์ : F");
}
}
// "์ ์ : pass" ์ถ๋ ฅ