JavaScript Array Tips For Developers

How to get Last Item of an Array in JavaScript

You can use ‘Slice’ with a negative number to get the last items of an array.

When you pass a negative number to slice, it returns the last n elements of the array as a new array.

Let’s look at an example.

JavaScript - Rounding the number

You can use toFixed() method to round the number to a specified number of decimals

Note: toFixed() always return a string.

Let’s look at an example.

How to return a value in JavaScript

In JavaScript, you don’t have to use the “return” keyword, you can use a bracket () instead.

Let’s look at an example.

How to get Unique Values from array in JavaScript

You can get all the unique values from an array by combining a SET and the Spread operator

A Set only contains unique elements and its constructor filters out all duplicates. The set operator extracts all elements into a new array.