Skip to main content

Posts

15 Helpful JavaScript One-Liners

  Whether you’re new to JavaScript or a more seasoned developer it’s always good to learn something new. In this article, we’ll be going over a collection of JavaScript one-liners that hopefully help you tackle some of the daily JavaScript problems that you’ll eventually run into. Hopefully, you learn a thing or two! 1. Generating a random number within a range Getting a random value in JavaScript can be easily done with the  Math.random()  function. But what about a random number in a certain range? There’s no standard JavaScript function for that. The following function can be used to solve this problem. Note that the  max  value is  included  in the range. If you want to exclude the  max  from the range you can remove the  + 1  from the function. const randomNumberInRange = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min; 2. Toggling a boolean Toggling a boolean value is one of the oldest tricks in all of the p...