Apr 20 2021
“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.
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.
03 - Check if a number is even or odd
Simple utility function to 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.
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 😉
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.
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.
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.
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.
That's it, guys! I hope you found this helpful. Thank you for reading and have a nice day😉
How Much Does it Cost to Outsource Software Development? Ways to Assemble an Effective Team and Pay ThemOn-demand Food Delivery Platforms — Types, Features, and MonetizationBest Countries for Outsourcing Developers in Eastern EuropeThe role of IoT in healthcare: advantages, popular segments in medicine and integration with clinic applicationsHow to Automate the Work of Real Estate Agents?13 Working Methods to Increase Gym Member RetentionHow to Create Security Risk Management Tools and a GRC Platform
GOT AN IDEA? LET'S DISCUSS!
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!