7 Awesome JavaScript Snippets (one-liners) You Should Use know in 2023

Umar Farooque Khan
3 min readJul 10, 2023

--

Today, I am excited to present a curated collection of 7 Killer JavaScript one-liners that are guaranteed to simplify your coding journey and make your life easier. Get ready for an exhilarating experience! Let’s dive in and unleash the power of these amazing snippets. Buckle up and let’s soar to new heights! 🚀

1. Copies an input to clipboard

navigator.clipboard.writeText(document.getElementById('myInput').value);

Replace 'myInput' with the actual id of your input field. This code uses the navigator.clipboard.writeText() method to write the value of the input field to the clipboard.

2. Checks if the tab browser Tab is in view

const isTabInView = () => !document.hidden;

This code defines an arrow function isTabInView that returns the negation of the document.hidden property, indicating whether the tab is currently in view or not.

3. Checks if the date is a weekday in one line

const isWeekday = d => d.getDay() % 6 !== 0;

This code defines an arrow function isWeekday that takes a d parameter, which represents a Date object. Here's what each part of the code does:

  • d.getDay(): This part calls the getDay() method on the Date object d. The getDay() method returns the day of the week as a number, where Sunday is represented by 0 and Saturday by 6.
  • % 6: The modulus operator % is used to calculate the remainder of dividing the day of the week by 6. This operation maps Sunday (0) and Saturday (6) to 0, while the other weekdays (Monday to Friday) will have a remainder greater than 0.
  • !== 0: The inequality operator !== compares the result of the previous operation with 0. If the remainder is not equal to 0, it means the day of the week is not Sunday (0) or Saturday (6), indicating that it is a weekday.

Therefore, the expression d.getDay() % 6 !== 0 evaluates to true if the Date object d represents a weekday and false if it represents a weekend day (Sunday or Saturday).

4.Shuffle Array

const shuffledArray = array => array.sort(() => Math.random() - 0.5);

This code defines an arrow function shuffledArray that takes an array parameter. It uses the sort() method on the array and provides a comparison function that randomly sorts the elements. By subtracting Math.random() multiplied by 0.5, the comparison function generates random positive or negative values, resulting in a shuffled array. Note that this method mutates the original array.

5. Check if Date is Valid

const isDateValid = (...val) => !Number.isNaN(Date.parse(val));
  • Date.parse(val): The Date.parse() method parses the val argument, which can be a date string or individual date components. It returns the number of milliseconds since January 1, 1970 (Unix epoch) if the date is valid, or NaN if it is not.
  • Number.isNaN(): The Number.isNaN() function is used to check if the result of Date.parse() is NaN. It returns true if the value is NaN and false otherwise.

By using Date.parse() instead of the new Date() constructor and removing the .valueOf() method, we simplify the code while still achieving the desired result of checking the validity of a date.

6. Capitalise a String

const capitalizeString = str => str.charAt(0).toUpperCase() + str.slice(1);

This code defines an arrow function capitalizeString that takes a str parameter. It uses the charAt(0) method to retrieve the first character of the string and then applies the toUpperCase() method to convert it to uppercase. The slice(1) method is used to extract the rest of the string starting from the second character. Finally, the capitalised first character and the remaining string are concatenated to form the capitalised string.

7. Remove Duplicated from Array

const uniqueArray = array => [...new Set(array)];

This code defines an arrow function uniqueArray that takes an array parameter. It uses the Set object to create a new set from the array, which automatically removes duplicate values. The spread operator ... is then used to convert the set back into an array, resulting in an array with only unique elements.

Here are some additional high-quality tutorials for you to explore:

  1. JavaScript interview Question and Answer
  2. Node Js Interview Question and Answer
  3. JavaScript Tricky Question

--

--

Umar Farooque Khan
Umar Farooque Khan

Written by Umar Farooque Khan

Experienced software developer with a passion for clean code and problem-solving. Full-stack expertise in web development. Lifelong learner and team player.

No responses yet