Functions in Javascript

Functions in Javascript

ยท

3 min read

What are Javascript functions

JavaScript functions are blocks of code that can be defined and called by a name. They are an essential part of the JavaScript programming language and are used to perform a specific task or set of tasks.

Functions work on the DRY ( don't repeat yourself ) concept. means you use some code many times in your codebase, So you wrap that code under a function and now when you need that code you don't need to write the same code you just simply call the function and use that code.

There are several ways to define a function in JavaScript.

function keyword

The most common way is using the function keyword, followed by the function name, a list of parameters (enclosed in parentheses), and a block of code (enclosed in curly braces). Here's an example:

function greet(name) {
  console.log(`Hello, ${name}!`);
}

In this example, greet is the name of the function, and name is the parameter. The function takes a single argument (the name argument) and logs a greeting message to the console.

To call a function, you simply use its name followed by a set of parentheses, like this:

greet('John'); // Output: 'Hello, John!'

function expression syntax

You can also define a function using the function expression syntax, which looks like this:

const greet = function(name) {
  console.log(`Hello, ${name}!`);
};

In this example, greet is a function expression that is assigned to a variable. You can call this function in the same way as before:

greet('John'); // Output: 'Hello, John!'

Arrow function syntax

Functions can also be defined using the arrow function syntax, which is a shorter and more concise syntax introduced in ECMAScript 6 (ES6). The arrow function syntax looks like this:

const greet = (name) => {
  console.log(`Hello, ${name}!`);
};

This syntax is equivalent to the function expression syntax shown above. You can call the function in the same way:

greet('John'); // Output: 'Hello, John!'

Arguments and return keyword

In addition to the syntax, there are several other important aspects of functions in JavaScript that you should be aware of.

First, functions can accept zero or more arguments. If a function is called with more arguments than it is defined with, the extra arguments are ignored. If a function is called with fewer arguments than it is defined with, the missing arguments are set to undefined.

Second, functions can return a value using the return keyword. For example:

Copy codefunction add(a, b) {
  return a + b;
}

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

In this example, the add function takes two arguments (a and b) and returns the sum of those arguments.

Finally, functions can be assigned to variables, passed as arguments to other functions, and returned as values from functions. This allows for powerful abstractions and code reuse.

For example:

Copy codefunction greet(name) {
  return `Hello, ${name}!`;
}

function logGreeting(fn, name) {
  console.log(fn(name));
}

logGreeting(greet, 'John'); // Output: 'Hello, John!'

In this example, the greet function is passed as an argument to the logGreeting function, which logs the greeting message to the console.

Thank you for reading ๐Ÿ˜Š

I hope this article will help you in your javascript journey ๐Ÿ‘๐Ÿ‘

Did you find this article valuable?

Support Rohan Malo's blog by becoming a sponsor. Any amount is appreciated!

ย