์ด๋ฒ์ Javascript์ ์ ์ฉํ ๋ฌธ๋ฒ์ ๋ํด ์๊ฐํ๋ ค๊ณ ํ๋ค.
?์:๋ฅผ ์ฌ์ฉํ์ฌ ์กฐ๊ฑด๋ฌธ์ ๋ง๋๋ ๊ฒ ์ด๋ค. ๊ทธ๋ฆฌ๊ณ ์ผํญ์ฐ์ฐ์๋ฅผ ์ค์ฒฉํด์ ์ฌ์ฉํ๊ธฐ ๋ณด๋จ,if๋ฌธ์ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ ํจ์จ์ ์ด๋ค.
const hansome = true;
const value = hansome == true ? "๋๋ ์์๊ฒผ๋ค~" : "๋๋ ๋ชป์๊ฒผ๋ค~";
console.log(value);์ด๋ฐ์์ผ๋ก ์ฝ๋๋ฅผ ์์ฑํ๋ฉด
hansome์ ๊ฐ์ดtrue์ด๋ฏ๋ก๋๋ ์์๊ฒผ๋ค~๊ฐ ์ถ๋ ฅ๋๋ค.
Falsy์ ๊ฐ์๋
undefined,null,0,๋น์ด์๋ ๋ฌธ์์ด,NaN์ด ์๋ค.
// &&
console.log(true && 'true๊ฐ์ด ์๋๋ฏ๋ก ์ด ๋ฌธ์ฅ์ด ์ถ๋ ฅ๋ฉ๋๋ค.');
// ||
console.log(false || 'false๊ฐ์ด๋ฏ๋ก ์ด ๋ฌธ์ฅ์ ์ถ๋ ฅํฉ๋๋ค.');์ด๋ฐ ์์ผ๋ก ๋จ์ถ ํ๊ฐ ๋ ผ๋ฆฌ ๊ณ์ฐ๋ฒ์์
&&์ ์์ ๊ฒ์ดtrue๋ฉดtrue๋ฅผ ์ถ๋ ฅํ๊ณ , ์๋๋ฉด ๋ค์ ๊ฒ์ ์ถ๋ ฅํ๋ค. ๊ทธ๋ฌ๋||์ ์์ ๊ฒ์ดfalse๋ฉด ๋ค์ ๊ฒ์ ์ถ๋ ฅํ๋ค.
const MyHeight = (height) => {
return height;
}
const result = MyHeight(180);
console.log(result);๋ง์ฝ ์์ ์ฝ๋์ฒ๋ผ ์์ฑ์ ํ๋ฉด
180์ด๋ผ๋ ๊ฐ์ด ์ถ๋ ฅ ๋ ๊ฒ์ด๋ค. ๊ทธ๋ฌ๋ ์ฌ๊ธฐ์ ๋ง์ฝ ํ๋ผ๋ฏธํฐ๋ฅผ ์ ์จ์ฃผ๊ณ ๊ธฐ๋ณธ์ ์ผ๋ก ์ง์ ์ ํด์ฃผ๋ ค๋ฉด ์ด๋ค ์์ผ๋ก ํด์ผํ ๊น?
const MyHeight = (height = 180) => {
return height;
}
const result = MyHeight();
console.log(result);์ด๋ฐ ์์ผ๋ก ํจ์์์ ํ๋ผ๋ฏธํฐ์ ๋ฏธ๋ฆฌ ๊ฐ์ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค.
function AnimalSound(animal) {
if(animal === 'dog') return '์์';
if(animal === 'elephant') return '๋ฟ์ฐ์ฐ';
if(animal === 'croco') return '์๊ฑฐ์๊ฑฐ';
if(animal === 'rino') return '์ฝ๋ฟ์ฝ๋ฟ';
return '์๋ฆฌ๊ฐ ์๋ค๋ ค!';
}
console.log(AnimalSound('dog'));
console.log(AnimalSound('elephant'));
console.log(AnimalSound('croco'));
console.log(AnimalSound('rino'));
console.log(AnimalSound('Human'));์ด๋ฐ์์ ๊ฐ๋ ์ฑ์ด ๋จ์ด์ง๋ ์ฝ๋๋ฅผ ๋ ๊ฐ๋ ์ฑ ์๊ฒ ๋ฐ๊ฟ ์ ์๋ค.
function AnimalSound(animal) {
const tasks= {
dog() {
console.log('์์!');
},
elephant() {
console.log('๋ฟ์ฐ์ฐ!');
},
croco() {
console.log('์๊ฑฐ์๊ฑฐ');
},
rino() {
console.log('์ฝ๋ฟ์ฝ๋ฟ');
}
};
if(!tasks[animal]) {
console.log('์๋ฆฌ๊ฐ ์๋ค๋ ค!');
return;
}
tasks[animal]();
}
AnimalSound('dog');
AnimalSound('elephant');
AnimalSound('croco');
AnimalSound('rino');
AnimalSound('Human');๋น๊ตฌ์กฐํ ํ ๋น์ ๊ฐ์ฒด์ ๋ฐฐ์ด,ํจ์๋ฑ์์ ํ ์ ์๋ค.
๊ฐ์ฒด์์์ ๋น๊ตฌ์กฐํ ํ ๋น
const Object = {name: 'Taehwan', age: 18};
const {name, age} = Object;
console.log(name);
console.log(age);๋ฐฐ์ด์์์ ๋น๊ตฌ์กฐํ ํ ๋น
const Array = ['Taehwan', 18];
const [name, age] = Array;
console.log(name);
console.log(age);๋น๊ตฌ์กฐํ ํ ๋น์ ์ด์ ์ ๊ฐ์ฒด๋ฅผ ๋ฐฐ์ธ๋ ์ ๊น ์ฌ์ฉํ์๋ค.
๊ฐ์ด ์ฃผ์ด์ก์๋
const Introduce = {
height: 180,
weight: 200
};
function print({height, weight}) {
console.log(height);
console.log(weight);
}
print(Introduce);๊ฐ์ด ์์ฃผ์ด์ก์๋
const Introduce = { height: 180 };
function print({height, weight = 200}) {
console.log(height);
console.log(weight);
}
print(Introduce);
spread๋ ๊ฐ์ฒดํน์ ๋ฐฐ์ด์์ ๊ฐ์ ์ธ์๋ฅผ ํผํธ๋ฆด์ ์๋ค.
๊ฐ์ฒด
const Goblin = {
name: 'goblin'
};
const Powergoblin = {
...Goblin,
power: true
};
const Kinggoblin = {
...Powergoblin,
army: true
};
console.log(Goblin);
console.log(Powergoblin);
console.log(Kinggoblin);๋ฐฐ์ด
const Goblin = ['goblin'];
const Powergoblin = [...Goblin, 'Power!'];
const Kinggoblin = [...Goblin, ...Powergoblin, 'King has army!'];
console.log(Goblin);
console.log(Powergoblin);
console.log(Kinggoblin);์ด๋ฐ์์ผ๋ก
...์ ์ฌ์ฉํ์ฌ ์ฝ๋๋ฅผ ์์ฑํ ์ ์๋ค.
rest๋ ๊ฐ์ฒด,๋ฐฐ์ด,ํจ์์ ํ๋ผ๋ฏธํฐ์์ ์ฌ์ฉํ ์ ์๋ค.
๊ฐ์ฒด
const Kinggoblin = {
name: 'goblin',
power: true,
army: true
};
const {name, ...rest} = Kinggoblin;
console.log(name);
console.log(rest);๋ฐฐ์ด
const Kinggoblin = ['Goblin', 'Im strong!', 'The king has army!'];
const [name, ...rest] = Kinggoblin;
console.log(name);
console.log(rest);์ด๋ฐ ์์ผ๋ก ๋น๊ตฌ์กฐํ ํ ๋น์ ์ด์ฉํ์ฌ ์ฝ๋๋ฅผ ์์ฑํ ์ ์๋ค. ์ถ๊ฐ์ ์ผ๋ก ๋ฐฐ์ด์์์
rest๋ ๋งจ ์์ ๋์ฌ ์ ์๋ค.
ํจ์ ํ๋ผ๋ฏธํฐ์์์ rest
function add(...rest) {
return rest.reduce((acc, current) => acc + current, 0);
}
console.log(add(1,2,3,4,5,6,7));์์ ์ฝ๋์ฒ๋ผ ์์ฑํ๋ฉด
rest๊ฐaddํจ์์ ํ๋ผ๋ฏธํฐ๋ก ๋ค์ด๊ฐ๋ค. ๊ทธ ์ดํ ๋ฐ์์ค๋ ๊ฐ๋ค์ ๋ฐฐ์ด๋ก ๋ฐ์์ค๋๋ฐ, ์ฌ๊ธฐ์ ๋ฐฐ์ด ๋ด์ฅํจ์์ธreduce๋ฅผ ์ฌ์ฉํ์ฌ ์ ๋ ฅ๋ฐ๋ ๊ฐ๋ค์ ๋ค ๋ํด์ค๋ค. ๊ฒฐ๊ณผ๋28์ด ๋์จ๋ค.
ํจ์ ์ธ์์์์ spread
- ํ๋ผ๋ฏธํฐ: ํจ์ ์์์ ๊ดํธ์์ ๋ฐ์์ค๋ ๊ฐ
- ์ธ์ : ํจ์๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด ๋ณ์์์ ๋ฃ์ด์ฃผ๋ ๊ฐ
function add(...rest) {
return rest.reduce((acc, current) => acc + current, 0);
}
const spread = [1,2,3,4,5,6,7];
console.log(add(...spread));์ด๋ฐ์์ผ๋ก ์ธ์ ์์์
spread๋ฅผ ๋ฐ์์ฌ ์ ์๋ค.
scope๋ ๋ณ์๋ ํจ์๋ฅผ ์ ์ธํ ๋ ์ด๋์ ์ด๋๊น์ง ์ ํจํ์ง์ ๋ฒ์๋ฅผ ์๋ฏธํ๋ค.Global,Function,Block๊ฐ ์๋ค.Global: ์ฝ๋ ์ ์ญ์์ ์ธ ์ ์๋ค.Function: ํจ์ ๋ด๋ถ์์๋ง ์ธ ์ ์๋ค.Block: ์กฐ๊ฑด๋ฌธ ๋ฑ์ ๋ด๋ถ์์๋ง ์ธ ์ ์๋ค.
const result = 'Im Global!';
function Say() {
const result = 'Im Function!';
if (true) {
const result = 'Im Block!';
console.log(result);
}
console.log(result);
}
Say();
console.log(result);- ์ด๋ฐ ์์ผ๋ก ์ฝ๋๋ฅผ ์์ฑํ ์ ์๋ค.
- ๋งจ ์ฒ์์
result๋Global์ด๋ฏ๋ก ์ฝ๋ ์ด๋๊ณณ์์๋ ํธ์ถ์ ํ ์ ์๋ค. - ๋๋ฒ์งธ
result๋function์ด๋ฏ๋ก ํจ์ ์์์๋ง ํธ์ถ์ด ๊ฐ๋ฅํ๋ค. if๋ฌธ ์์result๋Block์ด๋ฏ๋กif๋ฌธ ์์์๋ง ํธ์ถ์ด ๊ฐ๋ฅํ๋ค.
hoisting์ด๋ ์ ์ธ์ ์ ํธ์ถ์ด ๋์์๋ ์ ์ธ์ ์ฝ๋ ๋งจ ์๋ก ์ฌ๋ ค์ฃผ๋ ๊ฒ์ด๋ค.let๊ณผconst๋hoisting์ด ๋ฐ์ํ์ง ์๋๋ค.
Say();
function Say() {
console.log('Hello๐');
}์ด๋ฐ์์ผ๋ก ์ฝ๋๋ฅผ ์์ฑํด๋ ์คํ์ด ๋๋ค.
์ถ์ฒ : ํจ์คํธ์บ ํผ์ค ๊ฐ์ - ๋ฒจ๋กํผํธ์ ํจ๊ปํ๋ ์๋ฐ์คํฌ๋ฆฝํธ ์ ์ฉํ ๋ฌธ๋ฒ