Functions

javascript dev.to

FUNCTIONS:

=>Function is a block of code that executes the code when the function (function name) is called.

=>Functions are reusable code blocks designed for particular tasks.

Why Use Functions?

Functions help you to:

  1. Reuse code (run many times).
  2. Organize code into smaller parts.
  3. Make code easier to read and maintain.

To run a function, you call it by using its name followed by parentheses like sayHello():

Ref: W3 School.

Function Scope:

=> The functions that are executed within the {}( brackets)

The return Statement:

  1. When a function reaches a return statement, the function stops executing.

  2. The value after the return keyword is sent back to the caller.

Example:

Parameters and Arguments
In JavaScript, function parameters and arguments are distinct concepts:

  1. Parame****ters are the names listed in the function definition.
  2. Arguments are the real values passed to, and received by the function.

Ref: W3 School

The argument 4 is assigned to the parameter a.
The argument 5 is assigned to the parameter b.

Arrow Function Syntax

  • Arrow function symbol (=>)
  • We can remove the word function, the curly brackets and the return keyword.

Function to compute the product of a and b:

After useing Arrow function:

Source: dev.to

arrow_back Back to Tutorials