Skip to main content

Posts

Showing posts with the label Arrow function

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

Learn about arrow functions,here. 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 fun…

Making Arrow in Javascript - ES 6

Arrow function aka fat arrrow was intorduced in ES6 as a new syntax for writing Javascript function.This is the most popular and highly used ES6 feature. Source Ponyfoo
What is Arrow function? Arrow functions provide a different way of creating functions in JavaScript. Besides a shorter syntax, they offer advantages when it comes to keeping the scope of the this keyword. What I meant by this line is, unlike other functions, the value of this inside arrow functions is not dependent on how they are invoked or how they are defined.It depends only on its enclosing context.
// ES5 - function declaration function callMe(name){              console.log(name);  }
which you could also write as:  // ES5 - function expression
const callMe = function(name) {         console.log(name);
}
becomes:  // ES6
const callMe = (name) => {              console.log(name);  }
Important Uses:When having no arguments, you have to use empty parentheses in the function declaration:
const callMe = () => {