Loading...

Go Back

Next page
Go Back Course Outline

JavaScript Full Course


Functions in JavaScript

1. Defining Functions

Functions in JavaScript can be defined using function declarations, function expressions, or arrow functions.

1.1 Function Declarations

A function declaration defines a function using the function keyword, followed by the function name, parameters, and the code block.

                        function greet(name) {
                            console.log("Hello, " + name + "!");
                        }
                        
                        greet("Alice");  // Output: Hello, Alice!
                                
Output:
Hello, Alice!

1.2 Function Expressions

A function expression assigns a function to a variable. The function can be anonymous or named.

                        const greet = function(name) {
                            console.log("Hello, " + name + "!");
                        };
                        
                        greet("Bob");  // Output: Hello, Bob!
                                
Output:
Hello, Bob!

1.3 Arrow Functions

Arrow functions provide a concise syntax for defining functions. They don't have their own this context.

                        const greet = (name) => {
                            console.log("Hello, " + name + "!");
                        };
                        
                        greet("Charlie");  // Output: Hello, Charlie!
                                
Output:
Hello, Charlie!

2. Function Parameters and Arguments

Functions can accept inputs known as parameters. When calling a function, the values passed are arguments.

                        function sum(a, b) {
                            return a + b;
                        }
                        
                        console.log(sum(3, 5));  // Output: 8
                                
Output:
8


3. Return Values

Functions can return values using the return statement. Once a value is returned, the function exits.

                        function multiply(x, y) {
                            return x * y;
                        }
                        
                        let result = multiply(4, 6);
                        console.log(result);  // Output: 24
                                
Output:
24

4. Function Scope and Closures

Functions have their own local scope. A closure allows a function to "remember" variables from its outer scope.

4.1 Function Scope

                        let globalVar = "I am global";  // Global variable
                        
                        function testScope() {
                            let localVar = "I am local";  // Local variable
                            console.log(globalVar);  // Can access global variable
                            console.log(localVar);   // Can access local variable
                        }
                        
                        testScope();
                        // console.log(localVar);  // Error: localVar is not defined outside of testScope
                                
Output:
I am global
I am local


4.2 Closures

                        function outer() {
                            let outerVar = "I am from outer function";
                        
                            function inner() {
                                console.log(outerVar);  // Inner function can access outerVar
                            }
                        
                            return inner;
                        }
                        
                        const closureFunction = outer();  // Returns the inner function
                        closureFunction();  // Output: I am from outer function
                                
Output:
I am from outer function

5. Immediately Invoked Function Expressions (IIFEs)

An IIFE is a function that is executed immediately after being defined.

                        (function() {
                            let secret = "I am secret";
                            console.log(secret);
                        })();
                                
Output:
I am secret

6. Recursion

Recursion occurs when a function calls itself. It is used to solve problems that can be broken down into smaller subproblems.

                        function factorial(n) {
                            if (n <= 1) {
                                return 1;
                            }
                            return n * factorial(n - 1);
                        }
                        
                        console.log(factorial(5));  // Output: 120
                                
Output:
120
Go Back

Next page