๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“ŒDevelopment/JavaScript

[JS] operator(์—ฐ์‚ฐ์ž), if, switch, for loops ์ฝ”๋“œ

by geumsong 2023. 3. 13.

1. String(๋ฌธ์ž์—ด) concatenation (์—ฐ์†)

console.log('my'+'cat');

:  + ๊ธฐํ˜ธ๋ฅผ ์ด์šฉํ•œ ๋ฌธ์ž์—ด์„ ํ•ฉํ•ด์„œ ์ƒˆ๋กœ์šด ๋ฌธ์ž๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

 

 

console.log('1'+2);

:  ๋ฌธ์ž์—ด์— ์ˆซ์ž ๋”ํ•˜๋ฉด ์ˆซ์ž๊ฐ€ ๋ฌธ์ž์—ด์ด ๋ผ ํ•ฉ์ณ์ง

 

 

console.log(`string literals: 1 + 2 = ${1 + 2}`);

:  ๋นฝํ‹ฑ ๊ธฐํ˜ธ(``) ํ™œ์šฉํ•ด์„œ string literals๋„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”๋ฐ $์ด์šฉํ•˜๋ฉด ๋ณ€์ˆ˜ ๊ฐ’์„ ๊ณ„์‚ฐํ•ด์„œ string์œผ๋กœ ํฌํ•จํ•ด์„œ ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค๊ฒŒ๋œ๋‹ค.


๐Ÿ“string literals์˜ ์žฅ์ : 
-  ์ค„๋ฐ”๊ฟˆ ํ•˜๊ฑฐ๋‚˜ ํŠน์ˆ˜๊ธฐํ˜ธ์ธ ์‹ฑ๊ธ€์ฝ”ํŠธ('')๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅ๋จ


ex)
''''
1 + 2 = ${1 + 2}`);

 

 

 

 

+ console.log('ellie's book');
-์‹ฑ๊ธ€์ฝ”๋“œ๋กœ ๋ฌธ์ž์—ด ๋งŒ๋“ค๋ฉด ์‹ฑ๊ธ€์ฝ”ํŠธ๊ฐ€ ์ธ์‹๋˜์ง€X. ์ด๋Ÿด ๊ฒฝ์šฐ ๋ฐฑ์Šฌ๋Ÿฌ์‹œ๋ฅผ ์ด์šฉํ•ด์•ผ ์ œ๋Œ€๋กœ ํ‘œ์‹œ๋จ. 

ex)
console.log('ellie\'s book');

console.log('ellie\'s book');



console.log('ellie's /nbook');

์ค„๋ฐ”๊ฟˆํ• ๋•Œ๋Š” \n
ex)
console.log('ellie's /nbook');

\t ํƒญ
ex)
console.log('ellie's /n\tbook');
===>์ด๋Ÿฐ ํŠน์ˆ˜๊ธฐํ˜ธ๋Š” ๋”ฐ๋กœ ๊ฒ€์ƒ‰ํ•˜๊ธฐ. ์•Œ์•„๋งŒ๋‘˜๊ฒƒ.

 

 


2. Numeric operators (์ˆซ์ž์—ฐ์‚ฐ์ž)

:  ์ˆซ์ž๋ฅผ ์—ฐ์‚ฐํ•  ์ˆ˜ ์žˆ์Œ.

console.log(1 + 1); //add
console.log(1 - 1); // substract
console.log(1 / 1); // divide
console.log(1 * 1); // multiply
console.log(5 % 2); // remainder
console.log(2 ** 3); // exponentiation

 


3. Increment(++) and decrement(--) operators

:์ฆ๊ฐ€(++)์™€ ๊ฐ์†Œ(--) ์—ฐ์‚ฐ์ž

let counter = 2;
const preIncrement = ++counter;

// counter = counter + 1;
// preIncrement = counter;

-  counter๋ผ๋Š” ๋ณ€์ˆ˜๊ฐ€ ์žˆ์œผ๋ฉด ๋ณ€์ˆ˜์•ž์— ++๊ธฐํ˜ธ๋ฅผ ์•ž์— ๋ถ™์—ฌ์ฃผ๋Š” ๊ฒƒ์€ ์•„๋ž˜ // ๋‚ด์šฉ๊ณผ ๋™์ผํ•จ.

๋ณ€์ˆ˜(counter) ์•ž์— ++๋ฅผ ๋ถ™์ด๋Š” ๊ฒƒ์€ ๋ณ€์ˆ˜(counter)์— 1์„ ๋”ํ•œ ํ›„ ๊ทธ ๋ณ€์ˆ˜(counter)์˜ ๊ฐ’์ด ํ•ด๋‹น ๋ณ€์ˆ˜(preIncrement) ์˜ ๊ฐ’์ด๋ผ๋Š” ๋œป.

 

 

 

 

 

const postIncrement = counter++;

// postIncrement = counter;
// counter = counter + 1;

:  postIncrement  ๋ณ€์ˆ˜ ๋’ค์— ++๋ฅผ ๋ถ™์ด๋ฉด counter ๋ณ€์ˆ˜๋ฅผ postIncrement์— ํ• ๋‹นํ•œ ํ›„ counter ๋ณ€์ˆ˜์— 1์„ ๋”ํ•˜๋Š” ๊ฒƒ.

-  counter ๋‹ค์Œ์— ++๊ธฐํ˜ธ๋ฅผ ๋ถ™์ด๋ฉด ๋ณ€์ˆ˜ ๊ฐ’(counter = 2, ์—ฌ๊ธฐ์„œ ++counter ํ•ด์„œ 3์ด ์ฆ๊ฐ€๋œ ์ƒํƒœ์ด๋ฏ€๋กœ) 3์„ post๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ๋‹ค์Œ counter์˜ ๊ฐ’์„ 1 ์ฆ๊ฐ€์‹œํ‚จ๋‹ค. ๊ทธ ๋‹ค์Œ ์นด์šดํ„ฐ์˜ ๊ฐ’์ด 1 ์ฆ๊ฐ€ํ•˜๋‹ˆ๊นŒ ์นด์šดํ„ฐ์˜ ๊ฐ’์€ 4.

 

 

 

===> -- ๋„ ๊ฐ™์€ ์›๋ฆฌ์ด๋‹ค.

 

 

 


 

4. Assignment operators

: ํ• ๋‹น ์—ฐ์‚ฐ์ž

// 4. Assignment operators
let x = 3;
let y = 6;
x += y; // x = x + y;
x -= y; // x = x - y;
x *= y; // x = x * y;
x /= y; // x = x / y;

-  ์ค‘๋ณต๋˜๋Š” x๊ฐ’์„ ํ•˜๋‚˜๋กœ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด +=, -=, *=..... ๋“ฑ์œผ๋กœ ํ‘œํ˜„

 

 

 


5. Comparison operators

:๋น„๊ต์—ฐ์‚ฐ์ž

// 5. Comparison operators
console.log(10 < 6); // less than
console.log(10 <= 6); // less than or equal
console.log(10 > 6); // greater than
console.log(10 >= 6); // greater than or equal

 


6. Logical operators   โ˜…

:  ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž

: or, and, not

// 6. Logical operators : || (or), && (and), ! (not)
const value1 = false;
const value2 = 4 <2;

 

 

<  || (or)  >

: value๋‚˜ expretion๋“ค ์ค‘ ํ•˜๋‚˜๋ผ๋„ true๋ฉด true๋กœ ๋˜๋Š” ์—ฐ์‚ฐ์ž.
: ํ•จ์ˆ˜ check ๋Š” ์“ธ๋ฐ ์—†์ด ์‹œ๊ฐ„๋‚ญ๋น„ ํ•œ ํ›„ ๊ฒฐ๊ตญ, true๋ฅผ returnํ•œ๋‹ค.

// || (or), finds the first truthy value
console.log(`or: ${value1 || value2 || check()}`);

function check(){
    for (let i = 0; i < 10; i++){
        //wasting time
        console.log('๐Ÿ˜ญ');
    }
}

์ค‘์š” POINT!
๐Ÿ“ ์•ž์—์„œ true๊ฐ€ ๋‚˜์˜ค๋ฉด ๋ฉˆ์ถ˜๋‹ค. ํ•˜๋‚˜๋ผ๋„ true๋ฉด ๋๋‚˜๋ฒ„๋ฆฌ๊ธฐ ๋•Œ๋ฌธ.
๐Ÿ“ ๋ฌด๊ฑฐ์šด ํ•จ์ˆ˜๋Š” ๋’ค๋กœ, ์‹ฌํ”Œํ•œ value์˜ ๊ฐ’์„ ์•ž์— ๋‘ฌ์•ผ ์•ž์— ๊ฐ’์ด false์ผ ๋•Œ ๋งˆ์ง€๋ชปํ•ด ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ด ์ œ์ผ ์ข‹๋‹ค.

 

 

<  && (and)  >

๋ชจ๋‘ true๊ฐ€ ๋ผ์•ผํ•œ๋‹ค.
: ์•ž์—์„œ false๊ฐ€ ๋‚˜์˜ค๋ฉด, ๋’ค์—๋Š” ์ฒดํฌX.
: || (or)๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•จ์ˆ˜๋ฅผ ๋’ค์ชฝ์œผ๋กœ ํ˜ธ์ถœํ•  ๊ฒƒ.

// && (and), finds the first falsy value
console.log(`and: ${value1 && value2 && check()}`);

// often used to compress long if-statement
// nullableObject && nullableObject.something
if (nullableObject != null){
    nullableObject.something;
}

:  ๊ฐ„ํŽธํ•˜๊ฒŒ null ์ฒดํฌํ•  ์ˆ˜ ์žˆ๋‹ค.
:  object๊ฐ€ null์ด๋ฉด false์ด๊ธฐ ๋•Œ๋ฌธ์— ๋’ค์— ์‹คํ–‰X. null object๊ฐ€ null์ด ์•„๋‹๋•Œ๋งŒ something ์ด๋ผ๋Š” value๋ฅผ ๋ฐ›์•„์˜ค๊ฒŒ๋œ๋‹ค. 

 

if (nullableObject != null){
    nullableObject.something;
}
์ด ์ฝ”๋“œ๋กœ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

<  ! (not)  >

:  ๊ฐ’์„ ๋ฐ˜๋Œ€๋กœ ๋ฐ”๊ฟ”์คŒ
:  value 1์ด true ์ด๊ธฐ ๋•Œ๋ฌธ์—, false๋กœ ๋ฐ”๊ฟ”์คŒ.

// ! (not)
console.log(!value1);

 

 

 


7. Equality

// 7. Equality
const stringFive = '5';
const numberFive = 5;

// == loose equality, with type conversion
console.log(stringFive == numberFive);
console.log(stringFive != numberFive);

// === strict equality, no type conversion
console.log(stringFive === numberFive);
console.log(stringFive !== numberFive);

 

loose equality (==, ํƒ€์ž…์„ ๋ณ€๊ฒฝํ•ด์„œ ๊ฒ€์‚ฌํ•œ๋‹ค)
:  stringFive๊ณผ numberFive์ด ๊ฐ™๋‹ค๊ณ  ๋‚˜์˜ด >> stringFive๊ฐ€ ๋ฌธ์ž์—ด์ด๊ธด ํ•˜์ง€๋งŒ ์•ˆ์— ์žˆ๋Š” ๊ฑด ์ˆซ์ž 5๋‹ˆ๊นŒ ๋‘˜์ด ๋˜‘๊ฐ™์•„! ๋ผ๊ณ  ํ•ด์„๋˜๋Š” ๊ฒƒ.

(==) ๊ฐ™๋‹ค

(!=) ๊ฐ™์ง€์•Š๋‹ค

 

strict equality (===, ํƒ€์ž… ๋‹ค๋ฅด๋ฉด ๋‹ค๋ฅธ๊ฒƒ์œผ๋กœ ๊ฒ€์‚ฌ)
:  stringFive๊ณผ numberFive์ด ๋‹ค๋ฅด๋‹ค๊ณ  ๋‚˜์˜ด
:  ์ฝ”๋”ฉ ๊ฒ€์‚ฌํ•  ๋•Œ sctrict equality๋ฅผ ์‚ฌ์šฉ ํ•˜๋Š” ๊ฑธ ์ถ”์ฒœ

 


// object equality by reference
const ellie1 = { name: 'ellie'};
const ellie2 = { name: 'ellie'};
const ellie3 = ellie1;
console.log(ellie1 == ellie2);
console.log(ellie1 === ellie2);
console.log(ellie1 === ellie3);

-  object๋Š” ๋ฉ”๋ชจ๋ฆฌ์— ํƒ‘์žฌ๋  ๋•Œ reference๋กœ ์ €์žฅ๋จ.
-  ellie1๊ณผ ellie2๋Š” ๋˜‘๊ฐ™์€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด๊ฐ„ object์ง€๋งŒ ์‹ค์ œ ๋ฉ”๋ชจ๋ฆฌ์— 1๊ณผ 2์—๋Š” ๋‹ค๋ฅธ reference๊ฐ€ ๋“ค์–ด์žˆ๋‹ค.
-  ellie3์€ ellie1์˜ reference๊ฐ€ ํ• ๋‹น๋ผ์žˆ์–ด 1๊ณผ ๋˜‘๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์กŒ๋‹ค.

 

โ–ผ

console์—๋Š” ์–ด๋–ป๊ฒŒ ์ถœ๋ ฅ๋ ๊นŒ?

console.log(ellie1 == ellie2); //false;
console.log(ellie1 === ellie2); //false;
console.log(ellie1 === ellie3); //true;

- ellie1๊ณผ ellie2๋Š” ๋˜‘๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์กŒ์ง€๋งŒ reference๊ฐ€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— false

-  ellie1 ellie2๋Š” ๋˜‘๊ฐ™์€ ํƒ€์ž…์ด๋“  ์•„๋‹ˆ๋“  reference์˜ ๊ฐ’์ด ๋‹ค๋ฅด๊ธฐ๋•Œ๋ฌธ์— false

-  ๊ฐ™์€ reference์˜ ๊ฐ’์„ ํ• ๋‹นํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— true

ellie1 > ref1 > name

ellie2> ref2 > name

ellie3 >ref1

//equlity - puzzler
console.log(0 == false);  //0์€ false๋กœ ๊ฐ„์ฃผ๋˜๊ธฐ ๋•Œ๋ฌธ์— true
console.log(0 === false);  //0 ์€ boolean type ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— false
console.log('' == false);  //๋น„์–ด์žˆ๋Š” ๊ฐ’์€ false๋กœ ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์— true
console.log('' === false); //๋น„์–ด์žˆ๋Š” ๊ฐ’ ์ž์ฒด๋Š” boolean type์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— false
console.log(null == undefined); //null๊ณผundefined๋Š” ๊ฐ™์€ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผ๋˜๊ธฐ ๋•Œ๋ฌธ์—  true
console.log(null === undefined); // ํ•˜์ง€๋งŒ /null๊ณผundefined๋Š” ๋‹ค๋ฅธ type์ด๊ธฐ ๋•Œ๋ฌธ์— false
[์ถœ์ฒ˜] JavaScript #4 [defer, ๋ณ€์ˆ˜, operators]|์ž‘์„ฑ์ž zero

 


8. Conditional operator

:  if, else if, else

// 8. Conditional operators : if
// if, else if, else
const name = 'ellie';
if (name === 'ellie') {
    console.log('Welcome, Ellie!');
} else if (name === 'coder'){
    console.log('You are amazing coder');
} else {
    console.log('unknown');
}

-  if (  )๊ฐ€ true → ๋‹ค์Œ { }์‹คํ–‰.  (console๋กœ๊ทธ์—์„œ Welcome, Ellie!๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค)

 

๋งŒ์•ฝ const name์ด code๋ผ๋ฉด?

// 8. Conditional operators : if
// if, else if, else
const name = 'coder';
if (name === 'ellie') {
    console.log('Welcome, Ellie!');
} else if (name === 'coder'){
    console.log('You are amazing coder');
} else {
    console.log('unknown');
}

 

-  ๋งŒ์•ฝ ์ด๋ฆ„์ด coder ๋ผ๋ฉด ellie๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ else if ๋กœ ๋„˜์–ด๊ฐ„๋‹ค. ๊ทธ๊ฒƒ๋„ ์•„๋‹ˆ๋ฉด ๊ทธ ๋‹ค์Œ์— ์žˆ๋Š” else{ } ๋กœ ๋„˜์–ด๊ฐ„๋‹ค.

 


9. Ternary operator

:  ์‚ผํ•ญ ์—ฐ์‚ฐ์ž

// 9. Ternary operator : ?
// condition ? value1 : value2;
console.log(name === 'ellie' ? 'yes' : 'no');

:  ? ์‚ฌ์šฉ์‹œ, true๋ฉด :์˜ ์™ผ์ชฝ์— ์žˆ๋Š” ๊ฒƒ(yes)์„ ์‚ฌ์šฉํ•˜๊ณ  false๋ฉด : ์˜ค๋ฅธ์ชฝ์— ์žˆ๋Š” ๊ฒƒ(no)์„ ์‹คํ–‰ํ•œ๋‹ค.
:  ๊ฐ„๋‹จํ•œ ์ถœ๋ ฅ์‹œ ๋งŽ์ด ์‚ฌ์šฉ (๋ณต์žกํ•ด์ง€๋ฉด ๊ณ„์† ์จ์•ผํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— if ํ•จ์ˆ˜๋‚˜ switch๋ฅผ ์“ฐ๋Š” ๊ฒŒ ์ข‹๋‹ค)
:  ellie๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ no๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค

 


10. Switch operators

 

// 10. Switch statement
// use for multiple if checks
// use for enum-like value check
// use for multiple type checks in TS
const browser = 'IE';
switch (browser) {
    case 'IE':
        console.log('go away!');
        break;
    case 'Chrome':
        console.log('love you!');
         break;
    case 'Firefox':
        console.log('love you!');
        break;
    default:
        console.log('same all!');
        break;
}

:  switch ์•ˆ์— ๊ฐ’ (browser)๊ฐ’์ด 'IE'์ด๋ฉด 'go away!'์‹คํ–‰ํ•˜๊ฒŒ ๋˜๊ณ ,
break; ๋ฉˆ์ถ˜๋‹ค๋Š” ๋œป.
chrome ์ด๋ฉด ์ฝ˜์†”๋กœ๊ทธ love you๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ๋ฉˆ์ถ”๊ณ ....

๋˜‘๊ฐ™์€ ๊ฐ’์ด ๊ฒน์น˜๋ฉด ์—ฐ๋‹ฌ์•„ ์“ฐ๋ฉด ์ž๋™์ ์œผ๋กœ ๋ฌถ์–ด์„œ ์ถœ๋ ฅ๋œ๋‹ค.

ex)
case 'Chrome':
case 'Firefox':
console.log('love you!');
break;

-  if์—์„œ else if ๋‹ค์Œ else if...๊ณ„์† ๋ฐ˜๋ณตํ•œ๋‹ค๋ฉด switch๋ฅผ ์‚ฌ์šฉํ•ด๋ณด๋Š” ๊ฒƒ์„ ๊ถŒ์œ . 

 

 


11. Loops (๋ฐ˜๋ณต๋ฌธ)

:  while (  )์˜ ๊ฐ’์ด false๊ฐ€ ๋ ๋•Œ๊นŒ์ง€ ๊ณ„์† ๋ฐ˜๋ณต

i๊ฐ€ 0๋ณด๋‹ค ํฌ๋ฉด console.log(`while:${i}`);์„ ์‹คํ–‰ํ•˜๊ณ  i์˜ ๊ฐ’์„ 1 ๋งˆ์ด๋„ˆ์Šค ํ•ด์ค€๋‹ค.

// 11. Loops
// while loop, while the condition is truthy,
// body code is executed.
let i = 3;
while (i > 0) {
    console.log(`while: ${i}`);
    i--;
}

 

 

do-while loop

:  ์•ž์— ์žˆ๋Š” do{  }๋ธ”๋Ÿญ์„ ์‹คํ–‰ํ•œ ๋‹ค์Œ while์˜ ์กฐ๊ฑด์„ ๊ฒ€์‚ฌํ•˜๊ณ  ๋ฉˆ์ถค.
: ๋ธ”๋Ÿญ์„ ๋จผ์ € ์‹คํ–‰ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด do while, ์กฐ๊ฑด๋ฌธ์ด ๋งž์„๋•Œ block์ด ์‹คํ–‰๋ผ์•ผํ•œ๋‹ค๋ฉด while.

// do while loop, body code is executed first,
// then check the condition.
do {
    console.log(`do while: ${i}`);
    i--;
} while (i > 0);

 

 

 

for-loop

//for loop, for(begin; condition; step)
for(i = 3; i > 0; i--){
    console.log(`for:${i}`);
}

for loop ๋Š” begin์€ ์ฒ˜์Œ ํ•œ๋ฒˆ๋งŒ ์‹คํ–‰๋˜๊ณ  ๊ทธ ํ›„์—” condition์ด ๋งž์œผ๋ฉด ์‹คํ–‰, ์‹คํ–‰๋œ ํ›„์— step์ด ์‹คํ–‰๋˜์„œ begin์ด ์ฒ˜์Œ ํ•œ ๋ฒˆ๋งŒ ์‹คํ–‰๋˜๊ณ  ๋‚˜๋ฉด condition๊ณผ step๋งŒ ์กฐ๊ฑด์— ๋งž์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด์„œ ์‹คํ–‰

(์™„์ „ ํ’€์–ด์„œ ์„ค๋ช…ํ•˜์ž๋ฉด, i=3 ํ•œ๋ฒˆ๋งŒ ์‹คํ–‰ํ•จ. 3์€ 0๋ณด๋‹ค ํฌ๋‹ˆ? true => console๋กœ๊ทธ ์‹คํ–‰ → i--๋Š” 1์„ ๊ฐ์†Œํ•œ๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ 2๋Š” 0๋ณด๋‹ค ํฌ๋‹ˆ? true =>console๋กœ๊ทธ ์‹คํ–‰..... → ๋ฐ˜๋ณต)

 

 

<inline variable declaration>

for(let i = 3; i > 0; i = i - 2){
    //inline variable declaration
    console.log(`inline variable for:${i}`);
}

:  for ๋ฌธ ์•ˆ์—์„œ let์ด๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•ด์„œ ์‹คํ–‰ํ•  ์ˆ˜๋„ ์žˆ์Œ.

 

 

< nested loops >

//nested loops
for (let i = 0; i < 10; i++){
    for(let j = 0; j < 10; j++){
        console.log(`i: ${i}, j: ${j}`);
    }
}

for๋ฌธ ์•ˆ์— for๋ฌธ์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด๋Ÿฐ ๊ฒฝ์šฐ

i๊ฐ€ 0์ด๋ฉด j๊ฐ€ 1๋ถ€ํ„ฐ 9๊นŒ์ง€์ธ ๊ฒฝ์šฐ๊ฐ€ ์ถœ๋ ฅ๋œ ํ›„

๋‹ค์‹œ i๊ฐ€ 1์ด๋ฉด j๊ฐ€ 1๋ถ€ํ„ฐ 9๊นŒ์ง€์ธ ๊ฒฝ์šฐ,

i๊ฐ€ 2์ด๋ฉด j๊ฐ€ 1๋ถ€ํ„ฐ 9๊นŒ์ง€์ธ ๊ฒฝ์šฐ ,

.

.

.

i๊ฐ€ 9์ด๋ฉด j๊ฐ€ 1๋ถ€ํ„ฐ 9๊นŒ์ง€์ธ ๊ฒฝ์šฐ

๊ฐ€ ๋ชจ๋‘ ์ถœ๋ ฅ!

โ€‹

>์ด๋Ÿฌ๋Š” ๊ฒฝ์šฐ cpu์— ์ข‹์ง€ ์•Š์Œ. ๋˜๋„๋ก์ด๋ฉด ํ”ผํ•˜๋Š” ๊ฒŒ ์ข‹๋‹ค.

(์—ฌ๋Ÿฌ๋ฒˆ ์ฝ์–ด๋ด์•ผ ์ดํ•ด๋จ)

 

 

<break, continue>

:  loop์•ˆ์—์„œ๋Š” break, continue ๋ผ๋Š” ํ‚ค์›Œ๋“œ ์‚ฌ์šฉํ•ด์„œ ๋๋‚ผ ์ˆ˜ ์žˆ๋‹ค.
-  break : ๋ฉˆ์ถค
-  continue : ์ง€๊ธˆ ๊ฒƒ์„ ๋ฉˆ์ถ”๊ณ , ๋‹ค์Œ ์Šคํ…์œผ๋กœ ๋„˜์–ด๊ฐ.

 

 

 

 

๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์ž

Q1. iterate from 0 to 10 and print only even numbers(use continue)

:  ์ˆซ์ž 0-10๊นŒ์ง€ ์ˆซ์ž๋ฅผ ์ง์ˆ˜๋งŒ ํ”„๋ฆฐํŠธ ํ•ด์„œ 'continue' ์จ์„œ ๋งŒ๋“ค๊ธฐ

 

๋‚ด ๋‹ต:

for(let i=0; i < 10; i + 2) {

์“ฐ๋‹ค๋ง์•˜๋‹ค.. 11์ธ์ง€ 10์ธ์ง€ ๊ธด๊ฐ€๋ฏผ๊ฐ€ํ–ˆ๋Š”๋ฐ ์—ญ์‹œ๋‚˜ 11์ด์—ˆ๋‹ค..ใ…‹ใ…‹ 

 

 

<์ •๋‹ต>

for (let i = 0; i < 11; i++){
    if (i % 2 !== 0) {
        continue;
    }

 

:  i๋ฅผ 2๋กœ ๋‚˜๋ˆด์„ ๋•Œ ๋‚˜๋จธ์ง€ ๊ฐ’์ด(i%2)์ด 0์ด ์•„๋‹ ๊ฒฝ์šฐ(!== 0) ํ•ด๋‹น ๊ฐ’์€ ๊ฑด๋„ˆ๋›ด๋‹ค.

 

 

Q2. iterate frome 0 to 10 and print numbers until reaching 8 (use break)

:  ์ˆซ์ž 0-10๊นŒ์ง€ ํ”„๋ฆฐํŠธํ•˜๋˜ ์ˆซ์ž 8์ด ๋‚˜์˜ค๋ฉด 'break'์จ์„œ ๋ฉˆ์ถ”๊ธฐ

๋‚ด ๋‹ต:
for(let i = 0; i  < 10; i++) {
if (i = 8) {
break;
}

 

 

<์ •๋‹ต>

for (let i = 0; i < 11; i++) {
    if (i > 8) {
        break;
    }

-  1๋ถ€ํ„ฐ 10๊นŒ์ง€์˜€์œผ๋‹ˆ๊นŒ i < 11

-  i๊ฐ€ 8๋ณด๋‹ค ์ปค์ง„๋‹ค๋ฉด ๋ฉˆ์ถ”๊ธฐ

 

 

 

 

 

[์ถœ์ฒ˜] JavaScript #4 [defer, ๋ณ€์ˆ˜, operators]|์ž‘์„ฑ์ž zero] ๋งŽ์ด ์ฐธ๊ณ ํ•จ!

728x90

์ตœ๊ทผ๋Œ“๊ธ€

์ตœ๊ทผ๊ธ€

skin by ยฉ 2024 ttutta