Skip to content

Instantly share code, notes, and snippets.

@dan-laskowski
dan-laskowski / generateHex.js
Created May 5, 2021 12:07
[Generate a random HEX color] #javascript #js #generate #hex
// Generate a random HEX color
randomColor = () => `#${Math.random().toString(16).slice(2, 8).padStart(6, '0')}`;
// Or
const randomColor = () => `#${(~~(Math.random()*(1<<24))).toString(16)}`;
@dan-laskowski
dan-laskowski / getCookieValue.js
Created May 5, 2021 12:08
[Get the value of a specified cookie] #javascript #js #cookie #value
// Get the value of a specified cookie
cookie = name => `; ${document.cookie}`.split(`; ${name}=`).pop().split(';').shift();
cookie('_ga');
// Result: "GA1.2.1929736587.1601974046"
@dan-laskowski
dan-laskowski / swapVariables.js
Created May 5, 2021 12:09
[Swap the values of 2 variables] #javascript #js #swap #variables
// Swap the values of 2 variables
let a = 1;
let b = 2;
[a, b] = [b, a];
// Result:
// a = 2
// b = 1
@dan-laskowski
dan-laskowski / getSelectedText.js
Created May 5, 2021 12:09
[Get the text that the user has selected] #javascript #js #selected #text
/ Get the text that the user has selected
const getSelectedText = () => window.getSelection().toString();
getSelectedText();