箭头函数可以认为是函数的一种简写定义方式:
let f1 = p => "hello " + p;//定义一个箭头函数,并将其赋值给变量f,函数的参数是p,返回值是"hello " + p
console.log(f1("world"));//输出hello world
let f2 = () => "hello world";//定义没有参数的箭头函数
console.log(f2());//输出hello world
let f3 = (g, w) => g + " " + w;//定义有多个参数的箭头函数
console.log(f3("hello", "world"));//输出hello world
let f4 = (s, p ) =>{//定义函数体有多条语句的箭头函数
if(s == true){
return "hello " + p;
}
else{
return "hi " + p;
}
}
console.log(f4(true, "world"));//输出hello world
console.log(f4(false, "world"));//输出hi world