7 min
Apr 20 2021
JavaScript continues to grow and grow, opening the door to new "tech geeks" in the market, as it is one of the easiest languages to run. (is this really true?) It's true that JavaScript can do many amazing things! and there's so much more to learn. And whether you're new to JavaScript or a more professional developer, it's always good to learn something new. I'll cover some really useful one-liners that can help you improve performance and help you debug your code.
What is a one-liner actually? A one-liner is a code practice in which we perform some function in just one line of code.”

01 - Get a random boolean

This function will return a boolean (true or false) using Math.random() method. Math.random creates a random number between 0 and 1, after which we check if it is higher or lower than 0.5. That means it's a 50/50 chance to get either true or false.
5 javascript oneliners that you should know

02 - Check if the date is Weekend

By this function, you'll be able to check if the date that is provided is either a weekday or weekend.
02  check if the date is weekend

03 - Check if a number is even or odd

Simple utility function to check if a number is even or odd.
03  check if a number is even or odd

04 - Get the unique values in an array

A very simple method to remove all duplicate values from an array. This function converts our array to a Set and then back to an array.
04  get the unique values in an array

05 - Check if a variable is an array

A clean and easy way to check if a variable is an array. Well, there can be other ways too 😉
05  check if a variable is an array

06 - Generate a random string (unique id?)

Maybe you need to create a temporary unique id for something, here's a trick you can use to generate a random string on the go.
06  generate a random string unique id

07 - Scroll to the top of the page

The window.scrollTo() method takes an x and y coordinate to scroll to. If we set these to zero and zero, we’ll scroll to the top of the page.
07  scroll to the top of the page

08 - Toggle boolean

Toggling a boolean value is one of the very basic programming problems, that can be solved in a lot of different ways. Instead of using if-statements to determine what value to set the boolean to, you could instead use the function to flip the current value using the ! "not" operator.
08  toggle boolean

09 - Swapping Two Variables

The below code is one of the simpler ways to swap two variables without using a third variable and with just one line of code.
09  swapping two variables
That's it, guys! I hope you found this helpful. Thank you for reading and have a nice day😉
Popular articles
Share your project’s scope, timeline, technical requirements, business challenges, and other details you consider necessary. Our team will study them and contact you soon. Let’s make an exciting product together!
By sending this form I confirm that I have read and accept the Privacy Policy