JavaScript Part 5: Functions In JavaScript

JavaScript PART 5: FUNCTIONS IN JavaScript


Please Subscribe Youtube| Like Facebook | Follow Twitter

Functions In JavaScript

In this article, we will provide a detailed overview of Functions in JavaScript and provide examples of how they are used in JavaScript programming.

Functions in JavaScript are an essential aspect of the language, and they play a crucial role in writing efficient and reusable code. Functions enable developers to define a block of code that can be executed multiple times, with different arguments passed in each invocation. In this article, we will explore functions in JavaScript, their syntax, types, and how to use them effectively.

Syntax

The basic syntax for creating a function is as follows:

function functionName(parameters) {
  // code to be executed
}

The keyword function is used to define a new function, followed by the function name, which can be any valid identifier. The parentheses immediately after the function name can optionally contain a list of comma-separated parameters, which are used to pass values into the function.

The code that will be executed when the function is called is enclosed within a set of curly braces. This code block can contain any number of statements that perform a specific task or set of tasks.

Types of Functions in Javascript

Javascript supports two types of functions:

  1. Built-in Functions
  2. User-defined Functions

Built-in Functions: These are functions that are already defined in JavaScript and are available to use without having to define them explicitly. Examples include console.log(), Math.random(), Array.forEach(), and parseInt().

User-defined Functions: These are functions that are created by the programmer and are defined using the function keyword followed by a name, a set of parentheses for any parameters, and a block of code enclosed in curly braces. User-defined functions can perform any task that can be accomplished with built-in functions, and they can be called multiple times with different input values.

Creating and Calling a User-defined Function in JavaScript

Functions in JavaScript can accept zero or more parameters, which are placeholders for values that are passed into the function. Parameters are specified within the parentheses following the function name. Here’s an example of a function that takes two parameters:

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

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

Output

5

In the example above, we defined a function named add that takes two parameters a and b. The function body contains a single statement that adds the two parameters together and returns the result. To call the function with arguments, we pass the values within the parentheses when invoking the function

Example Function in JavaScript with no return type and no parameters

function sayHello() {
  console.log("Hello, world!");
}
sayHello();

Output

Hello, world!

Passing Parameters to a Function in JavaScript

You can pass parameters to a function in JavaScript using two methods:

  1. Pass-by-Value
  2. Pass-by-Reference

Pass-by-Value

When you pass a primitive data type such as a number, string, or boolean to a function, the value is passed by value. This means that a copy of the value is made and passed to the function, and any changes made to the parameter within the function do not affect the original value outside the function.
Here’s an example of passing a number to a function by value:

function doubleNumber(num) {
  num *= 2;
  console.log(num); // Output: 10
}

let myNum = 5;
doubleNumber(myNum);
console.log(myNum); // Output: 5

Output

10
5

In this example, the function doubleNumber() takes a parameter num, which is a primitive data type. The parameter is multiplied by 2 within the function, but this does not affect the original value of myNum outside the function.

Pass-by-Reference

When you pass an object or array to a function, the value is passed by reference. This means that a reference to the original object or array is passed to the function, and any changes made to the parameter within the function affect the original value outside the function.

Here’s an example of passing an object to a function by reference:

function changeName(person) {
  person.name = "Jane";
  console.log(person); // Output: {name: "Jane", age: 30}
}

let myPerson = { name: "John", age: 30 };
changeName(myPerson);
console.log(myPerson); // Output: {name: "Jane", age: 30}

Output

{name: "Jane", age: 30}
{name: "Jane", age: 30}

In this example, the function changeName() takes an object person as a parameter. The function changes the value of the name property to “Jane” within the function, and this change is reflected in the original object myPerson outside the function.

Conclusion

Functions are an essential part of JavaScript programming. They allow you to group together a set of statements and execute them as a single unit. Functions help to make your code more modular, reusable, and easier to maintain.

In JavaScript, there are two types of functions: built-in functions and user-defined functions. Built-in functions are provided by the JavaScript language and can be used directly in your code. User-defined functions are functions that you define yourself in your code.

JavaScript Beginner Tutorial Series

Please Subscribe Youtube| Like Facebook | Follow Twitter


Leave a Reply

Your email address will not be published. Required fields are marked *