Skip to content

Instantly share code, notes, and snippets.

@juliobitencourt
Created January 17, 2024 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliobitencourt/097782871df5225af8094485fc0c62ea to your computer and use it in GitHub Desktop.
Save juliobitencourt/097782871df5225af8094485fc0c62ea to your computer and use it in GitHub Desktop.
JavaScript Snippets
// Create an array
Array.from({ length: 100 }, (v, i) => i)
// Copy to clipboard
const copyToClipboard = (content) => navigator.clipboard.writeText(content)
copyToClipboard("Hello fatfish")
// Get mouse selection
const getSelectedText = () => window.getSelection().toString()
getSelectedText()
// Shuffle an Array
const shuffleArray = array => array.sort(() => Math.random() - 0.5)
shuffleArray([ 1, 2,3,4, -1, 0 ]) // [3, 1, 0, 2, 4, -1]
// Get the average of multiple numbers
const average = (...args) => args.reduce((a, b) => a + b, 0) / args.length
average(0, 1, 2, -1, 9, 10) // 3.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment