Utilities collection

JavaScript utilities in single line of code

JS
const copyToClipboard = (text) => navigator.clipboard.writeText(text);

copyToClipboard('Hello JavaScript');
JS
const isDateValid = (...val) => !Number.isNaN(new Date(...val).valueOf());

isDateValid('December 17, 1995 03:24:00');
// Result: true
JS
const capitalize = (str) => str.charAt(0).toUpperCase() + str.slice(1);

capitalize('follow for more');
// Result: Follow for more
JS
const 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
JS
const randomHex = () =>
  `#${Math.floor(Math.random() * 0xffffff)
    .toString(16)
    .padEnd(6, '0')}`;

console.log(randomHex());
// Result: #92b008
JS
const isEmpty = (arr) => !Array.isArray(arr) || arr.length === 0;

isEmpty([]); // true
isEmpty([1, 2, 3]); //false
Last Updated: