Useful Must Know One-Liners In JavaScript

Useful Must Know One-Liners In JavaScript

Numerous developers across the globe make use of JavaScript, and the numbers are just increasing each day. What makes JavaScript famous as a programming language is its dynamic nature along with numerous unique features. Let us look at some one-liners in JavaScript that are absolutely useful and a must know. 

1. Converting Fahrenheit to Celsius 

It is always good to be using a consistent unit of measurement of temperature, whether you choose to keep it as Fahrenheit or Celcius. 

2. Random ID Generation

This can serve as an easy function to use for the time when you need prototyping and need to generate unique IDs. 

3. Generate a Random Number 

There are a lot of programs or scenarios where developers require the code to generate random numbers from within a given range. Math.random function is capable of achieving that and it can be transformed to get a range. 

4. Shuffle An Array

There is not a module in JavaScript that can be used like the random.shuffle() function in Python. But there is still something that can be done to shuffle an array in JavaScript with the help of only one line of code. 

5. Getting a Random Boolean

The Math.random method of JavaScript is often used by developers when they need to generate random numbers from within a given range. For generating random Boolean numbers, to get either 1 or a 0, the checking method can be used, where it is checked whether it is below or above 0.5. 

6. Generating a Random Hex Code

One-liners are often used as a challenge by developers. The one-liner shown below can be useful for generating a hexadecimal code at random. The code lime can generate up to 3 to 6 codes of colours that will help in creating a colour palette. 

7. Reverse a String

Many methods in JavaScript can be used for reversing a string but to keep it simple and achieve it in one line, the below one-liner can be used. 

8. Swapping Two Variables

The code here will show you how two variables in JavaScript can be swapped with a one-liner code, without the use of a third variable. 

9. Multiple Variable Assignment

Similar to Python, JavaScript also has the ability to assign values to more than one variable at the same time. This can be done with one line of code using a deconstructing technique. 

10. Check If Even and Odd

A very basic code problem is determining whether a number is odd or even, a problem that every programmer faces in their journey. Many methods can be used for doing this, but a simple arrow function can be used to finish the task in just one line. 

11. FizzBuzz

This is a famously asked question in interviews, checking the core of a developer or programmer. This problem needs the developer to write a code that will print numbers from 1 to 100. Fizz is printed if the number is a multiple of 3 and Buzz is printed if the number is a multiple of 5. This can be done in one line in JavaScript. 

12. Palindrome

A string or a number that is the same when reversed is called a palindrome. 

13. Checking Array Elements For A Condition

Many programmers encounter a situation in which they have to check the elements of an array, whether they meet a given condition or not. This can be completed with the following one-liner in JavaScript. 

14. Calculating The Number Of Days 

Many problems require the counting of days between two given dates. The first is to find the absolute between those two dates and then divide that number with 86400000. This will share the output of time in milliseconds in one day, and then the result is rounded and returned. 

15. Converting A String To A Number

A very simple method of converting a string to number is to make use of type conversion. 

16. Merging Multiple Arrays

A merge of more than 2 arrays is required in a lot of problems and can be done easily as shown below. 

17. Truncate a number 

To round a number to a fixed number of decimal places is necessary to achieve concise results. Math.pow() method is generally used for truncating the number to a required decimal point. 

18. Scrolling To Top Of Page

The common method window.scrollTo() can be used to scroll the control of the page to the top. You will have to provide the x and y coordinate to the function, to which coordinate you want the control to go. If you set both the coordinates at zero, automatically it will scroll to the top. 

Leave a Comment