JavaScript Functions

A JavaScript function is a block of code designed to perform a specific task.

Functions can be defined and then called to execute the code they contain. They can also take parameters as inputs, and can return values.

Functions are a fundamental concept in JavaScript, and are used to organize and structure code for better readability and reusability.

JavaScript Function Syntax

The syntax for defining a JavaScript function is as follows:

function functionName(parameter1, parameter2, ...) {
  // code to be executed
  • function : is a JavaScript keyword indicating that this is a function definition
  • functionName : is the name of the function. The naming rules for JavaScript variables apply here, so the name can contain letters, digits, underscores, and dollar signs, but it cannot start with a digit.
  • parameter1, parameter2, ... : are the parameters that can be passed to the function when it is called. These are optional, and a function can have any number of parameters, or none at all.

You can call a function by using its name followed by parentheses, like this:


You can also pass arguments to a function when you call it, like this:

functionName(argument1, argument2, ...);

The code inside the curly braces {} is the function body, which is executed when the function is called.

A function can also return a value using the return statement:

function add(a,b){
  return a+b;

In this case the function add takes two parameters a and b and returns their sum.

Adding Parameters to Functions

To add parameters to a function, you need to include them within the parentheses when defining the function. Each parameter is separated by a comma ,. For example, the following function takes two parameters, a and b:

function add(a, b) {
  // code to be executed

You can also define function with no parameter, like this:

function greet(){
  console.log('Hello World!');

Once the function is defined, you can call it and pass values for the parameters when you call the function. These values are known as arguments.

For example:

add(1, 2); // this will call the function add and pass 1 and 2 as the values of the parameters a and b respectively


greet(); // this will call the function greet and will print 'Hello World!'

It is also important to note that while defining a function you can also set default values for the parameters in case no value is passed while calling the function.

function add(a=1, b=2) {
  return a + b;

console.log(add()); // Output: 3

In this case, if no arguments are passed while calling the function add it will take default values of 1 and 2 and return the sum.

Function with Return Value

A function with a return value is a function that returns a value to the code that called it after the function's code has been executed. The returned value can be of any data type, such as a number, a string, an object, etc.

The return statement is used to return a value from a function. Once a return statement is executed, the function stops executing and the returned value is passed back to the code that called the function.

Here is an example of a simple function that takes two numbers as parameters and returns their sum:

function add(a, b) {
  return a + b;

You can call this function and assign the returned value to a variable:

let result = add(1, 2);
console.log(result); // Output: 3

Or you can directly use the returned value in your code:

console.log(add(1, 2) * 2); // Output: 6

It is also important to note that a function can have multiple return statements, but once a return statement is executed, the function stops executing, so any code after a return statement will not be executed.

Also, if a function doesn't have a return statement or if the return statement doesn't have a value, the function will return undefined by default.

function greet(){
  console.log('Hello World!');

console.log(greet()); // Output: 'Hello World!' and undefined

In this case the function greet doesn't have a return statement, so it returns undefined by default.

Functions Used as Variable Values

In JavaScript, functions can be used as variable values. This means that a function can be assigned to a variable and then called using that variable.

Here is an example of a simple function being assigned to a variable:

let add = function(a, b) {
  return a + b;

In this example, the add variable is assigned the value of a function that takes two parameters, a and b, and returns their sum.

Now you can call the function using the variable:

console.log(add(1, 2)); // Output: 3

You can also use arrow function syntax to define a function and assign it to a variable:

let multiply = (a, b) => {
  return a * b;

console.log(multiply(2,3)); // Output: 6

Functions assigned to variables are also known as "anonymous functions" because they don't have a name, and can only be invoked via the variable they are assigned to.

These functions can also be passed as arguments to another function, or be returned as a value from another function, which is a technique called Higher-order function.

let square = (x) => x*x;
let double = (x) => x*2;

let math = (x, func) => func(x);

console.log(math(2,square)); // Output: 4
console.log(math(2,double)); // Output: 4

In this example, the math function takes two arguments: a number and a function. The function passed as a second argument is applied to the number passed as first argument.

Functions as variable values can be useful for creating reusable and modular code, as well as for implementing functional programming techniques in JavaScript.

JS Basics