0% found this document useful (0 votes)
8 views2 pages

JavaScript Function Types Explained

The document outlines various ways to define functions in JavaScript, including function declarations, expressions, and arrow functions. It also covers default parameters, functions without parameters, calling one function from another, and Immediately Invoked Function Expressions (IIFE). Each method is illustrated with code examples demonstrating their usage and behavior.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

JavaScript Function Types Explained

The document outlines various ways to define functions in JavaScript, including function declarations, expressions, and arrow functions. It also covers default parameters, functions without parameters, calling one function from another, and Immediately Invoked Function Expressions (IIFE). Each method is illustrated with code examples demonstrating their usage and behavior.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Functions

============

1. Function Declaration
--------------------------

A standard way to define a function.

// Function declaration
function greet(name) {
return "Hello, " + name + "!";
}

// Calling the function


[Link](greet("Hareesh")); // Output: Hello, Hareesh!

function greet(name) → defines a function called greet with a parameter name.

return → sends the result back to where the function was called.

2. Function Expression
------------------------

Functions can also be stored in variables.

// Function expression
const square = function(num) {
return num * num;
};

[Link](square(5)); // Output: 25

Function expressions are not hoisted like function declarations.

Useful for passing functions as arguments.

3. Arrow Function (ES6)


------------------------

A shorter syntax for writing functions.

// Arrow function
const add = (a, b) => a + b;

[Link](add(10, 20)); // Output: 30

If the function has a single expression, it returns the result automatically.

For multiple statements:

const multiply = (a, b) => {


const result = a * b;
return result;
};

[Link](multiply(4, 5)); // Output: 20


4. Function with Default Parameters
-------------------------------------

You can set default values for parameters.

function greetUser(name = "Guest") {


return "Welcome, " + name + "!";
}

[Link](greetUser()); // Output: Welcome, Guest!


[Link](greetUser("Hareesh")); // Output: Welcome, Hareesh!

5. Function without Parameters


-------------------------------
function sayHello() {
[Link]("Hello World!");
}

sayHello(); // Output: Hello World!

6. Function Calling Another Function


-------------------------------------
function add(a, b) {
return a + b;
}

function squareSum(x, y) {
const sum = add(x, y);
return sum * sum;
}

[Link](squareSum(2, 3)); // Output: 25

7. Immediately Invoked Function Expression (IIFE)


---------------------------------------------------

Function that runs immediately after definition.

(function() {
[Link]("IIFE executed!");
})();

Useful for avoiding global variables.

You might also like