Master JavaScript: 10 Surprising One-Liners You Need to Know π
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! β¬οΈ