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: () => { }ContinueContinue reading “How to use for…of loop with an Object in JavaScript”
Tag Archives: Javascript
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 toContinueContinue reading “Always Use the for…of loop to iterate in JavaScript”
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 valuesUsing the array’s filter method and othersUsing the Set object Let us say; you have an array as below, letContinueContinue reading “The simplest way to Remove Duplicate Items from a JavaScript Array”
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 functionTo merge two andContinueContinue reading “Using Spread Syntax to Merge Objects in JavaScript”
Four Ways to Create a Function in JavaScript
There are four ways a function can be created in JavaScript. They are as follows: A function as a statementA function as an expressionA function as an arrow functionA function created using the Function constructor All four ways of function creation have distinct features such as an arrow function that does not have its own this object,ContinueContinue reading “Four Ways to Create a Function in JavaScript”
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 scopeFunctional scopeLexical ScopeBlock Scope Out of the above four scoping, Block Scoping became part of JavaScriptContinueContinue reading “A JavaScript function inside a Block Scope: Simplified”
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 isContinueContinue reading “How to manually pass the value of ‘this’ object in a JavaScript function”
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, let Dog = { name :’foo’, age: 9, canRun : true, eatMilk : false } You can enumerate all propertiesContinueContinue reading “How to make a property non-enumerable in JavaScript?”
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, const metrocities = [‘Delhi’, ‘Mumbai’,’Chennai’,’Bangalore’,’Hyderabad’]; const smallcities = [‘Jamshedpur’,’Pune’,’Lucknow’,’Trivandrum’]; You need to merge them in another array with optional extra elements in the resultant array. JavaScript array spread operator makesContinueContinue reading “How to merge arrays using spread operator in JavaScript?”
How to create Private Properties in JavaScript.
We don’t have Access Modifiers in JavaScript, and all properties of an object are public, which means you can read and modify them outside the object. But what if you have a requirement to create private properties? Consider Product object as mentioned below, let Product = { id: ‘1’, price: 200, title: ‘Pencil’ } console.log(Product.id);ContinueContinue reading “How to create Private Properties in JavaScript.”