The Beginner's Guide to JavaScript Functions & Parameters.

Nazmul Huda
2 min readNov 3, 2020

--

Function plays the most important role in any programming language. Like other JavaScript, function helps you to make a block of code to perform a specific task. You can execute it as many times as you want.

Define A Function

The javascript function is defined by the function keyword, then the function name of function & pair of parentheses (). Function names can contain letters, numbers, underscores like variables.

function functionName() {
// Code to be executed
}

Defining & Calling a Function

// Defining function
function sayHello() {
alert("Hello, Programmer You are welcome to this website!");
}

// Calling function
sayHello();
// 0utputs: Hello, Programmer You are welcome to this website!

Add Parameter to Function

You can pass values as argument while calling the function. Function receives values as parameter when you define a function. Function will manage values according to predefined instruction.

// Defining function
function addSum(num1, num2) {
var total = num1 + num2
alert(total)
}

// Calling function
displaySum(6, 20)
// 0utputs: 26
displaySum(-5, 17)
// 0utputs: 12

Anonymous Function

JavaScript anonymous function is a function without a name.

let show = function () {
console.log('This Is Anonymous function')
}
show()

An Immediately-invoked Function Expression (IIFE)

An Immediately-invoked Function Expression (IIFE) is an automatically executed functions.

(function() {
console.log('This Is Immediately-invoked Function')
})()

JavaScript Arrow Fucntion

JavaScript arrow function is more elegant and readable than javascript normal function.

let arrowFunction = () => {
return "This is JavaScript Arrow Function Example";
}

Pass Argument in JavaScript Arrow Fucntion

When passing a single argument in javascript arrow function no need to use parenthesis () & when passing two or more values then use parenthesis().

//Example using Single Argument
let arrowFunction = name => {
return "My Name is "+ name;
}
arrowFunction("proNazmul")
//Example using more than one Argument
let arrowFunction = (name, age) => {
return "My Name is "+ name+ "My Age is" + age;
}
arrowFunction("proNazmul", 25)

Arrow Fucntion Return is Not Needed:

When You Write Single-line Arrow function Return is not required.

let arrowFunction = name => "My Name is "+ namearrowFunction("proNazmul")

Arrow Fucntion With Default Parameter Value

You can set an arrow function with a default value. If you pass argument function will run using given arguments or not function will run using the default argument.

// Default Value will work in this function 
let defaultFunction=(name, age=25)=> "Name is"+ name +"Age is"+age; defaultFunction("proNazmul")
// Assigned Value will work in this function
let defaultFunction=(name, age=25)=> "Name is"+ name +"Age is"+age; defaultFunction("proNazmul", 23)

--

--

Nazmul Huda
Nazmul Huda

Written by Nazmul Huda

I'm a self-taught passionate Web Application Developer specialized in MERN Stack Web Development. JavaScript is my native programming language.