Ayush Gupta

Ayush Gupta

433 Articles Published

Articles by Ayush Gupta

Page 37 of 44

How to get a subset of a javascript object's properties?

Ayush Gupta
Ayush Gupta
Updated on 02-Dec-2019 341 Views

To get a subset of object's properties and create a new object out of those properties, use object destructuring and property shorthand. For example, You have the following object −Exampleconst person = {    name: 'John',    age: 40,    city: 'LA',    school: 'High School' }And you only want the name and age, you can create the new objects using −const {name, age} = person; const selectedObj = {name, age}; console.log(selectedObj);OutputThis will give the output −{    name: 'John',    age: 40 }

Read More

What blocks Ruby, Python to get Javascript V8 speed?

Ayush Gupta
Ayush Gupta
Updated on 02-Dec-2019 149 Views

Nothing. They can get to V8 speed if proper investments are made in optimizing those language engines as are made for JS by Google in the V8 project.This is all really a matter of how much push is provided to the language by sponsoring organizations to further the development and optimization effort on these languages.

Read More

Jasmine JavaScript Testing - toBe vs toEqual

Ayush Gupta
Ayush Gupta
Updated on 02-Dec-2019 883 Views

Arrays can be compared in 2 ways −They refer to the same array object in memory.They may refer to different objects but their contents are all equal.ExampleFor case 1, jasmine provides the toBe method. This checks for reference. For example, describe("Array Equality", () => {    it("should check for array reference equility", () => {       let arr = [1, 2, 3];       let arr2 = arr       // Runs successfully       expect(arr).toBe(arr2);       // Fails as references are not equal       expect(arr).toBe([1, 2, 3]);    }); });OutputThis ...

Read More

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

Ayush Gupta
Ayush Gupta
Updated on 02-Dec-2019 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 from memory.

Read More

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

Ayush Gupta
Ayush Gupta
Updated on 02-Dec-2019 592 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 allow any further code execution from the try block, ie same as throw new Error.

Read More

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

Ayush Gupta
Ayush Gupta
Updated on 02-Dec-2019 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 extends Promise) can resolve itself, while a promise might not be able to do so. This can also be thought of as a promise that'll always succeed only.A promise is a placeholder for a result which is initially unknown while a deferred represents the computation that results in the value.

Read More

What is the reason to choose jasmine over jest?

Ayush Gupta
Ayush Gupta
Updated on 27-Nov-2019 123 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 it is faster. (https://github.com/facebook/jest/issues/6694)

Read More

What is the use of sinon.js?

Ayush Gupta
Ayush Gupta
Updated on 27-Nov-2019 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 way that suites us to be able to test multiple scenarios.Mocks −Fake methodsAll these objects help in unit testing our code.

Read More

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

Ayush Gupta
Ayush Gupta
Updated on 27-Nov-2019 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 prop obj[prop] = 'baz' console.log(obj);OutputThis will give the output −{    a: 'foo',    bar: 'baz' }ES6 introduces computed property names, which allow you to do −Exampleconst prop = 'bar' const obj = {    // Use a as key    a: 'foo',    // Use the value of prop ...

Read More

How to terminate javascript forEach()?

Ayush Gupta
Ayush Gupta
Updated on 27-Nov-2019 555 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) => {    // Check your condition here    if (element === 2) {       return true;    }    // Do what you want with the elements here    // ... });Throw an exception from forEach. For example,Exampletry {    [1, 2, 3, 4].forEach((element) => {       // Check your condition here       if (element === 2) {          throw new Error();       }       // Do what you want with the elements here       // ...    }) } catch (e) {    // Do nothing. }

Read More
Showing 361–370 of 433 articles
« Prev 1 35 36 37 38 39 44 Next »
Advertisements