15 Javascript Codes You Will Always Need
15 Javascript Codes You Will Always Need
Shuffle an Array
Shuffling an array is super easy with sort and random methods.
console.log(shuffleArray([1, 2, 3, 4]));
// Result: [ 1, 4, 3, 2 ]
Check if Date is Valid
Open in app Get started
Use the following snippet to check if a given date is valid or not.
Copy to Clipboard
Easily copy any text to clipboard using navigator.clipboard.writeText .
copyToClipboard("Hello World");
dayOfYear(new Date());
// Result: 272
Capitalize a String
Javascript doesn’t have an inbuilt capitalize function, so we can use the following code for
this purpose.
console.log(randomHex());
// Result: #92b008
console.log(removeDuplicates([1, 2, 3, 3, 4, 4, 5, 5, 6]));
// Result: [ 1, 2, 3, 4, 5, 6 ]
Open in app Get started
getParameters(window.location)
// Result: { search : "easy", page : 3 }
console.log(isEven(2));
// Result: True
isNotEmpty([1, 2, 3]);
// Result: true
getSelectedText();