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

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more