Tag: Interviews
-
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…
-
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…
-
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: () => { }…
-
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…
-
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, By default, JavaScript allows you to read and modify all the properties.…