JavaScript and Its Functions

Wreet Sarker
2 min readMay 6, 2021


Today we will briefly overview functions in JS. Functions are a common feature of every programming language and is one of the fundamental building block of your JavaScript programs. If you want to write few lines of code which you want to use in various places of your program, then functions are the thing that you are looking for.

Let’s have a closer look at functions.

  1. Function Declarations : Function declarations consist of the function keyword, a name of the function, a pair of parenthesis around zero or more parameters and a pair of curly braces. A typical function declaration:
function printMyName(x) {
//expected output: 'Wreet'

A key thing about function declarations is that these functions are ‘hoisted’. Which means, the functions that are declared this way can be called above from code that appears before this declaration.

2. Function Expressions: Function expressions are quite similar to function declarations, but here functions can be declared as a variable.

const addTen = function(x) { return x + 10};
//expected output : '15'

3. Arrow Functions: In ES6, functions can be defined in a more compact way. Here, ‘=>’ is used before curly braces and if it’s a single line code, the value inside the code is automatically returned.

const doubled = (x) => x*2;
//expected output: '20'

4. Optional Parameters: When a function is called where fewer arguments are passed than the actual function definition, the other parameters get a default value of ‘undefined.

5. Rest Parameters: This is quite the opposite of optional parameters. Here one can use rest operator(‘…’) to take multiple arguments in a function.

function printNames(...names){
for (name in names){
printNames('abc', 'def', 'ghi');
// expected output: 'abc', 'def', 'ghi'



Wreet Sarker

Materials and Metallurgical Engineering graduate. Machine learning and Data Science enthusiast.