Ayush Gupta has Published 486 Articles

If a DOM Element is removed, are its listeners also removed from memory in javascript?

Ayush Gupta

Ayush Gupta

Updated on 02-Dec-2019 05:35:14

3K+ Views

In modern browsers, if a DOM Element is removed, its listeners are also removed from memory in javascript.Note that this will happen ONLY if the element is reference-free. Or in other words, it doesn't have any reference and can be garbage collected. Only then its event listeners will be removed ... Read More

What is the difference between 'throw new Error' and 'throw someObject' in javascript?

Ayush Gupta

Ayush Gupta

Updated on 02-Dec-2019 05:34:13

590 Views

The difference between 'throw new Error' and 'throw someObject' in javascript is that throw new Error wraps the error passed to it in the following format −{    name: 'Error',    message: 'Whatever you pass in the constructor' }The throw someObject will throw the object as is and will not ... Read More

What are the differences between Deferreds, Promises and Futures in javascript?

Ayush Gupta

Ayush Gupta

Updated on 02-Dec-2019 05:31:52

1K+ Views

Future is an old term that is same as promise.A promise represents a value that is not yet known. This can better be understood as a proxy for a value not necessarily known when the promise is created.A deferred represents work that is not yet finished. A deferred (which generally ... Read More

What is the reason to choose jasmine over jest?

Ayush Gupta

Ayush Gupta

Updated on 27-Nov-2019 12:31:17

122 Views

There is no solid reason to choose jasmine over jest. Both are excellent libraries that have been around for a while and have an opinionated way of doing things that are quite similar. Jest is built on top of jasmine.One reason why you might choose jasmine over jest is that ... Read More

What is the use of sinon.js?

Ayush Gupta

Ayush Gupta

Updated on 27-Nov-2019 12:21:19

586 Views

SinonJS provides stand-alone test spies, stubs and mocks. It is a library that we can use to create object mocks for unit testing.Spies − Fake functions that we can use to track executions.Stubs −Functions replacements from which we can return whatever we want or have our functions work in a ... Read More

How to create an object property from a variable value in JavaScript?

Ayush Gupta

Ayush Gupta

Updated on 27-Nov-2019 12:20:26

9K+ Views

JS has 2 notations for creating object properties, the dot notation and bracket notation.To create an object property from a variable, you need to use the bracket notation in the following way −Exampleconst obj = {a: 'foo'} const prop = 'bar' // Set the property bar using the variable name ... Read More

How to terminate javascript forEach()?

Ayush Gupta

Ayush Gupta

Updated on 27-Nov-2019 12:17:48

554 Views

You can't break from the forEach method and it doesn't provide to escape the loop (other than throwing an exception).You can use other functions like _.find from lodash instead −_.find − it breaks out of the loop when the element is found. For example, Example_.find([1, 2, 3, 4], (element) => ... Read More

Most efficient method to groupby on an array of objects in Javascript

Ayush Gupta

Ayush Gupta

Updated on 27-Nov-2019 10:48:44

7K+ Views

The most efficient method to group by a key on an array of objects in js is to use the reduce function.The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.Exampleconst people = [    { name: 'Lee', ... Read More

How can I trigger an onchange event manually in javascript?

Ayush Gupta

Ayush Gupta

Updated on 27-Nov-2019 10:46:10

28K+ Views

You can dispatch events on individual elements using the dispatchEvent method. Let's say you have an element test with an onChange event −Event handler −document.querySelector('#test').addEventListener('change', () => console.log("Changed!"))Triggering the event manually −const e = new Event("change"); const element = document.querySelector('#test') element.dispatchEvent(e);This will log the following −Changed!Read More

How to delete a localStorage item when the browser window/tab is closed?

Ayush Gupta

Ayush Gupta

Updated on 27-Nov-2019 10:44:59

2K+ Views

To clear a localStorage data on browser close, you can use the window.onunload event to check for tab close.Let's say you have a local storage object called MyStorage as a global for the sake of this example. Then you can write an event handler −Examplewindow.onunload = () => {   ... Read More

Advertisements