JavaScript Functions

In javascript, functions are reusable blocks of code that can be invoked by their names. They are a fundamental part of the language and can be assigned to variables, passed as arguments to other functions, and returned as values from other functions. Here's an overview of how functions work in javascript:


1. **FUNCTION DECLARATION:**

- The most common way to create a function is by using a function declaration. It has the following syntax:

javascript
function functionname(parameter1, parameter2) {
    // function body
    // code to be executed
    // optional return statement
}

- the function declaration begins with the `function` keyword, followed by the name of the function. It can take zero or more parameters (also called arguments) enclosed in parentheses. The function body contains the code to be executed when the function is called.


2. **FUNCTION EXPRESSION:**

- Functions can also be created using function expressions, where a function is assigned to a variable. The syntax is as follows:

javascript
var functionname = function(parameter1, parameter2) {
    // function body
};

- in this case, the function is assigned to the variable `functionname`. Function expressions can be anonymous (no name) or named.


3. **ARROW FUNCTIONS (ES6+):**

- Arrow functions provide a more concise syntax for creating functions, introduced in ecmascript 6 (es6). They have implicit return and lexical `this` binding. The syntax is as follows:

javascript
var functionname = (parameter1, parameter2) => {
    // function body
};

- arrow functions are often used for shorter function expressions or as callbacks.


4. **FUNCTION INVOCATION:**

- To execute a function and run its code, you need to invoke or call it. This is done by using the function name followed by parentheses, optionally passing arguments if the function expects any.

javascript
functionname(argument1, argument2);

- the arguments provided during the function call will be assigned to the parameters defined in the function declaration or expression.


5. **RETURN STATEMENT:**

- Functions can return a value using the `return` statement. It specifies the value to be returned from the function back to the caller.
- if a `return` statement is not explicitly used or no value is specified, the function will return `undefined` by default.


6. **FUNCTION SCOPE:**

- Javascript has function scope, which means variables declared inside a function are only accessible within that function (unless they are explicitly made global).
- however, variables declared outside of any function (global scope) are accessible from anywhere in the code.


7. **BUILT-IN FUNCTIONS:**

- Javascript also provides a wide range of built-in functions, such as `console.log()`, `settimeout()`, `math.random()`, `array.prototype.map()`, etc., that perform specific tasks and can be used directly without needing to define them.


These are the basic concepts of working with functions in javascript. Functions play a crucial role in organizing and reusing code, as well as implementing complex logic in javascript applications.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext