Master JavaScript ‘this’ in 30 Mins

Do you know, there are four ways to calculate the value of 'this' inside a JavaScript function?  Some pointers about 'this'  Every function has it's own 'this' An arrow function does not have it's own 'this'The value of 'this' depends on, how that function is called. The value of 'this does not depend on how the function … Continue reading Master JavaScript ‘this’ in 30 Mins

JavaScript function is also a constructor?

JavaScript function is also a “constructor.” You must have heard it many times, haven’t you? Well, that is a wrong way of interpreting the JavaScript function; even though you can create an object using a function, it does not mean you can call it a constructor. Let us discuss it: There are four ways to … Continue reading JavaScript function is also a constructor?

Union and Intersection of types in TypeScript

Do you know, TypeScript allows union and Intersection of types? Why you need that? Let us assume you need to create a function that checks whether the passed value is boolean true and returns true, otherwise returns null. That function may look like below, function trueOrNull(foo: any): any {     if (foo === true) {         return true;     }     return undefined; } console.log(trueOrNull(88)); // null  As you see, the … Continue reading Union and Intersection of types in TypeScript

Master RxJS: Part 2 – What is an observer

This is the second post in the Master RxJS series. The first post is a video tutorial that explains, why RxJS Watch it here: https://www.youtube.com/watch?v=GfuGIh-Bmxw In this post, we will discuss an observer in RxJS. An observer is an object with three call-back functions to subscribe to the notifications from an Observable. Too much technical … Continue reading Master RxJS: Part 2 – What is an observer

How to make the length of an Array read-only in JavaScript

Sometimes, you may come across a requirement that once the array is created, its length should not be changed. In this blog post, let us explore how you can achieve that. Let us say you have an array as shown below, let foo = ['1', '11']; console.log(foo.length); // 2 console.log(foo[1]); //11 As you see, the length of the array is 2, and … Continue reading How to make the length of an Array read-only in JavaScript

Link Constructors prototypes to create Inheritance in JavaScript

In JavaScript, there are multiple ways to create an object. However, among all the most popular is using a function as a constructor.   A function returns a newly created object if it is called using the new, which is also known as Constructor Invocation Pattern. You can create a function constructor like any other … Continue reading Link Constructors prototypes to create Inheritance in JavaScript

How Object.entries work internally in JavaScript

In JavaScript, the Object.entries method returns an array of object’s own enumerable properties. It returns properties as a [key, value] pair, where the key is a string. You can read enumerable properties as an array of key-value pairs using the Object.entries method as shown below, let Product = {     Title: 'Book',     Price: 120,     Author: undefined,     inStock: true } console.log(Object.entries(Product)); Since it returns … Continue reading How Object.entries work internally in JavaScript

Working with objects in a JavaScript Arrow Function

The JavaScript Arrow function is a shorter way of writing a function expression. Let us say you have a function expression as below, var add = function (num1, num2) {     return num1 + num2; } The above function can refactor for a shorter syntax using the arrow function as below, var add = (num1, num2) => num1 + num2;  So, as you see, the arrow function provides a convenient and short syntax … Continue reading Working with objects in a JavaScript Arrow Function

Why use new.target inside a function in JavaScript – Simplified

There are four invocation patterns in JavaScript, which means you can call a function in four possible ways. They are, Function Invocation PatternConstructor Invocation PatternMethod Invocation PatternIndirect Invocation Pattern Essentially, these four-invocation patterns determine the value of ‘this’ object inside a function.  Let us consider the function created below, function add(num1, num2) {     console.log(this); // global object  } let a = add(8, 9);  In the … Continue reading Why use new.target inside a function in JavaScript – Simplified

How to use for…of loop with an Object in JavaScript

In my last post, I discussed Always Use the for…of loop to iterate in JavaScript. After this post, I got a question that, is it possible to use for…of loop with an object? Means, can you use the for…of loop to iterates properties of an object.  Let us try this, let Product = {     Id: 1,     Title: 'Pen',     Price: 1000,     sendDetail: () => { } … Continue reading How to use for…of loop with an Object in JavaScript