Javascript Functions

Declaring Functions:

You can declare functions using the function keyword.

                                      
                                        function functionName(parameters) {
                                            // Code to be executed
                                        }                                                                                                                                                                                                                                                                            
                                      
                                    

Example:

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

Runtime error: The name parameter was not provided, so greet function doesn't log a greeting.


Parameters:

Functions may have parameters, which simply serve for the values being passed to the function when it is called.

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

Here, add is a function that takes two parameters (a and b) and returns their sum.


Return Statements:

The return statement is used to specify the value that a function should return.

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

In this example, multiply returns the product of x and y.


Function Expressions:

Functions can also be assigned to variables, creating what is known as a function expression.

                                  
                                    let sayHello = function(name) {
                                        console.log("Hello, " + name + "!");
                                    };
                                    
                                    sayHello("Jane"); // Output: Hello, Jane!                                    
                                  
                                

In this example, sayHello is a function expression assigned to the variable sayHello.


Arrow Functions (ES6+):

Brief syntax of Arrow functions for representation of functions, which could be used for slight and simple operations (one-liners).

                                  
                                    let square = (x) => x * x;

console.log(square(3)); // Output: 9                                    
                                  
                                

Here, square is an arrow function that takes a parameter x and returns its square.