DEV Community

Lam
Lam

Posted on

1

Javascript Functions Quick References

JavaScript Functions Cheat Sheet

Function Declaration

Syntax

function functionName(parameters) {
  // Function body
  return result;
}
Enter fullscreen mode Exit fullscreen mode

Example

function greet(name) {
  return `Hello, ${name}!`;
}

console.log(greet('World'));
Enter fullscreen mode Exit fullscreen mode

Function Expression

Syntax

const functionName = function(parameters) {
  // Function body
  return result;
};
Enter fullscreen mode Exit fullscreen mode

Example

const greet = function(name) {
  return `Hello, ${name}!`;
};

console.log(greet('World'));
Enter fullscreen mode Exit fullscreen mode

Arrow Function

Syntax

const functionName = (parameters) => {
  // Function body
  return result;
};
Enter fullscreen mode Exit fullscreen mode

Example

const greet = (name) => `Hello, ${name}!`;

console.log(greet('World'));
Enter fullscreen mode Exit fullscreen mode

Immediately Invoked Function Expression (IIFE)

Syntax

(function() {
  // Function body
})();
Enter fullscreen mode Exit fullscreen mode

Example

(function() {
  console.log('IIFE executed');
})();
Enter fullscreen mode Exit fullscreen mode

Function Parameters and Arguments

Example

function addNumbers(a, b) {
  return a + b;
}

console.log(addNumbers(5, 10)); // Output: 15
Enter fullscreen mode Exit fullscreen mode

More: Javascript Cheat Sheet

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs