Tech Study

JavaScript Function | Functions in JavaScript

Functions in JavaScript

Functions are “self-contained” modules of code that perform a specific task. Functions generally “take in” data, process it, and “return” an output. Once a function is authored, it can be used over and over and over again. Functions can be “called” from within other functions. Now let us discuss what is a function in JavaScript.

JavaScript provides functions parallel to most scripting and programming languages. In JavaScript, a function allows you to define a block of code, give it a name and also execute it as several times as you want. A JavaScript function can be traced using the function keyword. The syntax for the function JavaScript is the following illustration shows how to define and call a function in JavaScript.

//defining a function

Function <function-name> ()

{

// code to be executed

};

 

//calling a function

<function-name> ();

 A function can have one or additional parameters, which will be outfitted by the calling code and can be used inside a function. JavaScript is a dynamic type scripting language, therefore a function parameter can have a worth of any data type. One can pass minimal or further arguments while calling a function. If one passes minimal arguments either the rest of the parameters will be undefined. If you pass further arguments also added arguments will be ignored.

All the functions in JavaScript can use an argument object by default. An arguments object includes the value of each parameter. The arguments object is an array-like object. You can access its valuations using an index analogous to an array. Yet, it doesn’t support array styles. An argument object is still valid truly if the function doesn’t include any parameters. An argument object can be repeated using for loop. A function JavaScript can return zero or one value using the return keyword. A JavaScript function can return another function.

JavaScript allows us to assign a function to a variable and additionally use that variable as a function. It’s called function expression. JavaScript allows us to define a function without any title. This unnamed function JavaScript is called the anonymous function. The anonymous function JavaScript must be assigned to a variable.

In JavaScript, a function can have one or further internal functions. These nested functions are in the range of the external function. The internal JavaScript function can access the variables and parameters of the external JavaScript function. Nevertheless, the external function cannot access variables defined inside internal functions.

JavaScript function allows you to define a block of code, give it a name and again execute it as multiple times as you want. A function can be outlined using the function keyword and can be applied using a () operator. A function can include one or further parameters. It’s elective to specify function parameter values while executing it. JavaScript is a loosely- typed language. A function parameter can hold the worth of any data type. You can specify minimal or further arguments while calling the function JavaScript. All the functions can access arguments object by default rather than parameter names. A function can return a literal worth or another function. A function can be commissioned to a variable with another name. JavaScript allows you to make anonymous functions that must be assigned to a variable.

Syntax:

function function_name(parameter1, parameter2, parameter3)

{

// executable code

// can reference parameters and other variables

return result; 

}
  •      function keyword: 

This keyword is used to define a new function.

  • function_name: 

The name of the function.It should be descriptive and start with a lowercase letter.

  • Parameter1, Parameter2, Parameter3: 

The input parameters of the function. Optional, but if optional, must be enclosed in parentheses and separated by commas.

Example:

let person = {

name: "Nisha",

sayHello: function() 

{

console.log("Hello, my name is " + this.name + ".");

}

};

person. sayHello();

Output:

Hello, my name is Nisha.

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more