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 to Build an Auction Platform for Real EstateE-learning Trends During 2021 PandemicHIPAA Compliance Checklist: Steps to Take to Become HIPAA-compliantInsurance App: Features and Development CostHow to automate the work of Real Estate agents?10 Fitness Industry Trends for 2020–2021How to Build an Effective Healthcare Mobile App
GOT AN IDEA? LET'S DISCUSS!
Share your project’s scope, time scales, 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!