Nairobi, Kenya

Creating JavaScript functions using 3 different methods

Creating JavaScript functions using 3 different methods

Hi in this tutorial today you will learn how to create functions in JavaScript using three common ways

We are going to declare a function that performs a similar task and our task today is to find the area of a rectangle.

Before we get to the main subject of today lets get an indepth understand of what functions are and how do they work.

what are functions?

By definition a function is a block of code that performs a specific task when invoked.

There are different tasks that one can do with a function for example adding numbers,sorting numbers,pausing a game,starting a game.

Functions perform a major role in any program since they have reduced code repetition by facilitating code re-usability

You can define a function in a program and call it whenever you need it.

Function syntax in JavaScript

In JavaScript,when defining a function we use the keyword function then followed by function name, then followed by curly braces {}, the body of the function is found in the {}

example

function name_of_function(){
	//body
}

code

function name(){
	console.log('ian')
}

When calling a function we use the function name at the end of the program

for example in our case we have name when called we will use

name()

code

function name(){
	console.log('ian')
}
name()

In our case here the function returns ‘ian’ in the console

Parameters and arguments

Parameters are the values passed inside the function during function declaration while arguments are values passed inside the function during function call.

Example

function name_of_function(parameter){
	//body
}
name_of_function(argument)

code

function get_name(name){
console.log(name)
}
get_name(ian) //returns ian

In our function above

  1. name is our parameter
  2. ian is our argument
  3. when the function is called it returns ian

Functions in JavaScript do have the return statement that return the results of the operation declared inside the function body as will be shown in or examples below.

3 ways of declaring functions in JavaScript

1. By use of function keyword

This is the normal way of declaring a function

We use the function keyword followed by the function name and body

function area(length,width)
{ return length * width }
console.log(area(8,9))

2. Naming our function as a variable

Here we assign a function a variable name followed by the function syntax.

let area= function(length,width)
{ return length * width }
console.log(area(8,9))

3. Use of arrow functions

Arrow function is one of the features introduced in the ES6 version of JavaScript.

It allows you write a code in a more clean and lesser way

let area = (length,width) =>{
	return length * width
}
console.log(area(8,9))

Explanation and functioning

In our examples above

  1. The area is our function name
  2. The length and the width are our parameters
  3. The last section in our function call the area(length,width) are our arguments
  4. When the function is called it returns 72

That’s it for today on Creating JavaScript functions using 3 different methods

Feel free to read and share other blog posts.