One-Liners in JavaScript

Home » Programming » One-Liners in JavaScript

One-Liners in JavaScript

Javascript

 

JavaScript can do a lot of things. Here is a list of one-liners you should know to become a JavaScript pro.

 

1. Swap variables

 

2. Unique Elements

Javascript has its own implementation of Hashmap known as Set.

 

3. Detect Dark Mode

Now a days it is common to add a option to switch between light and dark theme on website. Here media queries can be used to detect which theme is currently applied.

The support of matchMedia is 97.79%.

 

4. Remove falsy values from array

Pass Boolean as  parameter and you will be able to filter out all falsy values in the array.

 

5. Get average value of arguments

We can use the reduce method to get the average value of the arguments that we provide in this function.

 

6. Check if the provided day is a weekday

 

7. Scroll To Top

This is very common question of beginners. The easiest way to scroll elements is to use the scrollIntoView method. To have a smooth scrolling animation, add behavior: “smooth” parameter.

 

8. Reverse a string

This is a simple one using split(), reverse() and join() methods.

 

9. Copy to clipboard

 

10. Capitalise a string

We can use the following code to capitalise a string, as Javascript doesn’t have an inbuilt capitalise function.

 

I hope these one-liners will help you make more effective use of JavaScript in your future projects.