Found 6710 Articles for Javascript

How to make your code faster using JavaScript Sets?

Arnab Chakraborty
Updated on 23-Aug-2022 07:20:29

1K+ Views

While writing code, we always try to make our code easier to read, less complex, more efficient, and smaller in size. For that, we follow several methodologies which make our code efficient. In this article, we shall focus on some techniques based on Javascript sets to perform certain array-like or collection-based applications which will run faster and the code will also be concise. Let us focus few use cases. How sets are different than arrays and the benefits of using sets over an array Arrays are indexed collections where each element is associated with a specific index. On the other ... Read More

Javascript search for an object key in a set

Disha Verma
Updated on 13-Mar-2025 13:09:45

422 Views

In JavaScript, to search for an object key in a Set, you need to understand how Sets store values and how objects behave in JavaScript. JavaScript Sets are useful objects that store unique values, making them excellent for handling distinct data. In this article, we'll explore different ways to search for an object key in a Set. JavaScript Set A Set in JavaScript is a collection of unique values. Values like numbers and strings can be compared directly, but objects are stored by reference. This means that even if two objects have the same properties, they are still seen ... Read More

How to know whether a value is searched in Javascript sets?

Arnab Chakraborty
Updated on 23-Aug-2022 06:54:47

174 Views

In javascript, we may use some sets to hold a few objects. Sometimes we want to check whether an element is present inside a set or not. In this article, we will see different techniques to check whether an element is inside a given set or not.p> A trivial method using for loop At first, we can search for an element by manually picking an element and checking whether it is the same as the key element or not. If so then simply return true, otherwise after completing all elements return false. Examples HTML ... Read More

How to remove all the elements from a set in javascript?

Ayush Gupta
Updated on 18-Sep-2019 12:03:47

452 Views

The Set class in JavaScript provides a clear method to remove all elements from a given set object. This method can be used as follows −Examplelet mySet = new Set(); mySet.add(1); mySet.add(2); mySet.add(1); mySet.add(3); mySet.add("a"); console.log(mySet) mySet.clear(); console.log(mySet)OutputSet { 1, 2, 3, 'a' } Set { }You can also individually remove the elements by iterating over them.Examplelet mySet = new Set(); mySet.add(1); mySet.add(2); mySet.add(1); mySet.add(3); mySet.add("a"); console.log(mySet) for(let i of mySet) {    console.log(i)    mySet.delete(i) } console.log(mySet)OutputSet { 1, 2, 3, 'a' } Set { }

Ways to create a Set in JavaScript?

Ayush Gupta
Updated on 18-Sep-2019 12:01:15

154 Views

A set is an abstract data type that can store certain values, without any particular order, and no repeated values. It is a computer implementation of the mathematical concept of a finite set. Unlike most other collection types, rather than retrieving a specific element from a set, one typically tests a value for membership in a set.Ways to create a set in js−1. Using empty Set constructorlet mySet = new Set(); mySet.add(1); mySet.add(1); console.log(mySet)OutputSet { 1 } 2. Passing an iterable to the constructorThe set constructor accepts an iterable object(list, set, etc) using which it constructs a new set.Examplelet mySet ... Read More

What are javascript sets?

Ayush Gupta
Updated on 18-Sep-2019 11:57:00

208 Views

A set is an abstract data type that can store certain values, without any particular order, and no repeated values. It is a computer implementation of the mathematical concept of a finite set. Unlike most other collection types, rather than retrieving a specific element from a set, one typically tests a value for membership in a set.You should use sets, whenever you want to store unique elements in a container for which the order doesn't matter and you mainly want to use it to check for membership of different objects. Sets are also useful when you want to perform operations ... Read More

How to duplicate Javascript object properties in another object?

Abdul Rawoof
Updated on 02-Sep-2022 12:10:12

2K+ Views

In JavaScript, objects are the collection of a key value pairs. The properties of the object are the keys and is denoted with a string. The value of the key is the value of the property of the given object. In JavaScript, the objects can be copied to other by many ways in which some of them are − Using spread operator(…) The spread operator in JavaScript is used to copy the values of the original given object to a new object. This operator is represented by three dots(…). Example 1 This example demonstrates how spread operator is used to ... Read More

What is the use of OBJECT.assign() in javascript?

Ayush Gupta
Updated on 17-Sep-2019 08:55:08

280 Views

The Object.assign() method is used to copy the values of all of the object's own properties(enumerable only) from one or more source objects to a target object. It will return the target object.Exampleconst targetObj = { a: 1, b: 2 }; const sourceObj = { b: 4, c: 5 }; const returnedTarget = Object.assign(targetObj, sourceObj); console.log(targetObj); console.log(returnedTarget); console.log(returnedTarget === targetObj); console.log(sourceObj);Output{ a: 1, b: 4, c: 5 } { a: 1, b: 4, c: 5 } true { b: 4, c: 5 }Note −sourceObj did not change.returnedTarget and targetObj are the same.The Object.assign() method only copies enumerable and own properties from ... Read More

Async/Await Functions in JavaScript

Arnab Chakraborty
Updated on 23-Aug-2022 06:52:12

949 Views

Handling asynchronous tasks is essential for network programming. In JavaScript most cases, we ask for data that are not synchronous to our system. To handle asynchronous systems, we can use Async and Await functions in JavaScript. The async keyword which is used with a function will qualify a javascript function as an asynchronous function. Async Function The async functions are very similar to the normal functions in javascript. The syntax is like below− From the syntax, it is clear that only the ‘async’ keyword is added to the function signature. Syntax async function name(param1, param2, ...paramN) { ... Read More

Multiple inheritance in JavaScript

Arnab Chakraborty
Updated on 23-Aug-2022 06:39:00

6K+ Views

JavaScript is a partially object-oriented language. To work with this, the object-oriented nature needs to be understood. In this article, we are going to focus on the multiple inheritance concept inside JavaScript. An object can be inherited from multiple other objects, i.e. the object has common property from other parent objects not only a single parent. In JavaScript, this can be achieved by merging properties from different properties into one single object. Let us see the overall syntax which represents how multiple properties can be merged. SyntaxLet us consider a function syntax like the below − function multiple(...arguments) { ... Read More

Advertisements