Found 8591 Articles for Front End Technology

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

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

554 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.

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

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 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
Updated on 27-Nov-2019 12:31:17

98 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)

What is the use of sinon.js?

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

530 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.

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

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 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
Updated on 27-Nov-2019 12:17:48

521 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. }

Difference between console.dir and console.log in javascript?

Abdul Rawoof
Updated on 02-Sep-2022 11:43:02

4K+ Views

In JavaScript, dir() and log() are the methods of console object. Console object provides access to the browser’s debugging console. The console.dir() method The console.dir() method output the list of object properties of a specified object in the console to the user. It recognizes the object just as an object and outputs its properties. console.dir shows all properties of a DOM element and can display only one object. Syntax The below syntax is for console.dir method. console.dir(object) Example Following is an example of the console.dir() method in JavaScript − console.dir(673563); console.dir("Welcome to Tutorialspoint"); console.dir(76325 * 476); The console.log() ... Read More

How to get image data url in JavaScript?

Abdul Rawoof
Updated on 19-Jul-2023 14:51:58

6K+ Views

To obtain the canvas's image data URL, we can use the canvas object's toDataURL() method, which converts the canvas drawing into a 64 bit encoded PNG URL. You can pass image/jpeg as the first argument to the toDataURL() method if you want the image data URL to be in jpeg format. You can control the image quality for a jpeg image by passing a number between 0 and 1 as the second argument to the toDataURL() method. Any images drawn onto the canvas must be hosted on a web server with the same domain as the code executing it, according ... Read More

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

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', age: 21 },    { name: 'Ajay', age: 20 },    { name: 'Jane', age: 20 } ]; function groupBy(objectArray, property) {    return objectArray.reduce((acc, obj) => {       const key = obj[property];       if (!acc[key]) {          acc[key] = [];     ... Read More

How can I trigger an onchange event manually in javascript?

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!

Advertisements