Master JavaScript: 10 Surprising One-Liners You Need to Know πŸš€

Lokesh Prajapati
4 min read1 day ago

--

10 Unique JavaScript One-Liners to Boost Your Coding Skills πŸš€

JavaScript is full of surprises, and mastering one-liners can level up your coding game! Here are 10 fresh JavaScript one-liners that will make your code more efficient and readable. Get ready to add some magic to your scripts! ✨

β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€”

Outline:

1️⃣ Convert an Object to Query String Parameters 🌐
2️⃣ Find the Maximum Value in an Array πŸ”
3️⃣ Convert a Number to a Comma-Separated Format πŸ’°
4️⃣ Check if a Year is a Leap Year πŸ“†
5️⃣ Convert a String to Title Case πŸ” 
6️⃣ Get the Current Timestamp in Seconds ⏳
7️⃣ Merge Two Arrays Without Duplicates πŸ”—
8️⃣ Get the File Extension from a Filename πŸ“
9️⃣ Get the Average of an Array of Numbers πŸ“Š
πŸ”Ÿ Find the First Repeating Character in a String πŸ”„

β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€”

1️⃣ Convert an Object to Query String Paramet

βœ… Perfect for working with APIs and URL parameters.

const toQueryString = (obj) => new URLSearchParams(obj).toString();
console.log(toQueryString({ name: 'John', age: 30 })); // "name=John&age=30"

β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€”

2️⃣ Find the Maximum Value in an Array πŸ”

βœ… Ensures the input is a non-empty array before finding the maximum value.

const maxVal = (arr) => Array.isArray(arr) && arr.length ? Math.max(...arr) : undefined;
console.log(maxVal([10, 5, 100, 2])); // 100
console.log(maxVal([])); // undefined
console.log(maxVal("hello")); // undefined

β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€”

3️⃣ Convert a Number to a Comma-Separated Format πŸ’°

βœ… Great for displaying large numbers in a readable format.

const formatNumber = (num) => num.toLocaleString();
console.log(formatNumber(1000000)); // "1,000,000"

β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€”

4️⃣ Check if a Year is a Leap Year πŸ“†

βœ… Helps determine leap years with simple logic.

const isLeapYear = (year) => year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
console.log(isLeapYear(2024)); // true

β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€”

5️⃣ Convert a String to Title Case πŸ” 

βœ… Useful for formatting text dynamically.

const toTitleCase = (str) => str.replace(/\b\w/g, (char) => char.toUpperCase());
console.log(toTitleCase("hello world")); // "Hello World"

β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€”

6️⃣ Get the Current Timestamp in Seconds ⏳

βœ… Converts the current timestamp to seconds.

const getTimestamp = () => Math.floor(Date.now() / 1000);
console.log(getTimestamp()); // 1740365707 (example)

β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€”

7️⃣ Merge Two Arrays Without Duplicates πŸ”—

βœ… Combines arrays and removes duplicates effortlessly.

const mergeUniqueArray = (arr1, arr2) => [...new Set([...arr1, ...arr2])];
console.log(mergeUniqueArray([1, 2, 3], [3, 4, 5])); // [1, 2, 3, 4, 5]

β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€”

8️⃣ Get the File Extension from a Filename πŸ“

βœ… Quickly extracts the file type from a filename.

const getFileExtension = filename => filename.slice((filename.lastIndexOf(".") - 1 >>> 0) + 2);

console.log(getFileExtension("document.pdf")); // "pdf"
console.log(getFileExtension("archive.tar.gz")); // "gz"
console.log(getFileExtension(".gitignore")); // ""
console.log(getFileExtension("filename")); // ""

β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€”

9️⃣ Get the Average of an Array of Numbers πŸ“Š

βœ… Calculates the average in a clean, concise way.

const average = (arr) => arr.reduce((a, b) => a + b, 0) / arr.length;
console.log(average([10, 20, 30])); // 20

β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€”

πŸ”Ÿ Find the First Repeating Character in a String πŸ”„

βœ… Handy for detecting repeated characters in strings.

const firstRepeatingChar = (str) => str.split('').find((c, i, arr) => arr.indexOf(c) !== i);
console.log(firstRepeatingChar("javascript")); // "a"

β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€” β€”

πŸš€ Final Thoughts

These JavaScript one-liners help you write cleaner and more efficient code. By mastering these tricks, you’ll code smarter, not harder! πŸ’‘

πŸ’¬ Which one did you find most useful? Let me know in the comments! ⬇️

--

--

Lokesh Prajapati
Lokesh Prajapati

Written by Lokesh Prajapati

Front-end-developer | Web Designer | Shopify Experts - https://lokesh-prajapati.vercel.app/

No responses yet