Functions in JavaScript

Function in JavaScipt

A function in JavaScipt is a block of code that performs a specific task inside a program. Function expressions are shorter blocks of code that can be used as inputs for functions or to perform specific tasks on their own. This concept is much easier to understand when presented as an example.

Each function in javascript has its own dedicated set of variables and methods. First, let’s discuss what a variable is in JavaScript. A variable is a name given to a storage location in a program. It can hold information while the program executes and permits the program’s code to be modified during runtime. Variables are toured in memory spaces on your computer’s hard drive and are subjectively infinite in number.

An example of a variable in Javascript is ‘name’ where ‘name’ can hold any value you assign it. You can also create arrays and objects as part of your programming language; these are also known as data structures and are used for storing data or performing calculations on that data.

Functions can be used to perform a specific task in sequence or to act as a black box for performing operations on input data. JavaScript code is executed by sending commands from the browser to the server. this is known as an asynchronous event flow and allows multiple functions to execute without interference from other processes running in the browser at the same time.

Each function in JavaScript has an enclosing context that defines all the variables accessible within that scope when that function executes, this makes it easy for multiple functions to share allocated memory space without unintentional conflict resolution wars over scarce memory resources. Variables defined within an enclosing context are known as outer variables because they’re accessible from anywhere within that context but aren’t directly accessible from outside that context.

Declaring a Function

  • ‘function’ keyword is used to declare a function in JavaScript.
  • Like naming a variable, naming a function follows some fundamental guidelines. It is preferable to give your function a name that is descriptive. You could call the function ‘add’ or ‘addNumbers’, for instance, if it is used to add two numbers.
  • The body of the function is contained in ‘{}’.
  • Syntax to declare a function is:
function nameOfFunction () {
    // function body   
}

For example,

// declaring a function
function greet() {
    console.log("Hello!!");
}

Calling a Function

In the above example, we have created a function greet(). In order to execute or use it we need to call it.

We can call a function by the following method:

// function call
greet();

Function Return

The value from a function call can be returned using the return statement.

The function has concluded, as indicated by the return statement. After the return, no further code is run.

The function returns an undefined value if nothing is returned.

Example:

// program to add two numbers
// declaring a function
function add(a, b) {
    return a + b;
}

// take input from the user
let number1 = parseFloat(prompt("Enter first number: "));
let number2 = parseFloat(prompt("Enter second number: "));

// calling function
let result = add(number1,number2);

// display the result
console.log("The sum is " + result);

Output:

Enter first number: 3.4
Enter second number: 4
The sum is 7.4

Benefits of Using a Function

  • The code is made reusable by function. It can be declared once and used repeatedly.
  • The program is made simpler by function because each small task is separated into one.
  • The function makes text easier to read.

Function Expressions

We can also define functions as expressions in JavaScipt. For example,

// program to find the square of a number
// function is declared inside the variable
let x = function (num) { return num * num };
console.log(x(4));

// can be used as variable value for other variables
let y = x(3);
console.log(y);

Output

16
9

That’s all about the Function in JavaScript.

You may like Switch Statement in JavaScript.

Hope this article will guide you to recognize the Function in JavaScript that you needed and still if you have any problems or queries regarding this, post them in the comments section and we will be glad to assist you.

Leave a Reply