Javascript Functions Quick References

hoanganhlam

Lam

Posted on December 21, 2023

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

💖 💪 🙅 🚩
hoanganhlam
Lam

Posted on December 21, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related