JavaScript is stuffed with surprises, and mastering one-liners can stage up your coding sport! Listed here are 10 contemporary JavaScript one-liners that may make your code extra environment friendly and readable. Prepare so as to add some magic to your scripts! โจ
โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ
Define:
1๏ธโฃ Convert an Object to Question String Parameters ๐
2๏ธโฃ Discover the Most Worth in an Array ๐
3๏ธโฃ Convert a Quantity to a Comma-Separated Format ๐ฐ
4๏ธโฃ Examine if a 12 months is a Leap 12 months ๐
5๏ธโฃ Convert a String to Title Case ๐
6๏ธโฃ Get the Present Timestamp in Seconds โณ
7๏ธโฃ Merge Two Arrays With out Duplicates ๐
8๏ธโฃ Get the File Extension from a Filename ๐
9๏ธโฃ Get the Common of an Array of Numbers ๐
๐ Discover the First Repeating Character in a String ๐
โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ
1๏ธโฃ Convert an Object to Question String Paramet
โ Good for working with APIs and URL parameters.
const toQueryString = (obj) => new URLSearchParams(obj).toString();
console.log(toQueryString({ identify: 'John', age: 30 })); // "identify=John&age=30"
โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ
2๏ธโฃ Discover the Most Worth in an Array ๐
โ Ensures the enter is a non-empty array earlier than discovering the utmost worth.
const maxVal = (arr) => Array.isArray(arr) && arr.size ? Math.max(...arr) : undefined;
console.log(maxVal([10, 5, 100, 2])); // 100
console.log(maxVal([])); // undefined
console.log(maxVal("hey")); // undefined
โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ
3๏ธโฃ Convert a Quantity to a Comma-Separated Format ๐ฐ
โ Nice for displaying massive numbers in a readable format.
const formatNumber = (num) => num.toLocaleString();
console.log(formatNumber(1000000)); // "1,000,000"
โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ
4๏ธโฃ Examine if a 12 months is a Leap 12 months ๐
โ Helps decide leap years with easy logic.
const isLeapYear = (12 months) => 12 months % 4 === 0 && (12 months % 100 !== 0 || 12 months % 400 === 0);
console.log(isLeapYear(2024)); // true
โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ
5๏ธโฃ Convert a String to Title Case ๐
โ Helpful for formatting textual content dynamically.
const toTitleCase = (str) => str.substitute(/bw/g, (char) => char.toUpperCase());
console.log(toTitleCase("hey world")); // "Hiya World"
โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ
6๏ธโฃ Get the Present Timestamp in Seconds โณ
โ Converts the present timestamp to seconds.
const getTimestamp = () => Math.flooring(Date.now() / 1000);
console.log(getTimestamp()); // 1740365707 (instance)
โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ
7๏ธโฃ Merge Two Arrays With out 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 ๐
โ Shortly extracts the file sort from a filename.
const getFileExtension = filename => filename.slice((filename.lastIndexOf(".") - 1 >>> 0) + 2);console.log(getFileExtension("doc.pdf")); // "pdf"
console.log(getFileExtension("archive.tar.gz")); // "gz"
console.log(getFileExtension(".gitignore")); // ""
console.log(getFileExtension("filename")); // ""
โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ
9๏ธโฃ Get the Common of an Array of Numbers ๐
โ Calculates the common in a clear, concise manner.
const common = (arr) => arr.cut back((a, b) => a + b, 0) / arr.size;
console.log(common([10, 20, 30])); // 20
โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ
๐ Discover the First Repeating Character in a String ๐
โ Useful for detecting repeated characters in strings.
const firstRepeatingChar = (str) => str.break up('').discover((c, i, arr) => arr.indexOf(c) !== i);
console.log(firstRepeatingChar("javascript")); // "a"
โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ โ
๐ Last Ideas
These JavaScript one-liners make it easier to write cleaner and extra environment friendly code. By mastering these methods, youโll code smarter, not tougher! ๐ก
๐ฌ Which one did you discover most helpful? Let me know within the feedback! โฌ๏ธ