티스토리 뷰

  Function  

1. Function() 의 괄호 안의 매개변수에서 아규먼트를 받아들일 수 있다.

ex)
function sayHello(nameOfPerson) {
console.log(nameOfPerson);
}

sayHello("nico")
sayHello("dal")
sayHello("lynn")

2. 아규먼트는 여러개를 받을 수 있음.

ex)
function sayHello(nameOfPerson , age) {
console.log(nameOfPerson);
}

sayHello("nico" , 12);
sayHello("dal" , 20);
sayHello("lynn" , 23);

3. function의 값은 function '안'에서만 존재한다.

--> 오브젝트 안에서 매개변수가 아규먼트를 받는 방식

ex)
const player = {
name : function(Name) {
console.log("Your Name is " + Name + " 입니다.")
} ,
sayHello : function(Age) {
console.log("Your age is " + Age + " 입니다." )
}
}

player.sayHello(14)
player.name("james")

  function이 data 받는 방법  

1.아규먼트 순서가 중요하다.

function plus( a, b ){

  console.log( a+b ); 

}

plus(5, 10);  ----> 5 =a, 10=b 순서대로 받아들임.

 

2. 괄호안의 매개변수는 function의 body에서만 사용가능하다.

function minusFive(potato){

  console.log(potato - 5);

}

console.log(potato);  --->  not defined

 

 

r e t u r n

쉽게 이해시켜준 댓글을 가져왔다. 

-->return값이 왜 필요하지? 왜 retrun을 써야하는지 잘 모르고 썼는데 이제 조금 이해가 가기 시작한다.

1. console.log는 그림의 떡. 꺼내 먹으려면 return필요...
2. return은 카페나 식당의 테이크아웃 개념인 거 같음
함수에서 만들어서 내보낸 결과값을 함수 밖으로 들고 나가서, 다른데서 그대로 출력하건 다른 거랑 섞어서 비벼먹건 볶아먹건 사용할 수 있게 해줌 (니콜라스가 펄펙 설명이라 했음)
3.리턴을 안하면 믹서기 안에 있는 망고주스를 보고만 있고 마실 수 없다는 예시가 진짜 찰떡표현이다..
ex)
const age = 96;
function calculatorKrAge(ageOfForeigner){
   ageOfForeigner + 2;  //retun이 없으면 undefinded
   return ageOfForeigner + 2; //98이 리턴
}

const krAge = calculatorKrAge(age); //98 
//age를 calculatorKrAge(아규먼트)로 받아와 96+2가 반환(return)됨.

console.log(krAge); //98

return을 쓸 때의 주의사항

한번 return을 하면 해당 함수는 끝난다! --> return 하는 순간, function 종료됨!

그렇기 때문에 return뒤에 작업을 한다면 출력되지 않는다!

const calculator = {
    plus : function(a,b){
    	console.log("hello");  //출력 O
        return a+b;  
    	console.log("byebye");  //출력 X      
    }
 const plusResult = calculator.plus(2, 3); // 5가 리턴됨.

 

  Conditionals  조건문 

   type변경하는 방법   

typeof

parseInt

 

 무언가가 NaN인지 판별하는 방법 

  • isNaN()  --> 첫번째 아규먼트(argument)는 number여야 함.  
  • isNaN( age )   --> age가 NaN인지 아닌지에 따라 true와 false를 리턴함.

* NaN == Not a Number

EX)
const age = parseInt(prompt("몇살이니?"));

if( isNaN( age ){  //true - age가 NaN일 경우
console.log("숫자를 입력하세요");

} else{

console.log("Tank you");

}

 

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/09   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함