
๊ฐ์ฒด๋ ์ค๊ดํธ({})๋ฅผ ์ฌ์ฉํ๋ฉฐ ํค(key)์ ๊ฐ(value)์ ํ์์ผ๋ก ์ด๋ฃจ์ด์ง๋ค. ๋ฐฐ์ด(array)๋ ๋ณต์์ ๋ฐ์ดํฐ๋ฅผ ์ ์ํ ์ ์๋ ์๋ฃํ์ด๋ค.
//๋ฐฐ์ด
let scores = [10, 20, 30, 40, 50];
let names = ['์ ์ฒญ' , '์ด์์ฑ', '์ด์ค๊ตฌ'];
console.log(scores);
console.log(scores[2]); //index๋ก ์ ๊ทผํ ์ ์๋ค.
console.log(names);
๐ฝ์คํ ๊ฒฐ๊ณผ
//๊ฐ์ฒด
let stu1 = {
stuid:14155,
name:'๋์ด๋ฆ',
age:15
};
console.log(stu1);
console.log(stu1.name);
console.log(stu1['name']); //ํค๊ฐ์ผ๋ก ์ ๊ทผํ ์ ์๋ค.
๐ฝ์คํ ๊ฒฐ๊ณผ
+, -, *, /, %, **++, --, +, -=+=, -=, *=, /=, %=, **=//๋ณตํฉ ๋์
์ฐ์ฐ์์ ์
x += y; //x = x + y;
x **= y; //x = x ** y; x์ y๊ฑฐ๋ญ์ ๊ณฑ
==, ===, !=, !==, <, <=, >, >=== ์ ๋ ๊ฐ์ด ๊ฐ์ผ๋ฉด true๋ฅผ ๋ฐํํ๊ณ ===์ ๋ ๊ฐ๊ณผ ์๋ฃํ์ด ๊ฐ์ผ๋ฉด true๋ฅผ ๋ฐํํ๋ค.//==๊ณผ ===์ ๋น๊ต
let num100 = 100;
let str100 = '100';
console.log(num100 == str100); //true๋ฅผ ๋ฐํ
console.log(num100 === str100); //false๋ฅผ ๋ฐํ
&&, ||, !x ? y : zif else else if๋ฌธ : if ๋ค์ ์ค๋ ์๊ดํธ(()) ์์ ์กฐ๊ฑด์์ด ์ฐธ์ด๋ฉด ์ค๊ดํธ({}) ์์ ์ฝ๋๋ฅผ ์คํํ๋ ์กฐ๊ฑด๋ฌธ์ผ๋ก if๋ฌธ์ ์กฐ๊ฑด์์ด ๊ฑฐ์ง์ผ ๋๋ else๋ฌธ์ด ์คํ
else if๋ if๋ฌธ์ ์กฐ๊ฑด์ ์ถ๊ฐํ๊ณ ์ถ์ ๋ ์ฌ์ฉํ๋ ๋ธ๋ก๋ฌธ
switch๋ฌธ : switch ๋ค์ ์ค๋ ์๊ดํธ ์์ ๊ฐ๊ณผ ์ผ์นํ๋ case๋ฌธ์ด ์์ ๋ ํด๋น ์ฝ๋๋ฅผ ์คํํ๋ ์กฐ๊ฑด๋ฌธ
for(๊ฐ๋ณ์ in ๋ฐฐ์ด/๊ฐ์ฒด ๋ฆฌํฐ๋ด){ //๋ธ๋ก๋ฌธ }//for...in [๋ฐฐ์ด]
let arr = ['๊ฐ', '๋', '๋ค'];
for(let idx in arr){
console.log(idx + " " + arr[idx]);
}
๐ฝ์คํ ๊ฒฐ๊ณผ
//for...in {๊ฐ์ฒด}
let person = {
name : "๋ด์ด๋ฆ",
age:30,
phone:"010010"
};
for(let key in person){
console.log(key + " " + person[key]);
}
๐ฝ์คํ ๊ฒฐ๊ณผ
๐ฌ ์ถ๊ฐ๋ก ์ดํด๋ณด๋ for of๋ฌธ
//java์ 'ํฅ์๋ for๋ฌธ'๊ณผ ๊ฐ์ ๊ธฐ๋ฅ์ ํ๋ค.
let arr = ['๊ฐ', '๋', '๋ค'];
for(let val of arr){
console.log(val);
}
๐ฝ์คํ ๊ฒฐ๊ณผ
ํจ์๋ ์ด๋ค ๋ชฉ์ ์ ๊ฐ์ง๊ณ ์์ฑํ ์ฝ๋๋ฅผ ๋ชจ์ ๋ ๋ธ๋ก๋ฌธ์ด๋ค. ํจ์๋ฅผ ์ ์ํ๋ค๋ ๊ฒ์ ๋ธ๋ก๋ฌธ์ function ํค์๋, ์๋ณ์, ์๊ดํธ์ ํจ๊ป ๋ฌถ์ด ํจ์๋ฅผ ์์ฑํ๋ ๊ฒ์ด๋ค. ํจ์๋ฅผ ์ ์ํ๊ณ ๋๋ฉด ์ฝ๋๋ฅผ ์๋ก ์์ฑํ ํ์ ์์ด ์ ์ํ ํจ์๋ฅผ ํธ์ถํ๋ฉด ๋๋ค.
๐ฌํจ์์ ๋ฉ์๋
ํจ์๋ ์ด๋ค ํน์ ํ ๊ธฐ๋ฅ์ ์ํํ๊ธฐ ์ํ ๋ธ๋ก์ด๋ผ๋ฉด ๋ฉ์๋๋ ๊ฐ์ฒด ๋จ์๋ก ๊ฐ๋ฐํ๋ ํน์ ํด๋์ค ๋ด๋ถ์ ํน์ ์ฝ๋๋ค์ ๋ชจ์์ด๋ผ๊ณ ๋ณผ ์ ์๋ค. ๋ ๊ฐ๋
์ ๋น์ทํ ๊ฐ๋
์ผ๋ก ์ดํดํ๊ณ ๋์ด๊ฐ๋ ์ข๋ค.
ํจ์ ์ ์ธ๋ฌธ์ผ๋ก ํจ์ ์ ์
function ์๋ณ์ () {}
ํจ์ ํํ์์ผ๋ก ํจ์ ์ ์
const ๋ณ์๋ช
= funtion() {}; -> ์ต๋ช
ํจ์
const ๋ณ์๋ช
= funtion ์๋ณ์() {}; -> ๋ค์ด๋ฐ ํจ์
ํ์ดํ ํจ์๋ก ํจ์ ์ ์
() => {};
์๋์์ ์ฝ๋๋ฅผ ์ด์ฉํด ์์ธํ ์ดํด๋ณด๊ฒ ๋ค.
//1. ํจ์ ์ ์ธ๋ฌธ์ผ๋ก ํจ์ ์ ์
function gugudan(){
for(let i=1; i<=9; i++){
console.log(`3 * ${i} = ${3*i}`);
}
}
gugudan();
๐ฝ์คํ ๊ฒฐ๊ณผ
//2. ํจ์ ํํ์์ผ๋ก ํจ์ ์ ์-๋ค์ด๋ฐ ํจ์
const funcGugudan4 = function gugudan4(){
for(let i=1; i<=9; i++){
console.log(`4 * ${i} = ${4*i}`);
}
}
funcGugudan4();
๐ฝ์คํ ๊ฒฐ๊ณผ
//2. ํจ์ ํํ์์ผ๋ก ํจ์ ์ ์-์ต๋ช
ํจ์
const funcGugudan5 = function(){
for(let i=1; i<=9; i++){
console.log(`5 * ${i} = ${5*i}`);
}
}
funcGugudan5();
๐ฝ์คํ ๊ฒฐ๊ณผ
//3. ํ์ดํ ํจ์๋ก ํจ์ ์ ์
const funcGugudan6 = ()=> {
for(let i=1; i<=9; i++){
console.log(`6 * ${i} = ${6*i}`);
}
}
funcGugudan6();
๐ฝ์คํ ๊ฒฐ๊ณผ
๋งค๊ฐ๋ณ์๋ ํจ์๊ฐ ํธ์ถ๋ ๋ ์ ๋ฌ๋ฐ์ ๋ฐ์ดํฐ๋ฅผ ํ ๋นํ๊ธฐ ์ํด ํจ์์์ ์ ์ธํ๋ ๋ณ์์ด๋ค.
์ธ์๋ ์ ์ํ ํจ์๋ฅผ ํธ์ถํ ๋ ์ ๋ฌํ๋ ๋ฐ์ดํฐ์ด๋ค.
์ฆ, ํจ์ ์ ์ธ๋ฌธ์ ์ดํด๋ณด๋ฉด
function ํจ์๋ช
(๋งค๊ฐ๋ณ์1, ๋งค๊ฐ๋ณ์2, ... ๋งค๊ฐ๋ณ์n) {}
์ด๋ ๊ฒ ํจ์๋ฅผ ์ ์ํ ๋ ์ ๋ฌ๋ฐ๋ ์ฉ๋๋ก ์ฌ์ฉํ๋ ๊ฒ์ด ๋งค๊ฐ๋ณ์์ด๋ค.
ํจ์๋ฅผ ํธ์ถํ ๋
ํจ์๋ช
(์ธ์1, ์ธ์2, ... ์ธ์n);
์ด๋ ๊ฒ ํจ์์ ๋๊ฒจ์ฃผ๋ ๊ฐ(๋ณ์)์ด ์ธ์์ด๋ค.
๋งค๊ฐ๋ณ์๋ ํจ์๋ฅผ ํธ์ถํ๋ฉฐ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํด๋ ๋งค๊ฐ๋ณ์๋ฅผ ์ ์ํ์ง ์์ผ๋ฉด ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌ๋ฐ์ง ๋ชปํ๋ค. ํ์ง๋ง ์ค๋ฅ๋ ๋ฐ์ํ์ง๋ ์๋๋ค. ๋งค๊ฐ๋ณ์์ ๊ธฐ๋ณธ๊ฐ์ undefined์ด๋ค.
ํจ์ ๋ด๋ถ์์ ํจ์๋ฅผ ํธ์ถํ ๊ณณ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ ๋๋ return๋ฌธ์ ์ฌ์ฉํ๋ค.
function sum(num1, num2){
let result = num1 + num2;
return result;
}
let result = sum(50, 40);
console.log(result); //90์ด ์ถ๋ ฅ๋๋ค.
๐ฌreturn์ ํจ์๊ฐ ์ข ๋ฃ๋๋ ์ญํ ๋ก๋ ์ธ ์ ์๋ค.
function sample(){
console.log(1);
console.log(2);
return;
console.log(3);
}
sample();
๐ฝ์คํ ๊ฒฐ๊ณผ
=> return ์๋์ ์ฝ๋๋ ์คํ๋์ง ์์ ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ์ ์๋ค.
๐ฌํ์ดํ ํจ์์์ ์ค๊ดํธ({})๋ฅผ ์๋ตํ๋ฉด ํ์ดํ ๋ค์์ ์ค๋ ์ฝ๋๋ return๋ฌธ์ผ๋ก ์ฒ๋ฆฌ๋๋ค. ์๋์ ๊ฐ์ด ์ธ ์ ์๋ค.
const sum = (num1, num2)=> num1 + num2;