DEV Community

Deva I
Deva I

Posted on

Function in Javascript

Definition of Function:

✓ Some activity or set of instructions with name called function.

✓ In JavaScript, a function is a reusable block of code designed to perform a specific task. Functions are executed when they are "invoked" or called.

1. Function Declaration:

✓ Most common way to define a function using the function keyword.

Example:

Parameters: name is a placeholder for the value you pass into the function.

Arguments: "Deva" is the actual value (argument) passed during the call.

Return Statement: The return keyword stops function execution and sends a value back to the caller.

Function Expression:

A function can also be defined inside an expression and assigned to a variable.


Key Concepts:

Reusability: You can call the same function multiple times with different arguments to get different results.

Scope: Variables defined inside a function are "local" and cannot be accessed from outside that function.

Anonymous Functions: These are functions without a name, often used as "callbacks" (functions passed as arguments to other functions).

Built-in Functions: JavaScript includes pre-defined functions that you can use immediately like console.log(), alert(), and Math.sqrt(). (To be discussed)

Reference:https://www.geeksforgeeks.org/javascript/functions-in-javascript/

Top comments (0)