JavaScript Functions

CodingTute

JavaScript

A function is a block of code that can be executed when it is called. Functions are often used to encapsulate reusable pieces of code, to separate concerns within a program, and to abstract away complexity.

Here is an example of a simple function in JavaScript:

function sayHello() {
  console.log('Hello, world!');
}

sayHello();  // outputs 'Hello, world!'

In this example, the sayHello function is defined using the function keyword, followed by the function name and a set of parentheses. The code inside the curly braces is the body of the function, which is executed when the function is called.

To call a function in JavaScript, you simply use its name followed by a set of parentheses. In the example above, the sayHello function is called by using its name followed by an empty set of parentheses.

Functions can also accept parameters, which are values that are passed to the function when it is called. These parameters can be used inside the function to perform a specific task. Here is an example of a function with a parameter:

function sayHello(name) {
  console.log(`Hello, ${name}!`);
}

sayHello('Alice');  // outputs 'Hello, Alice!'
sayHello('Bob');  // outputs 'Hello, Bob!'

In this example, the sayHello function has a single parameter, name. When the function is called, the value of the name parameter is used to customize the output of the function.

Functions can also return a value, which is the result of the function’s execution. The return value can be used by the code that calls the function. Here is an example of a function that returns a value:

function add(x, y) {
  return x + y;
}

let result = add(2, 3);  // result is 5

In this example, the add function has two parameters, x and y, and it returns the result of x + y.

Functions with Default parameters

you can assign default values to function parameters using the = operator. This allows you to specify a default value for a parameter in case the caller does not provide a value for that parameter.

Here is an example of a function with default parameters:

function sayHello(name = 'world') {
  console.log(`Hello, ${name}!`);
}

sayHello();  // outputs 'Hello, world!'
sayHello('Alice');  // outputs 'Hello, Alice!'

In this example, the sayHello function has a single parameter, name, with a default value of 'world'. If the caller does not provide a value for the name parameter, the default value is used.

You can specify default values for multiple parameters in a function. In this case, the default values are used for any parameters that are not provided by the caller. Here is an example:

function add(x = 0, y = 0) {
  return x + y;
}

console.log(add(2, 3));  // outputs 5
console.log(add(2));  // outputs 2
console.log(add());  // outputs 0

In the above JavaScript snippet, a function called add that takes two parameters, x and y. Both x and y have default values of 0, which means that if the function is called without any arguments or with only one argument, the missing arguments will be set to 0. The function returns the sum of x and y.

The function is being called with different sets of arguments in the three console.log statements. In the first call, add(2, 3), both x and y are being passed arguments, so the function returns 2 + 3, which is 5.

In the second call, add(2), only x is being passed an argument, so y is set to its default value of 0. The function returns 2 + 0, which is 2.

In the third call, add(), neither x nor y are being passed arguments, so both are set to their default values of 0. The function returns 0 + 0, which is 0.

You can find the complete JavaScript Tutorials here.

Follow us on Facebook, YouTube, Instagram, and Twitter for more exciting content and the latest updates.

Exit mobile version