JavaScript Functions

JavaScript Functions

What is a Function?

A function is a reusable piece of code that performs a particular task or executes a set of instructions and returns something.

In JavaScript, functions are created by using the 'function' keyword followed by the function name (it can be anything but should be relatable to the code), parenthesis (), and then curly braces {} that contain the actual piece of code. We will later discuss each part in detail.

function add(){
    return 1 + 2
}
// Output: undefined

Here in the above example we have declared our function but if you run this code it will return undefined, why? Because we haven't called it yet.

Now what is function calling?

When we declare a function we are just defining our function body that tells about the functionality of the code but we have to tell JavaScript where we want to use this function, that's why we use function call.

function add(){
    return 1 + 2
}
add()
// Output: 3

Call the function by its name and parenthesis().

Here we are using these parenthesis for arguments that we can pass to the actual function as parameters.

Then we can use those parameters inside a function. Like this:

function subtract (a, b){
    return a - b
}
subtract(5,3)
// Output: 2

We can write multiple statements inside a function and execute them.

function students(){
    const subjects = ["Mathematics", "Science", "Geography", "English"]
    console.log(subjects[2])
}
students()
// Output: Geography

We can pass different datatypes as parameters like (number, string, Boolean, function, and object)

num = 10 // number type
str = "aman" // string type
bool = true  // boolean type
const data = {  // object 
    name: "aman",
    age: 19,
}
function surname(){  // function 
    console.log("bisht")
}
// now pass it into the function...
function identity(num, str, bool, data, surname){
    console.log(num, str, bool, data, surname)
}
identity(num, str, bool, data, surname)

There are a few more methods of declaring a function:

  • Anonymous functions

  • Arrow functions

Let's discuss them one by one,

Anonymous functions

Those functions that don't have their name and cannot be accessed after defining it but can be stored in a variable and then can be used.

var greet = function(){
    console.log("hello")
    console.log("there")
}
greet()

Arrow functions

Arrow functions are compact and a great alternative to traditional functions. The functionality is the same as normal functions but there is a difference in syntax.

const add = ()=>{
    return 2 + 3
}
add()

Here we use a variable that holds the function with parenthesis for parameters, a thick arrow, and then curly braces for a piece of code.

If your code inside the function is of a single line then you can remove the curly braces and return the keyword just like this:

const add = ()=> 2 + 3
add()

Now our code is more readable and short. This is the power of arrow functions.


This is the end of this article

If you found this article informative and useful show your support by liking and sharing it with your friends.

Till then, keep learning.