[inflearn] javascript: 함수

eve·2023년 2월 6일
0

frontend

목록 보기
22/40

1. 기본 작성법

원의 너비를 구하는 함수를 선언해보자.

<script>
  function circleArea(r){
    let Area = r*r*3.14;
    return width;
  }
  document.write(circleArea(10));
</script>
	-> 314




2. 지역변수와 전역변수

function sum(x){
  let y = 50;
  return x + y;
}

document.write(sum(10);
document.write('<br>');
document.write(x, y);
  • 스니펫에서 x는 매개변수 (parameter)이면서 지역변수(Local val)이다.
  • sum함수에 들어간 10은 전달인자, 즉 argument이다.
  • x, y는 sum이라는 함수 내에서 선언된 지역함수이므로, sum 함수를 벗어나서는 사용할 수 없다.




3. arrow function

function sum(x, y){
  return x + y;
}

위의 함수에 arrow function을 활용하면 다음과 같다.

let sumArrowFunction = (x, y) => x + y;

동일한 기능을 가진 함수를 한 줄로 작성할 수 있는 기능이다.

profile
유저가 왜 그랬을까

0개의 댓글