Tag: Tips
-
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…
-
Always Use the for…of loop to iterate in JavaScript
In JavaScript to iterate over the values of an array, map, object, etc. use the for…of loop. In a specified order, it iterates over the set of values of an iterable object such that array. It would be best if you use a for…of loop for looping. You must have used the for…in loop to…
-
The simplest way to Remove Duplicate Items from a JavaScript Array
You may have often come across a requirement to remove duplicate items from a JavaScript array. There are multiple ways to achieve it. Such that, Using the forEach loop and manually checking for the duplicate values Using the array’s filter method and others Using the Set object Let us say; you have an array as…
-
How to use conditions in Angular Template using
There are various ways you can apply conditions on Angular Template. Usually, you apply conditions to show a particular value or render a specific element based on a condition. In this post, let us see, the simplest way of achieving the conditions in Angular Template using the <ng-container> and <ng-template>. To start with consider data,…
-
Using Spread Syntax to Merge Objects in JavaScript
Have you come across a requirement to merge two or more object in another object? Most simply, you can achieve it by using JavaScript Spread syntax (…). Due to its multi functionalities purpose, JavaScript Spread Syntax is one of the most useful operators. You can use it, Rest parameters to a function To merge two…
-
Why use exportAs Property in Angular
Have you used exportAs property of a directive? It could be instrumental in working with public methods of a directive. Let us say; you have created a custom directive to change the background colour of the host element on the mouse hover as below, The above custom directive changes the background colour of the host…
-
A JavaScript function inside a Block Scope: Simplified
Scoping determines the visibility of a variable, an object, or a function. For example, a variable defined inside a function is visible or can be accessed only inside that particular function. JavaScript has 4 types of scope. Global scope Functional scope Lexical Scope Block Scope Out of the above four scoping, Block Scoping became…
-
How to manually pass the value of ‘this’ object in a JavaScript function
Every JavaScript function has a ‘this’ object, and the value of ‘this’ object depends on the way you call the function. To pass the value of ‘this’ manually, you need to call the function indirectly. In JavaScript, you call a function indirectly to pass the value of ‘this’ object manually. Manually calling a function is…
-
How to make a property non-enumerable in JavaScript?
By default, all the properties of JavaScript objects are enumerable. That means you can enumerate them in a for loop or read them using the Object.values(). Let us consider an object Dog as shown below, You can enumerate all properties and read their values using the for loop or Object.values() method as shown below, Now…
-
How to merge arrays using spread operator in JavaScript?
Have you come across a requirement to merge two or more arrays into another array? Let us say, you have two arrays as shown below, You need to merge them in another array with optional extra elements in the resultant array. JavaScript array spread operator makes it very simple to merge arrays. You can merge…