This article explains callback functions, call by value, and call by reference in JavaScript. A callback function, also known as a higher-order function, is a function that is passed to another function (let’s call this other function “otherFunction”) as a parameter, and the callback function is called (executed) inside the other function. A callback function is essentially a pattern (an established solution to a common problem) and therefore, the use of a callback function is also known as a callback pattern.
Example
Let’s look at the following example. In this example, we will try to implement one simple callback function. We know that in JavaScript a function is a type of object and we can send a function as a function argument. Here is one example:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
</head>
<body>
<script>
function abc() {
console.log("Its a callback function");
}
function xyz(callback) {
callback();
}
xyz(abc);
</script>
</body>
</html>
Now let’s pass an argument to the callback function. We can check it’s typed before calling the callback function and we are passing a name and surname parameter to the callback function.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
</head>
<body>
<script>
function print(name, surname) {
console.log("Name:- " + name + "Surname:-" + surname);
}
function collect(name, surname, callback) {
if (typeof callback === "function")
callback(name, surname);
}
collect('sagar', 'pulidindi', print);
</script>
</body>
</html>
Now to understand the concept of call by value and call by reference in JavaScript.
In a call by value method, the called function creates a new set of variables and copies the values of the arguments into them.
In other words, whatever value we pass to the function will be copied and the function will act on the copy. So the original value doesn’t change.
Example
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
</head>
<body>
<form id="form1" runat="server">
<script>
function samplefun(value) {
valuevalue = value + 200;
console.log("New Value:-" + value);
}
var value = 200;
samplefun(value);
console.log('Outsideof function:- ' + value);
</script>
</form>
</body>
</html>
Outside of the function, we are declaring a variable and then sending the variable to the samplefun() function. Then within the samplefun() function the new copy is being created and the reason is, if we change something within the samplefun() function then it does not affect the original variable defined outside of the function. Here is the sample output.
In a call by reference method, instead of passing a value to the function a reference/pointer to the original variable is passed.
In other words, whatever value we pass to the function, it will act on the same value and if it modifies that value, the value will be changed for the entire context.
Example
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
</head>
<body>
<form id="form1" runat="server">
<script>
function samplefun(oil) {
oil.type = 'Olive';
console.log("Oil Type:- " + oil.type);
}
var oil = {
name: 'Sunflower',
type: 'oils'
}
samplefun(oil);
console.log('Outsideof function:- ' + oil.type);
</script>
</form>
</body>
</html>
To send the reference, we first need to create the object. The object creation in JavaScript is very simple and we have created an oil object with two properties called “name” and “type”. When we are passing the actual object as a function parameter, we are passing the reference of this object. Then we are modifying the property value within the function and automatically it’s affecting the original object that we declared outside of the function.
In this article, we learned about callback functions and call by value or call by reference in JavaScript. Thank you for reading!
#javascript