Functions in JavaScript

ยท

4 min read

Hello Everyone ๐ŸŒž Today is 14th February and we celebrate International Book Giving Day. ( What else is on 14th Feb ๐Ÿค” )

Google (Android 12L)

I haven't published any book yet :) but I can pass on the knowledge I have acquired about functions in JavaScript. Let's dive into it:

What are functions and why are they soo important in JavaScript

A Function is a block of code that we can reuse n no. of times. It's a reusable code! For. eg.

function sing(){
console.log("Do")
console.log("Ra")
console.log("Me")
}

In the above code, sing() is a function and for everything inside the {} brackets, we can use the code as many times as required. We can call the sing() function whenever we want in our program.

sing()
// some javascript 
// jlkjf
//kljfkj//

sing()
sing()

And each time, we don't have to type the whole code inside the sing function again! It does make life easy, you know. Functions are used ALL THE TIME AND ALMOST EVERYWHERE in JavaScript. It's crucial to understand functions. Now, lets deep dive into the Syntax:

How to define a Function

It is a Two-Step Process: 1. Defining the function 2. Calling the function

function play(){
console.log("Go the park")
console.log("play with dogs")
let flag = 1
}

In the above code, we have defined a function play(). ( STEP 1 ). We need to use the keyword function, then the name of your function ( it can be anything ) of your choice along with the () parenthesis. We have defined the function but It won't show any output in the console yet. For that, we need to Call the function ( STEP 2 ).

play() // We are calling the function
// we can call the function as many time as we want
play()
play()

Arguments in a Function

If you are familiar with javascript a little bit then you may already have encountered arguments in a function. A very basic example of this is :

console.log("hello world")

Here, we are passing the string "hello world" as an argument in the function console.log(), it prints the string on the console window.

Another example of this is :

alert("warning, beware of dogs") //"warning, beware of dogs" here is the argument
array.slice(1,3) // here, 1 and 3 are arguments for the function slice()

Arguments in a function help to change the outcome according to the arguments passed.

//we can create a function greet() which takes single argument from the user, 
function greet( firstName ){
console.log(`hello! ${firstName}. How are you?`)
}

greet("Rahul")
//output: hello! Rahul. How are you?

Here, firstName is called a Parameter. It is used to import arguments into the function. It can be any identifier name but It is conventional to give a meaningful name to the parameter instead of x and y.

Multiple arguments in function

A function can have multiple arguments. For eg.

function greet( firstName, lastName, middleName){
console.log(firstName + middleName + lastName)

}

greet('John', 'Washington', 'Jr')
//output: John Jr Washington

If while calling the function, we don't specify any argument, then the function will ignore it. But, if we try to interact with the parameter associated with it, It will show undefined.

function greet( firstName, lastName, middleName){
console.log(firstName + middleName + lastName)

}

greet('John', 'Washington')
//output: John undefined Washington

Return keyword

Presently, we will not be able to store the result/outcome of our above function anywhere. Suppose, we want to store the outcome in a variable for future use. For eg:

function sum(num1, num2){
console.log(num1 + num2)
}

sum(1,5)
//outcome: 6

In the above function sum(), we are taking two arguments ( numbers) and printing the sum. But, if we want to use this result in the future, we won't be able to do so.

Here, the return keyword helps us.

function sum(num1, num2){
return num1+num2
}

result = sum(4,5)
console.log(result+1) //9 + 1 
//output: 10

Note: Anything in the function after return will not execute. The function stops when the return is executed.

function sum(num1, num2){
return num1+num2
console.log("pls show me")
}

result = sum(6,6)
console.log(result)
//output: 12

That's it guys, these were the basic concepts regarding functions in JavaScript. I will be sharing more advanced blogs in the coming future. Thanks for reading till the end ๐Ÿ˜„

Keep Coding ๐Ÿš€

ย