
Object propertiesin ์ฐ์ฐ์Object.hasOwnProperty()const person = {
name: 'Tia',
address: 'Seoul'
};
console.log('name' in person) // true
console.log('person.hasOwnProperty('name)) // true
for โฆ infor ( ๋ณ์์ ์ธ๋ฌธ in ๊ฐ์ฒด ){ ... }for โฆ in ๋ฌธ์ ์ฌ์ฉfor( key in person) {
console.log( `${key}: ${person[key]}` )
}
// name: Tia
//address: Seoul
Object.keys/values/entriesconsole.log(Object.keys(person)); // ['name', 'address']
console.log(Objects.values(person)); // ['Tia', 'Seoul']
console.log(Objects.entries(person)); //['name', 'Tia'], ['address', 'Seoul']
Object.entries(person).forEach(([key, value]) => console.log(key, value));
// name Tia
// address Seoul
์ ์ด๋ฌธ๐ ์ ์ด๋ฌธ( control flow statement )
์กฐ๊ฑด๋ฌธ: ์กฐ๊ฑด์ ๋ฐ๋ผ ์ฝ๋ ๋ธ๋ก์ ์คํ
๋ฐ๋ณต๋ฌธ: ์กฐ๊ฑด์ ๋ฐ๋ผ ์ฝ๋ ๋ธ๋ก์ ๋ฐ๋ณต ์คํ
์กฐ๊ฑด๋ฌธ( conditional statement )์ผํญ์ฐ์ฐ์: ๋จ์ํ ๊ฐ์ ๊ฒฐ์ ํ์ฌ ๋ณ์์ ํ ๋นํ ๊ฒฝ์ฐif ... else: ์กฐ๊ฑด์ ๋ฐ๋ผ ์คํํด์ผ ํ ๋ด์ฉ์ด ๋ณต์กํ์ฌ ์ฌ๋ฌ ์ค์ ๋ฌธ์ด ํ์ํ ๊ฒฝ์ฐswitch: ์กฐ๊ฑด์ด ๋๋ฌด ๋ง์์ ๊ฐ๋
์ฑ์ด ๋จ์ด์ง ๊ฒฝ์ฐif โฆ elseif( ์กฐ๊ฑด์1 ) {
// ์กฐ๊ฑด์1์ด true ์ด๋ฉด ์คํ๋ ์ฝ๋;
}else if ( ์กฐ๊ฑด์2 ) {
// ์กฐ๊ฑด์2๊ฐ true ์ด๋ฉด ์คํ๋ ์ฝ๋;
}else {
// ๋ชจ๋ ์กฐ๊ฑด์์ด false ์ด๋ฉด ์คํ๋ ์ฝ๋;
}
์ผํญ์ฐ์ฐ์์กฐ๊ฑด์ ? ์กฐ๊ฑด์์ด true ์ผ ๊ฒฝ์ฐ ๋ฐํํ ๊ฐ : ์กฐ๊ฑด์์ด false ์ผ ๊ฒฝ์ฐ ๋ฐํํ ๊ฐ
switchcase ๋ฌธ์ผ๋ก ์ด๋, default ๋ optionalswitch ( ํํ์ ){
case ํํ์ 1:
switch ๋ฌธ์ ํํ์๊ณผ ํํ์1์ด ์ผ์น๋ ๊ฒฝ์ฐ ์คํ๋ ์ฝ๋;
break;
case ํํ์ 2:
switch ๋ฌธ์ ํํ์๊ณผ ํํ์2์ด ์ผ์น๋ ๊ฒฝ์ฐ ์คํ๋ ์ฝ๋;
break;
default:
switch ๋ฌธ์ ํํ์๊ณผ ์ผ์นํ๋ case ๊ฐ ์์ ๋ ์คํ๋ ์ฝ๋;
}
๋ฐ๋ณต๋ฌธ( loop statement )for: ๋ฐ๋ณต ํ์๊ฐ ๋ช
ํํ ๋ ์ฃผ๋ก ์ฌ์ฉwhile: ๋ฐ๋ณต ํ์๊ฐ ๋ถ๋ช
ํํ ๋ ์ฃผ๋ก ์ฌ์ฉforfor( ๋ณ์ ์ ์ธ๋ฌธ ๋๋ ํ ๋น๋ฌธ; ์กฐ๊ฑด์; ์ฆ๊ฐ์ ){
์กฐ๊ฑด์์ด true ์ผ ๊ฒฝ์ฐ ๋ฐ๋ณต ์คํ๋ ์ฝ๋;
}
// ์ด์ค ์ค์ฒฉ for๋ฌธ
for( let i = 0; i < arr.length; i ++ ){
for( let j = 1; i <= arr.length; i ++ ){
์กฐ๊ฑด์์ด true ์ผ ๊ฒฝ์ฐ ๋ฐ๋ณต ์คํ๋ ์ฝ๋;
}
๐ฎ
for ?array:
forEach()
object:for ... in
iterable:for ... of
whileif๋ฌธ ์ผ๋ก ํ์ถ ์กฐ๊ฑด์ ๋ง๋ค๊ณ break๋ฌธ ์ผ๋ก ์ฝ๋ ๋ธ๋ก ํ์ถlet count = 0;
//๋ฌดํ ๋ฃจํ
while( true ){
console.log(count);
count ++;
if( count === 3( break;
} // 0 1 2
do โฆ whilelet count = 0;
do {
console.log(count); // 0 1 2
count ++;
}while( count < 3 );
breakswitch ๋ฌธ์ ์ฝ๋ ๋ธ๋ก ํ์ถํ๊ธฐ