-->
JavaScript Tricks

JavaScript Tricks

Here is a list of useful tricks for JavaScript developers that will definitely help you one day. They help you to achieve the same goal, but these are shorter and much cleaner.

1. Format JSON Code

JSON is JavaScript Object Notation. Convert a JavaScript object into a string with JSON.stringify(); This will convert the object into a string.

2. Get unique values from an array with the help of set

The set objects let you store unique values of any type, whether primitives values or object references. Set is an ES6 feature supported in all modern browsers.

3. Removing Falsy values from arrays

Use Boolean. Passing Boolean constructor as the argument of filter method. The filter() method creates an array filled with all array elements that pass a test. filter() does not execute the function for array elements without values. It does not change the original array.

4. Sort number array

sorting items in an array can be done with the sort() method. It helps sorts the items of an array. The sort order can be either alphabetic or numeric and either ascending or descending. Sort works for string, but it works differently with numbers. The problem can be fixed by providing a compare function.

5. Disable right click

The context menu can be disabled by returning false with the attribute of the body element. It can be applied to other elements as well.

6. Get last items in an array with slice

You can use the slice method with negative numbers. The slice() method returns the selected elements in an array, as a new array object. The original array is not affected by this method.






 

0 Response to "JavaScript Tricks"

Post a Comment

Ads on article

Advertise in articles 1

advertising articles 2

Advertise under the article