ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [NodeJS] Arrow Function
    NodeJS/Basic 2022. 9. 14. 17:25
    // function 함수이름(매개변수){} -> 화살표를 이용한 표현방법으로 사용
    
    // 함수의 표현방법 1
    function add1(x, y){
        return x+y;
    }
    console.log(add1(10, 20));
    
    // 함수의 표현방법 2
    let add2 = function(x, y){
        return x+y;
    }
    console.log(add2(10, 20));
     
    // 함수의 표현방법 3-1 (화살표 함수)
    // const add3 = function(x, y){return x+y;}
    const add3 = (x, y)=>{
        return x+y;
    } // 익명함수(x,y)=>{return x+y;}가 add3에 저장
    console.log(add3(10, 20));
    
    // 함수의 표현방법 3-2 
    const add4 = (x, y)=> x+y;
    console.log(add4(20, 30));
    // 함수의 몸체가 단순하게 매개변수들의 연산의 결과들을 return하는 명령만 있을 때 사용
    
    // 함수의 표현방법 3-3 
    const add5 = (x, y)=> (x+y);
    console.log(add5(20, 30));
    
    // 함수의 표현방법 3-4 (매개변수 하나)
    function not1(x){
        return !x;
    }
    console.log(not1(true));
    const not2=x=>!x; // const not2=(x)=>!x; 같은 표현
    console.log(not2(false));
     
    const func1 = () => {
        console.log('매개변수 없고 리턴값 없는 함수');
    }
    
    const func2 = (x,y) => {
        console.log(`매개변수(${x}, ${y}) 리턴값 없는 함수`);
    }
    func2(10,20);
    
    const func3 = (x,y) => {
        console.log(`매개변수(${x}, ${y}) 리턴값 있는 함수`);
        return x+y;
    }
    console.log('리턴값 '+func3(10,20));
    
    const func4 = () => {
        console.log(`매개변수 없고 리턴값 있는 함수`);
        return 100;
    }
    console.log('리턴값 '+func4(10,20));
    
    // 매개변수 상관없이 단순 리턴값만 있는 함수: {}가 없는 함수
    const func5 = (x,y) => x+y;
    // const func5 = (x,y) => (x+y);
    // 이름변수 = 매개변수 => 함수의 몸체

    'NodeJS > Basic' 카테고리의 다른 글

    [NodeJS] Promise, Await  (0) 2022.09.14
    [NodeJS] Array  (0) 2022.09.14
    [NodeJS] Object  (0) 2022.09.14
    [NodeJS] var, const, let, template string  (0) 2022.09.14
    [NodeJS] 설치, 콘솔실행  (0) 2022.09.14

    댓글

Designed by Tistory.