Functions in JavaScript — Part 1

Sunilkathuria
In Computing World
Published in
5 min readFeb 3, 2024

A function is a block of code that performs a task. A function is run/called whenever the task is required. It is a building block of any programming language. JavaScript has several built-in functions and allows the creation of user-defined functions.

This task could be a specific task or a set of tasks as one unit. Each function has a name. Whenever this task is required, the corresponding function is run to complete the task.

In most programming languages, a function has the following structure.

Name: This name identifies the unit of code. Generally, it is a meaningful name that explains the purpose of this function.

Parameters / Arguments: Each function optionally accepts additional data required to perform its task.

Function Body: The code block executes when a function is used.

Return value: Each function, optionally, can return a result of its operation.

The following code snippet explains the structure of a function in JavaScript.

Working with Functions

Declaring a function

Use the keyword function to define a new function, followed by a function name. Right after the function name is parentheses (), to mention optional parameters, and finally, the curly brackets {} define the body of a function.

In the above code snippet, function firstFunction() is defined. This function accepts no arguments. The function body has only one statement of console.log.

To run/execute/call the function, use the function name with parentheses.

When a function is defined, it is created as an object.

Function names in JavaScript are case-sensitive. firstFunction is not the same as FirstFunction or firstfunction.

While declaring a function, use the camel case notation.

Parameters of an argument

At the time of function declaration that accepts values, these are called parameters. When a function is called at that time, the passed values are called arguments.

Both words, in general, are used interchangeably.

Function parameters

A function can have an optional parameter(s). Following are some rules to take care of while defining a function.

Parameter Mapping

When a function accepts two or more parameters, mapping the parameters with the values passed (arguments) happens from left to right.

Output

Note: JavaScript does not raise an error when a function that accepts 1 or more parameters is called with Zero or less arguments. All the missing parameters, in this case, will have a value of “undefined”. A pitfall to be aware of.

Output

Note: JavaScript does not raise an error when a function that accepts Zero or more parameters is called with one or more arguments. All these additional parameters are collected in an attribute called “arguments”. Another pitfall to be aware of.

Output

Default Parameters

JavaScript allows to set a default value to a parameter. It is mentioned while declaring a function. The default value is used when a function is called with fewer arguments. In this case, instead of using the value “undefined”, the function uses the predefined default value. Refer to the code snippet below.

Output

Pass by Value or Pass by Reference

When an argument is passed to a function, it is passed by value. This means the function will maintain a separate copy of the argument. Any change in the argument’s value will not be available outside the function.

Output

When an object is passed as an argument to a function, it is passed as a value. Since an object resides in a heap, the argument value refers to the object's memory location. So, the separate copy of the argument in a function will also have the same heap address as its value. Any change made at the attribute level, in this case, will change the original object.

Output

Return Value

It is optional for a function to return a value. A function returns “undefined” when the function has no return statement. Or when the function has a return statement with no return value.

When a function has a return statement followed by an expression or a value, then the function returns that value.

A function can return only one value. One possible way to return more than one value is to pack these values in an array and return the array.

Output

A function can have more than one return statement. The return statement terminates the function execution.

In case you are not aware…

Undefined: For a variable, it means a variable has been declared and it has not been initialised. For a function, it either has a return statement with no expression or the function has no return statement.

Null: For a variable, it means that a variable has been declared and initialised as well with a “null” value. Generally, it means no value has been assigned to this variable.

Empty String “”: It is a string literal with no characters. It means that a variable has been declared and initialised as well. Unlike null, “”, is a valid assigned value.

References

--

--