Function > JavaScript — 5

Sachin Sarawgi
3 min readApr 23, 2020

This blog is the fifth part of the JavaScript basics series. The series will cover how to define a function with and without arguments, how to return value from the function. It’s best suited for someone new to Javascript. Link to the previous blog on the array.

Topic Covered

  • Function Definition
  • How to define a Function
  • Define a Function without arguments
  • Define a Function with arguments
  • Return Value from Function

Function Definition

In programming languages, it’s a very common requirement to have a part of code that will be used multiple times, and that part is used for doing a specific task/logic/operation. Take an example of a converting temperature in degree to celsius, it can be used in multiple places and it will have a constant operation taking place inside it.

Isn’t it's nice to have that piece of code separated out and call from wherever you want? Well here comes the functions as your savior.

How to define a Function

We define a function in Javascript with the help of the ‘function’ keyword, followed by the function name, followed by parenthesis ‘()’(will discuss more on this), followed by opening curly braces ‘{‘, followed by your piece of code which you want to call, again and again, followed by closing curly braces ‘}’.

Here we will see a simple one-line statement function that will log ‘Hello World’ to the console.

function myFirstFunction(){
console.log("Hello World");
}

Define a Function without arguments

If we are not passing any argument inside the parenthesis ‘()’ which is present after the function name, then the function defined is ‘Function without argument’. So our above function definition is an example of that only. However, after defining you want to call it right. We do it by wiring function name then parenthesis without passing any arguments in between the parenthesis as there are no arguments to it.

function myFirstFunction(){
console.log("Hello World");
}
//calling a function which doesn't take any argument
myFirstFunction();

Define a Function with arguments

If we are passing any argument inside the parenthesis ‘()’ which is present after the function name, then the function defined is ‘Function with argument’. Let’ see an example where we want to add two numeric values and log the result. We call this type of function by wiring function name then parenthesis with passing any arguments in between the parenthesis.

function add(a, b){
console.log(a + b);
}
//calling a function which takes argument
add(5, 10);

Return Value from Function

A very common scenario is you want to get some value from function after it's done with the operations inside it. Suppose in the above example after doing add operation we want to get the value instead of login it in the console. We can do this by using the ‘return’ keyword inside the function.

function add(a, b){
var result = a + b;
return result;
}
//using assignment operator to store result returned by function in a variable
var addResult = add(5, 10);

Note: We need to define a variable on the left-hand side to store the result which is returned by a function.

Note: To our surprise even if the function defined doesn’t have a return type it will return something. And guess what it returns, as there is no return statement written manually it will return ‘undefined’.

function add(a, b){
console.log(a + b);
}
var result = add(5, 10);
console.log(result); // this will print "undefined"

This ends the fifth blog of this series, in the next section, we will discuss if-else, Comparision, and Logical operators in Javascript in detail.

Reached till here, give me follow up to get the latest stories.

If you enjoyed reading this, don’t forget the applause. 👏
Thank you.

--

--

Sachin Sarawgi

Microservices | Rest API | Spring Boot | Spring Security | PostgreSQL | Kafka | Elasticsearch | Liquibase | Independent Contributor