Functions in JS. Code examples open

Functions in JS. Code examples

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators

Functions are a set of instructions that perform a specific action or calculate a specific value.

    function function_name([parameters [, ...]]) {

        //Action
    }

Let’s consider an example:

    function hello() {//function name hello, () - means that the function has no parameters

        console.log("Hello");//will output Hello to the console

    }

The function will not be executed until called. For example, let’s call the function we created:

    hello();//function call

If parameters need to be passed to the function when called, add them in square brackets:

    hello('Text', 22)

For example, let’s create a simple function with parameters:

    function print(message) {//the print function will take the message parameter
        console.log(message);//and will output this parameter to the console
    }

    print("Hello JavaScript");//call the print function and pass the string "Hello Javascript" to it, when executed, the function interprets "Hello Javascript" into the message parameter, after that it will execute the body of the function and output this string to the console.
When calling the print() function we do not pass any parameter to it, then this parameter will be equal to underfined

If a function takes multiple parameters, they are listed separated by commas:

    print("Hello JavaScript", 200) 

When calling a function with multiple parameters, the values are passed to the parameters by position. That is, the first value is passed to the first parameter, the second value is passed to the second, and so on.

To avoid the underfined value in the function, we can set the default value:

    function sum(x = 8, y = 10 + x) {

        const z = x + y;
        console.log(z);
		
    }
	
    sum();      // 26
    sum(6);     // 22
    sum(6, 4) 

To get all the parameters passed to the function, you can use the arguments array:

With the (ellipsis) operator, we can indicate that a variable number can be passed using a parameter values:

    function display(season, ...temps) {
        console.log(season);
        for (index in temps) {
            console.log(temps[index]);
        }
    }
    display(-2, -3, 4, 2, 5);
    display(20, 23, 31);
    function sum() {
        let result = 0;
        for (const n of arguments)
            result += n;
        console.log(result);
    }

    sum(6);             // 6
    sum(6, 4)           // 10
    sum(6, 4, 5)

A distinctive feature of functions is that they can be called multiple times in different places in the program:

    function hello() {
        console.log("Hello");
    }
    hello();
    hello();
    hello();

Variables and constants as functions

Variables and constants can be assigned a function:

function hello() {
	console.log("Hello");
}

const hellotext = hello;//assign the hello function to the constant

hellotext();//call the hello function

Anonymous functions

It is not necessary to give functions a specific name. You can use anonymous functions:

let message = function () {
  console.log("Hello JavaScript");
}
message();

An anonymous function in JavaScript is a function that has no name and is typically used as an argument to another function or to create closures. It can be defined and called without using the function name.

A function as a parameter of another function

Consider an example:

    function sum(x, y) {//let's create a function that will find the sum of two values
        return x + y;
    }

    function minus(x, y) {//let's create a function that will subtract the values of two variables
        return x - y;
    }

    function operation(x, y, func) {//let's create a function whose third parameter will receive a function

        const result = func(x, y);//let's write a function in the constant
        console.log(result);//output the result to the console
    }

    console.log("Sum");
    operation(10, 6, sum);  // 16

    console.log("Minus");
    operation(10, 6, minus);

We can define various functions (e.g. sum and subtract functions in this case) and pass them to the function call operation.

The result of the function

A function can either return the result of its work or not return anything. In order for the function to return a certain value, you need to use the return operator:

function sum(a, b) {
  	const result = a + b;
    return result;
}
let number = sum(2, 4);//now the result of the sum function will be recorded in the number variable

One function can return another function:

    function menu(n) {

        if (n == 1) return function (x, y) { return x + y; }

        else if (n == 2) return function (x, y) { return x - y; }

        else if (n == 3) return function (x, y) { return x * y; }

        return function () { return 0; }
    }

    const action = menu(1);
    const result = action(2, 5);
    console.log(result);

0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation