Learn these awesome Javascript concepts.
Abhishek Raj
Posted on August 2, 2021
Introduction
You must have seeing people abusing and hating JS, because they compare Javascript with other languages like Java, C++, Go but Javascript is entirely different.
In this post I will be showing some cool things in javascript.
Generator Function ⚡
These are a type of function which can pause and resume its execution.
In simple words, suppose you call this function and you want to pause it execution at a certain state/condition and after certain condition you want to again resume its execution, you can do using generator function.
In the above example, you can see generator function are created using Asterisk(*) after writing function and when you want to pause its execution use yield and to stop use return, you can even return values using yield.
If you want to resume the execution, execute .run() method of the generator object.
Usages
- Iterator
- Generating infinite number efficiently
Read More (Ctrl + Click)
Some More (Ctrl + Click)
Async Await vs Generator Function ⏳
Generator Functions and Async Functions can be used to write asynchronous code which can wait.
Generator function always yields an object like {value: any, done: bool} but Async function returns a promise to resolve or can throw an error if doesn't resolves.
Generator function runs till yield and pauses but Async function runs till await and waits there.
Closure 🤏
Closure is an environment, created inside a function which stores some variables and can be used by another function inside that scope.
In the above example, you can see how the parentName is bind with child function.
Usages
- Hiding data inside function.
- Maintaining state.
Currying 🍛
Suppose of you have a function with N arguments, converting it into N function calls with only 1 arguments, is called Currying in Javascript.
Famous Question: Create a currying function to give sum of N numbers e.g. function(1)(2)(3).....(N) = 1+2+3+...+N
Usage
- Used to create Higher Order Function
- Memoization
- Error handling
- Initializing functions
Higher Order Functions (HOF) 💪
HOF accepts functions as argument and/or returns function with closure.
E.g. Array methods like map, reduce, filter.....etc.
Usage
- Binding functions with state
Call, Apply & Bind 📞
Call, Apply and Bind are JS methods using to bind object with this.
In the above example, I have shown how you can use call, apply and bind.
Usage
- DRY: Do Not Repeat Code
- Debouncing
Connect Me @ Linkedin, Github, Twitter, Youtube 😇
Thanks to Akshay Saini for his amazing series on JS.
Posted on August 2, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.