To write simple way as “Arrow Function”

sunghoon·2024년 12월 30일
0

2.0 Glove Project

목록 보기
6/35
post-thumbnail

2.17 Arrow function, the basics - link

// arrow function
let func = (arg1, arg2, ..., argN) => expression;
// 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
  • only on argument, parentheses can be omitted
let double = n => n * 2;

alert( double(3) );
  • it must be presented parentheses when there are no arguments.
let sayHi = () => alert("Hello");

sayHi();
// to dynamically create a function
let age = prompt();

let welcome = (age < 18) ?
	() => alert() :
	() => alert();
	
welcome();

Mutiline arrow functions

// mutiple expressions and statements

let sum = (a, b) => {  
  let result = a + b;
  // if we use curly braces, then we need an explicit "return"
  return result; 
};

alert( sum(1, 2) ); // 3
profile
프라다 신은 빈지노와 쿠페를 타는 꿈을 꿨다.

0개의 댓글