DEV Community

Cover image for "Exploring the World of JavaScript Function Types: An In-Depth Look"
Karthick (k)
Karthick (k)

Posted on

"Exploring the World of JavaScript Function Types: An In-Depth Look"

function expression

The function keyword can be used to define a function inside an expression.
You can also define functions using the function declaration or the arrow syntax.

**Syntax**
const fName = function(params) {
// function body
};

  • fName: Variable storing the function.
  • function(params): Defines the function. Parameters are optional.
  • { // function body }: Contains the logic to execute when the function is called.

Arrow functions in JavaScript

Arrow functions in JavaScript are a concise way to write functions using the => syntax, automatically binding this from the surrounding context.

The following code defines an arrow function that takes two numbers and returns their sum.

Syntax

const functionName = (parameters) => { // function body return result;};

  • const: declares the function as a constant variable.
  • functionName: name of the arrow function.
  • (parameters): inputs to the function.
  • =>: arrow showing it’s an arrow function (replaces function keyword).
  • { ... }: function body where code runs.
  • return result: explicitly returns a value.

Top comments (0)