Skip to content
Home » JavaScript functions

JavaScript functions

JavaScript functions are an essential aspect of programming, allowing developers to encapsulate reusable blocks of code and enhance the modularity and readability of their applications.
This cheat sheet aims to provide a comprehensive tabular format to quickly reference and understand the key concepts and syntax related to JavaScript functions.
No.TopicDescription and SyntaxExample
1.Function DeclarationDeclares a named function that can be called later.
Syntax: function functionName(parameters) { … };
function greet(name) {
return “Hello, ” + name;

Input: greet(“John”);
Output: “Hello, John”
2.Invoking a FunctionCalls a function, passing any required arguments.
Syntax: functionName(arguments);
function sayHello() { console.log(“Hello!”);

Input: sayHello();
Output: Prints “Hello!” to the console
3.Return StatementSpecifies the value to be returned by a function.
Syntax: return value;
function sum(a, b) {
return a + b;

Input: sum(2, 3);
Output: 5
4.Anonymous FunctionsFunctions without a specified name, are commonly used for callback functions or IIFE.
5.Function ExpressionAssigns an anonymous function to a variable.
Syntax: const functionName = function(parameters) { … };
const multiply = function(a, b) {
return a * b;

Input: multiply(2, 3);
Output: 6
6.Named Function ExpressionsAssigns a name to a function expression, useful for self-reference or debugging.
Syntax: const functionName = function name() { … };
const factorial = function fact(n) { … }

Input: factorial(5);
Output: Calculates the factorial of 5
7.Arrow FunctionA concise syntax for function expressions using the arrow (=>) notation.
Syntax: const functionName = (parameters) => { … };
const square = (num) => num * num;

Input: square(4);
Output: 16
8.Default ParametersAssigns default values to parameters if no argument is provided.
Syntax: (param1 = defaultValue, param2) => { … };
const greet = (name = “Stranger”) => “Hello, ” + name;

Input: greet();
Output: “Hello, Stranger”
9.Rest ParametersCaptures multiple arguments into an array-like structure using the rest operator (…).
Syntax: (param1, …rest) => { … };
const sum = (first, …rest) => first + rest.reduce((acc, cur) => acc + cur, 0);

Input: sum(1, 2, 3, 4);
Output: 10
10.Spread SyntaxExpands an array into individual elements, suitable for function calls or array literals.
Syntax: functionName(…array);
const numbers = [1, 2, 3];
Input: Math.max(…numbers);
Output: 3
11.First-Class FunctionsFunctions are treated as first-class citizens, allowing them to be assigned to variables and passed as arguments or returned from other functions.
12.Higher-Order FunctionsFunctions that can accept other functions as arguments or return them.
Syntax: const higherOrder = () => { return () => { … } }
const withLogging = (func) => { … };

Input: const loggedFunc = withLogging(someFunction);
Output: Function someFunction wrapped with logging capabilities.
13.Callback FunctionsPasses a function as an argument to another function, allowing it to be called back later.
Syntax: functionName(callback);
function fetchData(callback) { … }
Input: fetchData(displayData);
Output: Invokes the `displayData` function.
14.Pure FunctionsFunctions that always produce the same output for the same input, without any side effects.
15.Immediately Invoked Function Expressions (IIFE)Executes a function immediately after its declaration.
Syntax: (function() { … })()
(function() { console.log(“Hello!”); })()
Output: Prints “Hello!” to the console
16.Function ScopeVariables declared within a function are only accessible inside that function’s scope.console.log(carName);
// Output: carName is not defined.

function myFunction() {
  let carName = “Volvo”;
  // local variable hence can use.

// Output: carName is not defined.

Explanation: carName is declared under the local scope of the function hence can not access it outside of the function.
17.Lexical ScopeFunctions can access variables defined in their outer (enclosing) scope.
18.ClosuresA combination of a function and its surrounding state (lexical environment).
19.RecursionA function that calls itself, useful for solving problems that can be broken down into smaller sub-problems.function sum(number) {
if ( number === 0 ) {
return 0;
return number + sum(number – 1);
20.Function ChainingInvoking multiple methods on the return value of a function to achieve a concise and chained syntax.
Syntax: functionName().method1().method2()...
Performs a series of operations on an array.

Conclusion: JavaScript functions are the building blocks of a robust and modular codebase. By understanding the syntax and concepts presented in this tabular cheat sheet, you can leverage the power of JavaScript functions to create efficient and scalable applications. Refer to the examples provided to see how each concept can be applied in practice.

Remember, practice makes perfect, so don’t hesitate to dive into coding exercises and real-world projects to solidify your understanding and master the art of working with JavaScript functions.

Happy coding!

Visit Techtalkbook to find more related topics.


Leave a Reply

Your email address will not be published. Required fields are marked *