DEV Community

Cover image for Coding Interview: Functions and closure.
Karen Molina
Karen Molina

Posted on

Coding Interview: Functions and closure.

Hi!

Now we going to review the functions and the closure.

Functions

A function is similar to a procedure. A set or statements that performs a task. Also, a function in JS is a high-level object.

We have many different ways to create functions.

A function has a particular syntax.

A function has a particular syntax.

Function declaration

A function declarations is the most easy mode to create a functions, just we must to use the keyword "function", followed by the name of the functions, the parameter inside parenthesis and the curly brackets with the statements.

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

sum(2,4) // => 6
Enter fullscreen mode Exit fullscreen mode

Function expression

For the beginners is easy to confuse the function declaration and functions expression. 'Cause both look very similar.

But the functions expression does not start with "function" keyword. Here, the functions is created at the right side of the "assignment
expression" =:

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

sum(2,4) // => 6
Enter fullscreen mode Exit fullscreen mode

IIFE (Immediately Invoked Function Expression)

An IIFE is a way to execute a function immediately after creation.

(function() {
  console.log("Hello World") // Hello World
})()
Enter fullscreen mode Exit fullscreen mode

Arrow Function

In 2015 with ES6, the arrow functions was introduced. This way to create functions allow us to write with a shorter syntax.

const sum = (num1, num2) => {
  return num1 + num2
}

sum(2,4) // => 6
Enter fullscreen mode Exit fullscreen mode

Another feature introduce it was that return value by default. If the function has only one statement, the statement returns the value without the curly brackets

const sum = (num1, num2) => num1 + num2

sum(2,4) // => 6
Enter fullscreen mode Exit fullscreen mode

Constructor

In JS, a constructor is used to create objects. The purpose of a constructor is to create an object and set some of values. Is a simple way to create an object because we don't need to explicitly state what to return. By default, return the values which the constructor has been created.

function User(first, last) {
  this.firstName = first
  this.lastName = last
}

let user1 = new User("Jon", "Snow")
console.log(user1)
let user2 = new User("Ned", "Stark")
console.log(user2)
Enter fullscreen mode Exit fullscreen mode

Closure

First, when we invoke a function, this create a new scope, with local variables. A closure is a function which has accessibility to variables and parameters from another function's scope.

function sayHi() {
  let hello = "Hola"
  function innerSayHi(){
    console.log(hello)
  }
  return innerSayHi
}

let inner = sayHi()

sayHi() // "Hola"
Enter fullscreen mode Exit fullscreen mode

Discussion (1)

Collapse
rameshkmunjal profile image
RAMESH KUMAR MUNJAL

Good article. Concisely discussed all types of functions at one place.