profileRyan KesPGP keyI build stuffEmailGithubTwitterLast.fmMastodonMatrix

JavaScript arrow functions

Introduction

const power = (base, exponent) => {
  let result = 1
  for (let count = 0; count < exponent; count++) {
    result *= base
  }
  return result
}

console.log(power(2, 3))

If there is only one parameter name, parentheses can be omitted around the parameter list. Same for the function body and brackets

const square1 = (x) => {
  return x * x
}
const square2 = (x) => x * x

console.log(square1(5))
console.log(square1(6))

When an arrow function has no parameters at all, its parameter is just an empty set of parentheses

const horn = () => {
  console.log("Toot")
}

horn()