Month: February 2019

Understanding JavaScript Promises

Dealing with asynchronous code? Promises is the way to deal with it without writing multiple callbacks. Promises were standardized and introduced in ES2015, but in ES2017 they have been superseded by async function. Understanding of Promises is the foundation of learning Async functions.

Read More

Quick Reference of Arrow Function in JavaScript

Arrow functions were introduced with ES6 as a new way of writing JavaScript functions. This post will cover basic syntax of Arrow functions, common use cases.Note – I am putting my JavaScript Developer study notes here. Note – Please read my previous post where I have explained the basics of functions in JavaScript.Basics of Functions in JavaScriptWhat is Arrow Functions?Arrow functions, sometimes referred as Fat Arrow is a new concise way of writing functions. They utilize a new syntax, => which looks like Fat Arrow. Arrow functions are anonymous and change the way this binds in functions. By using arrow functions, we can avoid typing function and return keyword and also curly brackets.Basic Syntax of Arrow Function with Multiple Parametersconst multiplyFunction = (x, y) => x * y;console.log(multiplyFunction(5, 6));Output: 30Syntax of Arrow Function with Single Parameterconst splitString = (text) => text.split(” “);console.log(splitString(“My name is Sudipta Deb”));Output: [ ‘My’, ‘name’, ‘is’, ‘Sudipta’, ‘Deb’ ]Syntax of Arrow Function with No Parameterconst functionWithNoParam = () => { console.log(“This function with no parameter”);};functionWithNoParam();Output: This function with no parameterSyntax of Arrow Function with Object returnArrow functions can be used to return an object literal expressions. var funcWithObject = (empName, empNumber) => ({ empName: empName, empNumber: empNumber,});console.log(funcWithObject(“Sudipta Deb”, 101));Output: { empName: ‘Sudipta Deb’, empNumber: 101 }Use Cases for Arrow FunctionsOne very common use case for Arrow function is Array manipulation. In the below example, each array items will be converted to upper case.const...

Read More

Class-based vs Prototype-based Programming using JavaScript

Douglas Crockford described JavaScript as the world’s most misunderstood language. Like many developers, when I started learning JavaScript, to me also, this was not a “proper” language. But I quickly understood that JavaScript comes with a rich system of object-oriented programming.There are many concepts that makes JavaScript unique, but prototype model is definitely the most important one and it creates many confusions. So having a clear understanding of prototype model is very important. And in this post, I would like to explain the concept of prototype based programming in JavaScript.The prototype based programming is a very easy to understand concept, but the source of confusion is the syntax. When a new developer looks at the JavaScript code and sees new, class or constructor it makes them think that object instantiation works the same way as other languages. But in reality, they are nothing more than syntactic sugar in JavaScript. Behind the scenes, things are working in different way. So it is very important to have a clear understanding how things works which will help you to predict their behavior.Class Based LanguageA class based language (Java, C++, C# etc.) has two important concepts:class – the interface which defines the properties and methods.entities – instantiations of the class (objects) An instance is exact same copy of the class, you cannot dynamically add/remove properties/methods from the instance as declared in the class.  You...

Read More

Archives