[TIL #7 WECODE] ES6-arrow function

Whoyoung90·2021년 2월 23일
0
post-thumbnail

210221 WECODE #7

ES6 = ECMA Script 2015

ECMA Script는 JavaScript를 표준화한 것을 말한다.

function(){}
() => {}

<script>
function getname(){}
const getname = () => {}
const getname = function(){}
</script>
<script>
function getname(name){}
const getname = (name) => {}
const getname = name => {} 
const getname = (name, age) => {} 
</script>
  • 인자가 하나일 때는 소괄호 생략이 가능.
  • 인자가 두 개일 때는 생략할 수 없다.
<script>
function hi(text) {
  text += '하세요';
  return text;
}
const hi = text => {
  text += '하세요';
  return text;
}
</script>
<script>
function getName(name) {
  return name;
}
const getName = name => { return name; }
const getName = name => name; 
</script>
  • 함수가 실행내용이 딱히 없이 return만 한다면 return 키워드와 중괄호가 생략이 가능하다.
  • 중괄호와 return문이 생략될 경우, 화살표 오른쪽에는 리턴될 "값"만 쓰여야 한다. 다른 코드가 들어가면 안됨!
<script>
function getFullName(first, family) {
  return first + family;
}
const getFullName = (first, family) => { return first + family };
const getFullName = (first, family) => first + family;
</script>
profile
비전공으로 일식 쉐프가 되었듯, 배움에 겸손한 프론트엔드 개발자가 되겠습니다 :)

0개의 댓글