Skip to content

Instantly share code, notes, and snippets.

@willsheppard
Last active June 7, 2024 10:05
Show Gist options
  • Save willsheppard/23c092e9188bebf588a037ce1898aa7b to your computer and use it in GitHub Desktop.
Save willsheppard/23c092e9188bebf588a037ce1898aa7b to your computer and use it in GitHub Desktop.
Node arrow functions tutorial

Basic function

function buildTower(height) {
  return "Built a tower " + height + " blocks high";
}

Basic arrow function

const buildTower = (height) => "Built a tower " + height + " blocks high";

More arrow functions

const square =  x  => x * x; // single expression, with implicit return
const square = (x) => x * x; // equivalent to above, with optional parentheses

const squareRoot = x => {
  const result = Math.sqrt(x);
  return result;
}; // multiple lines, with explicit return

const greet = () => "Hello, world!"; // No parameters: parentheses are required
const add = (x, y) => x + y;         // Multiple parameters: parentheses are required
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment