๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
JavaScript/Nomad Coders

[TIL] ๐Ÿ’กDay 3 - function, argument, return, prompt, typeof, ์กฐ๊ฑด๋ฌธ

by vividmin 2022. 3. 12.
320x100

 

โœ” ์ด ํฌ์ŠคํŒ…์€ ๋…ธ๋งˆ๋“œ์ฝ”๋” - "๋ฐ”๋‹๋ผ JS๋กœ ํฌ๋กฌ ์•ฑ ๋งŒ๋“ค๊ธฐ๋ฅผ ๋“ค์œผ๋ฉด์„œ ๊นƒํ—ˆ๋ธŒ์— ๊ธฐ๋กํ•œ ํ•„๊ธฐ๋ฅผ ์˜ฎ๊ธด ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

 

์•ž์—์„œ ๋ฐฐ์šด ๋‚ด์šฉ๋“ค์„ ๋ณต์Šตํ•˜๊ณ , function(ํ•จ์ˆ˜), argument(์ธ์ˆ˜), return(๋ฐ˜ํ™˜ ๊ฐ’), prompt, typeof, ์กฐ๊ฑด๋ฌธ์— ๋Œ€ํ•ด ๋ฐฐ์› ๋‹ค.

 

๐ŸŒฑ Function

๐Ÿ‘‰ ๊ณ„์† ๋ฐ˜๋ณตํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ ์กฐ๊ฐ, ์–ด๋–ค ์ฝ”๋“œ๋ฅผ ์บก์Šํ™”ํ•ด์„œ ์‹คํ–‰์„ ์—ฌ๋Ÿฌ ๋ฒˆ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด ์ค€๋‹ค.

  • function ์—†์„ ๋•Œ ๐Ÿ™…‍โ™€๏ธ
// ๋ถˆ ํ•„์š”ํ•œ ๋ฐ˜๋ณต ๋„ˆ๋ฌด ๋งŽ๋‹ค.console.log("Hello my name is ์ด๋ฆ„2");
console.log("Hello my name is ์ด๋ฆ„1");
console.log("Hello my name is ์ด๋ฆ„3");
console.log("Hello my name is ์ด๋ฆ„4");
console.log("Hello my name is ์ด๋ฆ„5");
console.log("Hello my name is ์ด๋ฆ„6");
  • function ์žˆ์„ ๋•Œ ๐Ÿ™†‍โ™€๏ธ
function SayHello () {
    // function์ด ์‹คํ–‰ ๋  ๋•Œ๋งˆ๋‹ค ๋ฐ˜๋ณต ๋  ๋ถ€๋ถ„
    console.log("Hello!");
};

SayHello();    // ์—ฌ๋Ÿฌ ๋ฒˆ ํ˜ธ์ถœ ๊ฐ€๋Šฅ
SayHello();

 

 

๐ŸŒฑArgument

๐Ÿ‘‰ function์„ ์‹คํ–‰ํ•˜๋Š” ๋™์•ˆ ์–ด๋–ค ์ •๋ณด๋ฅผ function์—๊ฒŒ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•

  • function ์•ˆ์—์„œ data๋ฅผ ๋ฐ›๋Š” ๋ฐฉ๋ฒ• โ“
    (์ฒซ ๋ฒˆ์งธ ์˜ˆ์‹œ์ฒ˜๋Ÿผ console.log๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์ž…๋ ฅํ•˜์ง€ ์•Š์•„๋„ argument๋งŒ ๋ฐ”๊ฟ”์„œ ๊ฐ„ํŽธํ•˜๊ฒŒ ๊ฐ’์„ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.)
  • ( ) ์•ˆ์— ๋ญ”๊ฐ€๋ฅผ ์ ๋Š” ๊ฒƒ.
function plus(potato, salad) {     // argument์˜ ์ด๋ฆ„ ๊ผญ a, b์•„๋‹ˆ์–ด๋„ ๊ฐ€๋Šฅ๐Ÿ˜„
console.log(potato + salad);
}

// ๋‹จ, potato๋Š” function๋ฐ–์—์„œ๋Š” ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋‹ค.

plus(5, 10);
plus(1.333, 9898);
plus(9898, 1.3333);

 

 

๐ŸŒฑ Return

๐Ÿ‘‰ return์€ function์—์„œ ๊ฐ’์„ ์ €์žฅํ•ด ์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ์ด ๊ฐ’์€ return value๊ฐ€ ๋œ๋‹ค.๐Ÿ˜ฒ

  • return ์—†์„ ๋•Œ ๐Ÿ™…‍โ™€๏ธ
// ex) ๐Ÿ“ฒ ๊ณ„์‚ฐ๊ธฐ ํ•จ์ˆ˜const calculator = {
        add: function(a, b) {
            console.log(a + b);
        },
        minus: function(a, b) {
            console.log(a - b);
        },
        multi: function(a, b) {
            console.log(a * b);
        },
        div: function(a, b) {
            console.log(a / b);
        },
        powerof: function(a, b) {
            console.log(a ** b);
        },
    };

calculator.add(3, 4);// console์— ๊ฒฐ๊ณผ ๋‚˜์˜จ๋‹ค, ๊ฐ’์ด ์ €์žฅ๋˜์ง€๋Š” ์•Š๋Š” ์ƒํƒœ๐Ÿค”
calculator.minus(3, 4);
calculator.multi(3, 4);
calculator.div(3, 4);
calculator.powerof(3, 4);
  • return ์žˆ์„ ๋•Œ ๐Ÿ™†‍โ™€๏ธ
// ex) ๐Ÿ“ฒ ๊ณ„์‚ฐ๊ธฐ ํ•จ์ˆ˜const calculator = {
        plus: function(a, b) {
            return a + b;
        },
        minus: function(a, b) {
            return a - b;
        },
        time: function(a, b) {
            return a * b;
        },
        divide: function(a, b) {
            return a / b;
        },
        power: function(a, b) {
            return a ** b;
        },
    };

/**
์ด ๊ฒฐ๊ณผ๋“ค์€ ์œ ๊ธฐ์ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค. 
์ด๊ฒƒ์ด console.log์™€ return์˜ ์ฐจ์ด
*/
const plusResult = calculator.plus(2, 3);
const minusResult = calculator.minus(plusResult, 10);
const timeResult = calculator.time(10, minusResult);
const divideResult = calculator.divide(timeResult, plusResult);
const powerResult = calculator.power(divideResult, minusResult);
  • function์— return์ด ์žˆ์„ ๋•Œ ๋ณ€์ˆ˜์— function์„ ํ• ๋‹นํ•˜๋ฉด, function์˜ return์ด ๋ณ€์ˆ˜์— ์ €์žฅ๋œ๋‹ค.
    • ์•„๋ž˜์˜ ๊ฒฐ๊ณผ๋ฅผ ์ฝ”๋“œ๋กœ ๋ฐ›๊ณ  ์‹ถ๋‹ค๋ฉด โ“   'ํ•จ์ˆ˜๋Š” ์–ด๋–ค ์ผ์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ์•Œ๋ ค์ฃผ๋Š” ๊ฒƒ์ด๋‹ค'๋ผ๊ณ  ์ƒ๊ฐ
// ex) ๐Ÿ‘€ ํ•œ๊ตญ๋‚˜์ด ๊ณ„์‚ฐ๊ธฐconst age = 96;

  function calculateKrAge(ageOfForeigner) {
      return ageOfForeigner + 2;
  }

const KrAge = calculateKrAge(age);  // return์ด ์žˆ๋‹ค๋ฉด ๊ฐ’์„ ์ €์žฅ ํ•  ์ˆ˜ ์žˆ๋‹ค.
console.log(KrAge);
// ๋งŒ์•ฝ ์œ„์˜ ์ฝ”๋“œ์— return์ด ์—†์œผ๋ฉด ๊ฒฐ๊ณผ๊ฐ€ undefined๋กœ ๋‚˜์˜จ๋‹ค.
  • ๐Ÿ“Œ ์•„์ฃผ ์ค‘์š”ํ•œ ๊ฐœ๋… โ—
    • ํ•œ ๋ฒˆ return ํ•˜๋ฉด function์€ ๋๋‚œ๋‹ค.
      โœ” return์„ ํ•˜๋ฉด function์€ ์ž‘๋™์„ ๋ฉˆ์ถ”๊ณ , ๊ฒฐ๊ณผ ๊ฐ’์„ return ํ•˜๊ณ  ๋๋‚œ๋‹ค.
// ์˜ˆ์‹œ)
const calculator = {
    plus: function(a, b) {

        // ์ด ์ฝ”๋“œ๋Š” ์ž‘๋™ โญ•
        console.log("hello");
        return a + b;
        
        // ์ด ์ฝ”๋“œ๋Š” ์ ˆ๋Œ€ ์ž‘๋™ํ•˜์ง€ โŒ 
        // (returnํ•˜๋ฉด ํ•จ์ˆ˜๋Š” ๋๋‚˜๊ธฐ ๋•Œ๋ฌธ!)
        console.log("hello");
    }
}

 

 

๐ŸŒฑ Prompt์˜ ์‚ฌ์šฉ๊ณผ typeof, parseInt

๐Ÿ‘‰ ๋งค์šฐ ์˜ค๋ž˜๋œ JS, ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์„ ๋ฐ›๋Š” ํ•จ์ˆ˜

  • promt๊ฐ€ ์‹คํ–‰๋  ๋•Œ๋Š” js๊ฐ€ ์ž ์‹œ ๋ฉˆ์ถ˜๋‹ค. ์ด๊ฒƒ์€ ๋งค์šฐ ์˜ค๋ž˜๋œ js,
  • ์š”์ฆ˜์€ ์ž˜ ์“ฐ์ง€ ์•Š๊ณ  custom ํ•œ promt๋ฅผ ์ง์ ‘ ๋งŒ๋“œ๋Š” ์ถ”์„ธ์ด๋‹ค.
const age = prompt("How old are you?");

console.log(age);// ์ž…๋ ฅ๋ฐ›์€ age๋ฅผ ํ™•์ธ๊ฐ€๋Šฅํ•˜๋‹ค.
  • typeof
    • ์‚ฌ์šฉ ์‹œ ( ) ์•ˆ์˜ ๊ฐ’์˜ ํƒ€์ž…์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
console.log(typeof "15", typeof (15));// string, number
  • parseInt
    • int ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•ด ์ฃผ๋Š” ํ•จ์ˆ˜
const age = parseInt(prompt("How old are you?"));  
                                                    /** 
                                                    ์ˆซ์ž๊ฐ€ ์•„๋‹ˆ๋ฉด NaN ๋‚˜์˜จ๋‹ค.
                                                    */
console.log(typeof "15", typeof parseInt("15"));
  • NaN
    • = ์ˆซ์ž๊ฐ€ ์•„๋‹ˆ๋‹ค (Not A Number)
    • isNaN( ) ์–ด๋–ค ๊ฒƒ์ด NaN์ธ์ง€ ํŒ๋ณ„ํ•  ๋Œ€ ์‚ฌ์šฉ
    • isNaN( )์€ true or false๋ฅผ return ํ•œ๋‹ค. (boolean)
    const age = parseInt(prompt("How old are you?"));

    if (isNaN(age)) {
        console.log("Please write a number");// isNan = true
    }else {
        consoled.log("Thank you for write your age.");// isNaN = false
    }

 

 

๐ŸŒฑ Conditionals, ์กฐ๊ฑด๋ฌธ

๐Ÿ‘‰ ์กฐ๊ฑด์ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ผ ๋•Œ, if์™€ else if, else ์ด์šฉํ•ด์„œ ์กฐ๊ฑด๋ฌธ ์‚ฌ์šฉ

  • ์กฐ๊ฑด ํ•œ ๊ฐ€์ง€์”ฉ ์žˆ์„ ๋•Œ
    const age = parseInt(prompt("How old are you?"));

    if (isNaN(age)) {
        console.log("Please write a number");
    }else if (age< 18){
        console.log("You are too young");
    }else {
        console.log("You can drink");
    }
  • ๋‘ ๊ฐ€์ง€ ์ด์ƒ์˜ ์กฐ๊ฑด์„ ๋™์‹œ์— ๋งŒ์กฑํ•ด์•ผ ํ•  ๋•Œ
    • AND ๐Ÿ‘‰ && ๋‘˜ ๋‹ค ๋งŒ์กฑ
    • OR ๐Ÿ‘‰ || ๋‘˜ ์ค‘์— ํ•˜๋‚˜๋งŒ ๋งŒ์กฑ
    if (isNaN(age) || age < 0) {     // ์‚ฌ์šฉ์ž๊ฐ€ -์ˆ˜๋ฅผ ์ž…๋ ฅํ•  ๋•Œ ์•Œ๊ณ  ์‹ถ๋‹ค.
    console.log("Please write a real positive number");
    }else if (age< 18){
        console.log("You are too young");
    }else if(age >= 18 && age <=50) {
        console.log("You can drink");
    }else if (age > 50 && age <= 80) {
        console.log("You should exercise");
// ๐Ÿ‘‡ ์ด ์ฝ”๋“œ๊ฐ€ ๋งจ ๋์— ์žˆ์œผ๋ฉด ์œ„์˜ ์กฐ๊ฑด์ด ๋” ์•ž์„œ๊ธฐ ๋•Œ๋ฌธ์— ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค.
    }else if (age === 100) {
        console.log("Wow you are wise")
    }else if (age >80) {
        console.log("You can do whatever you want.")
    }
  • =์™€ ===์™€ !==
    • = : ๊ฐ’์„ ํ• ๋‹น
    • === : ๊ฐ’, ํƒ€์ž…์ด ๊ฐ™๋‹ค
    • !== : ๊ฐ’, ํƒ€์ž…์ด ๋‹ค๋ฅด๋‹ค
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€