### Making Curry in Javascript

In this post, we will try to understand Currying in Javascript, how we can make curry function and the reason behind the currying concept.

### What is Currying in Javascript?

Currying is the process of taking a function with multiple arguments and turning it into a sequence of functions each with a single argument and eventually resolve to a value.
Not clear with the definition,right?
Lets understand with example, in which we have written a function to find the volume.

// volume function definition using arrow
const volume = (l, b, h) => (l * b * h);
// calling volume function
volume(3,2,4);   // 24

Now here, when we want to call this volume function, we need to pass all the three arguments at one time. Consider a scenario where you get length, breadth and height at different part of the function or javascript file. How can we achieve this? To answer such scenario, we can use currying in javascript.

### Making curry function

Let rewrite the above volume function using currying.
const volume = (l) => {
return (b) => {
return (h) => l * b * h;
}
};
// calling this volume function
volume(3)(2)(4);  // 24
Basically, when we call volume by passing first single argument(here 3),it return a function where we pass second single argument(here 2) which return another function where we pass third single argument (here 4) which returns the final result( here 24).

The word curry has been derived from the mathematician named 'Haskell Curry'.
In algebra, we can compose a new function from two different function. e.g.
m: a -> b
n: b -> c
the two can be composed to make new function
p: a -> c     by p = m.n i.e. m(n(x))    in algebra

Following this rule in javascript, we can derive
const n = a => a + 5;
const m = a => a * 2;
const p = x => m(n(x));
p(10); //=> 30

### Behind the scene

The root cause because of which we are able to make curry function is closure in javascript.
Closure makes currying possible in JavaScript. It’s ability to retain the state of functions already
executed, gives us the ability to create factory functions — functions that can add a specific value to their argument.

Comment me if any query.
Thank you!!

### Come Closure in Javascript

What is a closure? If we want to derive a definition,we can say, A closure is a feature of Javascript where inner function has access to the outer (enclosing) function’s variables—scope chain. This scope chains means inner function has: access to its own scope (variables defined between its curly brackets and its parameters),access to the outer function’s variables and parameters, access to the global variables. Now we will look into it in detail. Consider following example
var globalVar = "xyz"; function outerFunc(outerArg) { var outerVar = 'Alex'; function innerFunc(innerArg) { var innerVar = 'Chris'; console.log("globalVar = " + globalVar); console.log("outerArg = " + outerArg); console.log("innerArg = " + innerArg); console.log("outerVar = " + outerVar); console.log("innerVar = " + innerVar); } innerFunc(456); } outerFunc(123); In this example, i…

### Javascript Hoisting Interview Question and Answers - 5

In this post,I have tried to cover all the types of possible interview questions and answers on hoisting. If you are not familiar with the hoisting, I would highly recommend you to go to my post on hoisting. Once you are comfortable with the topic, you can attempt these questions. Understanding the type of question will help you to answer. I have provided the hint after each question. All the answers are given at the end of the post.
//Question 1 console.log('bar:', bar) bar = 15 var foo = 1 console.log(foo, bar) var bar Hint: Basics of hoisting //Question 2 var foo = 5 console.log('foo:', foo) var foo; var bar = 10; var bar; console.log('bar:', bar) var baz = 10 var baz = 12 console.log('baz:', baz) Hint: Basics of hoisting //Question 3 function foo() { function bar() { return 5 } return bar() function bar() { return 10 } } console.log(foo()); Hint: Think what happens if two functions wit…

### Javascript Interview Question and Answer - 1

In this article we are going to discuss 2 javascript interview question and their answers.These questions are highly asked in the Javascript interviews.

Disclaimer: - You might have different solution working with all test cases for both the problems.

So lets discuss the first question.This question is to test your ability in terms javascript object knowledge and your logical thinking.

Q1. You have been given a nested object "Person" which has one of the property as "password".Write a generic function to change the value of all the "password" property to "*****". Object is nested and may contain other object with property "password" and so on. Here is a sample object.

var Person = {
name: 'Alex',
age : 40,