Ayush Gupta has Published 530 Articles

How to prevent moment.js from loading locales with webpack?

Ayush Gupta

Ayush Gupta

Updated on 02-Dec-2019 06:57:27

447 Views

A local file is a .json file that contains a set of translations for the text strings used in a theme template file. A separate local file is used for every language.When you require moment.js in your code and pack it with webpack, the bundle size becomes huge because it ... Read More

Jasmine.js comparing arrays

Ayush Gupta

Ayush Gupta

Updated on 02-Dec-2019 06:55:34

1K+ 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.For case 1, jasmine provides the toBe method. This checks for reference. For example, Exampledescribe("Array Equality", () => {    it("should check for array ... Read More

How to break the _.each function in underscore.js

Ayush Gupta

Ayush Gupta

Updated on 02-Dec-2019 06:45:59

321 Views

You can't break from each method. It copies the native forEach method's behavior and the native forEach doesn't provide to escape the loop (other than throwing an exception).You can use other functions like −_.find: it breaks out of the loop when the element is found. For example, _.find([1, 2, 3, ... Read More

What is the use of sentry in javascript?

Ayush Gupta

Ayush Gupta

Updated on 02-Dec-2019 06:25:48

358 Views

Sentry is a complete javascript debugging and monitoring tool package that allows you to track your production code. Some of the features of sentry −Record environment and usage details to recreate and fix bugsSee the error and stack trace previously only visible in user's debug console.Apply source maps automatically to ... Read More

Proper way to catch exception from JSON.parse

Ayush Gupta

Ayush Gupta

Updated on 02-Dec-2019 06:17:05

4K+ Views

The best way to catch invalid JSON parsing errors is to put the calls to JSON.parse() to a try/catch block.Examplefunction parseJSONSafely(str) {    try {       return JSON.parse(str);    }    catch (e) {       console.err(e);       // Return a default object, or null based on use case.       return {}    } }

Difference between application/x-javascript and text/javascript content types?

Ayush Gupta

Ayush Gupta

Updated on 02-Dec-2019 06:07:38

1K+ Views

The text/javascript content type is obsolete. This was used in the early days of Html.application/x-javascript was an experimental content type(hence the x-). You should not be using this in your application.The correct content type to use in browsers is application/javascript. This helps browsers accept the content as js code.Read More

What is the replacement of lodash pluck() method?

Ayush Gupta

Ayush Gupta

Updated on 02-Dec-2019 06:06:54

1K+ Views

Pluck has been removed from lodash 4. This is because it did the same thing as a map.As a replacement, you can use the map function in the following way −Exampleimport _ from 'lodash' const objects = [{ 'a': 1 }, { 'a': 2 }]; console.log(_.map(objects, 'a'))OutputThis will give the output −[1, 2]

Where is _.pluck() in lodash version 4?

Ayush Gupta

Ayush Gupta

Updated on 02-Dec-2019 06:06:00

427 Views

Pluck has been removed from lodash 4. This is because it did the same thing as a map.As a replacement, you can use the map function in the following way −Exampleimport _ from 'lodash' const objects = [{ 'a': 1 }, { 'a': 2 }]; console.log(_.map(objects, 'a'))This will give the output −Output[1, 2]

Why doesn't JavaScript support multithreading?

Ayush Gupta

Ayush Gupta

Updated on 02-Dec-2019 06:04:38

522 Views

JavaScript used to be single-threaded. It runs using what is called an event loop.The Event Loop has one simple job — to monitor the Call Stack and the Callback Queue. If the Call Stack is empty, it will take the first event from the queue and will push it to ... Read More

How to test if a letter in a string is uppercase or lowercase using javascript?

Ayush Gupta

Ayush Gupta

Updated on 02-Dec-2019 05:59:26

7K+ Views

To test if a letter in a string is uppercase or lowercase using javascript, you can simply convert the char to its respective case and see the result.Examplefunction checkCase(ch) {    if (!isNaN(ch * 1)){       return 'ch is numeric';    }     else {     ... Read More

Advertisements