JavaScript utilities in single line of code
JSconst copyToClipboard = (text) => navigator.clipboard.writeText(text); copyToClipboard('Hello JavaScript');
JSconst isDateValid = (...val) => !Number.isNaN(new Date(...val).valueOf()); isDateValid('December 17, 1995 03:24:00'); // Result: true
JSconst capitalize = (str) => str.charAt(0).toUpperCase() + str.slice(1); capitalize('follow for more'); // Result: Follow for more
JSconst dayDif = (date1, date2) => Math.ceil(Math.abs(date1.getTime() - date2.getTime()) / 86400000); dayDif(new Date('2020-10-21'), new Date('2021-10-22')); // Result: 366
JSconst randomHex = () => `#${Math.floor(Math.random() * 0xffffff) .toString(16) .padEnd(6, '0')}`; console.log(randomHex()); // Result: #92b008
Last Updated:JSconst isEmpty = (arr) => !Array.isArray(arr) || arr.length === 0; isEmpty([]); // true isEmpty([1, 2, 3]); //false