๐Ÿ“ŒLanguage/JavaScript

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ํฌ๋กฌ ์•ฑ ๋งŒ๋“ค๊ธฐ - ๋…ธ๋งˆ๋“œ ์ฝ”๋” ํด๋ก ์ฝ”๋”ฉ #3

hellohailie 2022. 4. 7. 18:44

ํ•œ๊ตญ๋‚˜์ด๋ฅผ ๋งŒ๋“œ๋Š” function์„ ๋งŒ๋“ค์ž.

 

* console.log๋Š” console์— ๊ฒฐ๊ณผ๋ฅผ ํ‘œ์‹œํ•ด์ฃผ๋Š”๋ฐ, ๊ถ๊ทน์ ์œผ๋กœ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๊ฑด ๊ฒฐ๊ณผ๋ฅผ ํ™”๋ฉด์— ์ถœ๋ ฅํ•œ๋‹ค๋“ ์ง€, ๋ฌด์–ธ๊ฐ€๋ฅผ ๋ณ€๊ฒฝํ•œ๋‹ค๋“ ์ง€, data๋ฅผ ๋ฐ›์•„์„œ ์‚ฌ์šฉํ•˜๊ธฐ๋ฅผ ์›ํ•œ๋‹ค. 


* ํ•จ์ˆ˜๊ฐ€ ๊ทธ๋ƒฅ ์–ด๋–ค ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ธฐ๋ณด๋‹จ, ์–ด๋–ค ์ผ์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์•Œ๋ ค์ฃผ๋Š” ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์ž.


*function ์•ˆ์—์„œ ๋ฌด์–ธ๊ฐ€๋ฅผ return ํ•˜๋ฉด, function์„ ์‹คํ–‰ํ•  ๋•Œ, ๊ทธ ๊ฒฐ๊ณผ๋ฅผ return ๋‹ค์Œ์˜ ๊ฒƒ์œผ๋กœ ๋Œ€์ฒดํ•œ๋‹ค. 

const age = 100;
function calculaterKrAge (ageOfForeigner){
   return ageOfForeigner + 2;
}
const krAge = calculaterKrAge(age);

console.log(krAge);

102

 

calculaterKrAge๋ฅผ ์‹คํ–‰์‹œํ‚ค๋ฉด, 100์ด๋ผ๋Š” argument๊ฐ€ age์ž๋ฆฌ๋กœ ๋Œ€์ฒด๋œ๋‹ค. 

 

๋”๋ณด๊ธฐ

ageOfForeigner์€ calculateKrAge ํ•จ์ˆ˜์—์„œ ์•„์ง ํ• ๋‹น๋˜์ง€ ์•Š์€ ๋ณ€์ˆ˜๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๊ทธ ๋ณ€์ˆ˜์— const age = 100์œผ๋กœ ๋ฏธ๋ฆฌ ํ• ๋‹นํ•œ ๊ฐ’์„ ๋„ฃ์–ด์ค€ ๊ฒƒ์ด์ง€์š”.

์˜ˆ๋ฅผ ๋“ค์–ด, ์•„๋ž˜์™€ ๊ฐ™์€ ๊ณผ์ •์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

const age = 100;
let ageOfForeigner;
ageOfForeigner = age;

 

console.log(krAge);๋กœ ์ผ๊ธฐ ๋•Œ๋ฌธ์— 102๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค. 

 

 


 

const age = 100;
function calculaterKrAge (ageOfForeigner){
   ageOfForeigner + 2;
   return "hello";
}
const krAge = calculaterKrAge(age);

console.log(krAge);

hello

 

 

**๋ฌด์–ธ๊ฐ€๋ฅผ return ํ•˜๋ฉด, ๊ณ„์‚ฐ์„ ๋‹ด๋‹นํ•˜๋Š” function์˜ ์ฝ”๋“œ๋Š” ๊ทธ function์ด return ํ•˜๋Š” ๊ฐ’๊ณผ ๊ฐ™์•„์งˆ๊ฑฐ์•ผ!

 

์ด function์ด ์ž‘๋™์„ ์™„๋ฃŒํ–ˆ์„๋•Œ, function์„ ํ˜ธ์ถœํ•œ ์ด ์ฝ”๋“œ (calculaterKrAge(age);) ๋Š” function์˜ return ๊ฐ’์ด ๋œ๋‹ค. 

์ฆ‰, krAge ๋Š” calculaterKrAge์˜ ๋ฐ˜ํ™˜๊ฐ’ (return ๊ฐ’)์ด ๋œ๋‹ค. 

 

 

function์„ ์‹คํ–‰ํ•˜๊ณ  return ๊ฐ’์ด ์žˆ๋‹ค๋ฉด function์„ ์‹คํ–‰ํ–ˆ์„๋•Œ,  ๋ฐ˜ํ™˜๊ฐ’(return value)๋ฅผ ์ค€๋‹ค. 

 

 


const calculator = {
    plus: function(a,b){
        return a,b;
    },
    minus: function(a,b){
        return a-b;
    },
    mul: function(a,b){
        return a*b;
    },
    div: function(a,b){
        return a/b;
    },
    power: function(a,b){
        return a**b;
    },
}
const plusResult = calculator.plus(2,3); 
const minusResult = calculator.minus(plusResult,2);
const mulResult = calculator.mul(20,minusResult);
const divResult = calculator.div(mulResult,plusResult);
const powerResult = calculator.power(minusResult,2);
 

์ถœ๋ ฅ๊ฐ’์€ ์—†๋‹ค. console.log() ์•ˆ์ผ์œผ๋‹ˆ๊นŒ!

 

 

returns์„ ์“ฐ๋Š” ๋ชฉ์ ์€ returns๋ฅผ ๋ชฐ๋ž์„๋•Œ๋Š” console.log๋ฅผ ์จ์•ผ๋งŒ ๋ฐ”๊นฅ์—์„œ ๊ฐ’์„ ํ˜ธ์ถœํ•ด์„œ ์“ธ์ˆ˜์žˆ์—ˆ๋Š”๋ฐ

returns๋ฅผ ์“ฐ๋ฉด console.log๋ฅผ ์“ฐ์ง€์•Š๊ณ ๋„ function์˜ ๊ฐ’์„ ๊ณ„์† ๋ฐ”๊นฅ์—์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. 

 

 

ํ•œ๋ฒˆ returnํ•˜๋ฉด function์€ ๋๋‚œ๋‹ค. 

 

 

 


const age = prompt("How old are you?");
console.log(typeof "15", typeof parseInt ("15"));

string – "number"

 

 


prompt๋Š” ์ฐฝ ์—ด์–ด์„œ ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. 

prompt๋Š” ๋‹ตํ•˜๊ธฐ ์ „๊นŒ์ง€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ์˜ ์‹คํ–‰์„ ๋ฉˆ์ถ”๊ฒŒ ํ•œ๋‹ค, CSS๋ฅผ ์ ์šฉ๋ชปํ•œ๋‹ค.
value์˜ type์„ ๋ณด๋ ค๋ฉด typeof๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ์จ์•ผํ•œ๋‹ค.
์ˆซ์ž์ธ์ง€ ์•Œ์•„๋ณด๊ณ  ์‹ถ์„๋•Œ parseInt()๋ฅผ ์“ด๋‹ค.

 

const age = parseInt(prompt("How old are you?"));
console.log(age);

15์“ฐ๋ฉด 15๋‚˜์˜ด

hi์“ฐ๋ฉด NaN๋‚˜์˜ด (not a number)

 


NaN์ธ์ง€ ํŒ๋ณ„ํ•˜๋Š” ๋ฐฉ๋ฒ•: isNaN()

 

const age = parseInt(prompt("How old are you?"));
console.log(isNaN(age));

12 ์ž…๋ ฅํ–ˆ๋”๋‹ˆ False ๋‚˜์˜ด.

 

boolean์œผ๋กœ ์•Œ๋ ค์ค€๋‹ค. (true / false)

true = ์ˆซ์ž๊ฐ€ ์•„๋‹ˆ๋‹ค.

false = ์ˆซ์ž๋‹ค

 


if ์กฐ๊ฑด๋ฌธ

์กฐ๊ฑด๋ฌธ ์‹
if(condition){
/// condition == true
} else {
/// condition == false
}
์—ฌ๊ธฐ์„œ condition์€ boolean์œผ๋กœ ํŒ๋ณ„ ๊ฐ€๋Šฅํ•ด์•ผํ•œ๋‹ค. 

 

 

&& and ๋‘˜ ๋‹ค true์ด๋ฉด true
|| or ๋‘˜ ์ค‘ ํ•˜๋‚˜๋งŒ์ด๋ผ๋„ true์ด๋ฉด true

true || true === true
false || true === true
true || false === true
false || false === false

true && true === true
false && true === false
true && false === false
false && false === false

 


 

๋‚˜์ด๋ณ„ ์Œ์ฃผ ๊ฐ€๋Šฅ ํ”„๋กœ๊ทธ๋žจ

const age = parseInt(prompt("How old are you?"));

if(isNaN(age) || age < 0){
    alert("please write a real positive again.");
}else if (age < 18){
    console.log("You are too young to drink.");
}else if (age >= 18 && age <= 80){
    console.log("You can drink.");
}else if (age === 100){
    console.log("wow!")
}else if (age > 80) {
    console.log("You are too old to drink.");
}

 

**

else if (age === 100) 
์ด ํ•จ์ˆ˜๊ฐ€ ์œ„์น˜๊ฐ€ else if (age > 80) ๋ณด๋‹ค ์œ„์— ์žˆ์–ด์„œ ์ถœ๋ ฅ๋œ๋‹ค. 

 


์ฐธ๊ณ  about ===

JS์—์„œ๋งŒ ์‚ฌ์šฉํ•˜๋Š” ์—ฐ์‚ฐ์ž ===
== ์€ ๊ฐ’๋งŒ์„ ๋น„๊ตํ•˜๊ณ 
=== ์€ ์œ ํ˜•๋„ ๋น„๊ตํ•˜์—ฌ === ๋ฅผ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š”๊ฑธ ์ถ”์ฒœํ•œ๋‹ค๊ณ  ํ•˜๋„ค์š”.
ex)
0 == false ---> true
0 === false ---> false