DEV Community

Lam
Lam

Posted on

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

Top comments (0)