2.17 Arrow functions, the basics

히진로그·2022년 2월 7일
0

Modern-Javascript

목록 보기
14/14

출처: https://javascript.info/

  • 혼자 읽고 타이핑하며 이해하면서 공부한 흔적 남기기

very simple and concise syntax for creating functions, 종종 함수 표현식 보다 좋다

let func = (arg1, arg2, ..., argN) => expression;

concrete example:

let sum = (a, b) => a + b;

/* This arrow function is a shorter form of :
let sum = function(a, b) {
	return a + b;
};
*/

alert(sum(1,2)); // 3

If we have only one argument, then parentheses around parameters can be omitted, making that even shorter.

인수가 하나라면 인수 옆 괄호들을 생략할 수 있다.

let double = n => n * 2;
alert(double(3)); // 6

If there are no arguments, parentheses will be empty (but they should be present)

만약 인수가 하나도 없다면 괄호는 빈채로 쓴다. (괄호는 꼭 있어야 함)

let sayHi = () => alert("Hello!");
sayHi();

0개의 댓글