Do you know of any other poses besides trillium? Leave a message for you

A single line of concise code is hard to maintain (and sometimes even hard to understand), but that doesn’t stop the vast majority of siege lions from imagining that they can get some satisfaction out of writing concise code.

Below is a collection of some of my recent favorites of javascript compact code. They all run in your development console, where you can view the results. In the meantime, I hope you’ll share some of your own in the comments!

The calendar

Create an array for the past seven days. If you replace the minus sign with a plus sign in the code, you’ll get an array for the next seven days

// Create an array for the past seven days
[...Array(7).keys()].map(days= > new Date(Date.now() - 86400000 * days));
Copy the code

Generate random IDS

Create ID functionality that is often used in prototyping. But I’ve seen it used in real projects. It’s not safe

// Generates a random alphanumeric string of length 11
Math.random().toString(36).substring(2);
// hg7znok52x
Copy the code

Gets the query parameters of the URL

This query parameter code to get the URL is the most streamlined QAQ I have ever seen

? foo=bar&baz=bing => {foo: bar, baz: bing}

// Get query parameters for the URLq={}; location.search.replace(/([^?&=]+)=([^&]+)/g,(_,k,v)=>q[k]=v); q;Copy the code

Local time

With a bunch of HTML, you can create a local time that contains source code that you can read in one breath, updating the page every second with the current time

// Create a local time
<body onload="SetInterval () = > document. The body. The innerHTML = new Date (). ToLocaleString () slice (10, 12))"></body>
Copy the code

An array of confusion

Randomly change the order of the array elements to obfuscate the array

// Randomly change the order of the array elements to obfuscate the array
(arr) => arr.slice().sort((a)= > Math.random() - 0.5)
/ * let a = (arr) = > arr. Slice (), sort (() = > math.h random () 0.5) let b = a ([1, 2, 3, 4, 5]) console. The log (b) * /
Copy the code

Generate random hexadecimal code (generate random colors)

Generate random hexadecimal code using JavaScript terse code

// Generate random hexadecimal code such as '# c618B2 '
The '#' + Math.floor(Math.random() * 0xffffff).toString(16).padEnd(6.'0');
Copy the code

An interview question

This is a notorious interview question that asks you to write out the results of its run

for(i=0; ++i<101;console.log(i%5? f||i:f+'Buzz'))f=i%3?' ':'Fizz'
Copy the code

Array to heavy

This is a native JS function but very compact. Set takes any iterable, such as array [1,2,3,3], and removes duplicates

// Array decrement
[...new Set(arr)]
Copy the code

Creates an array of a specific size

Easy to quickly create arrays of a specific size

[...Array(3).keys()]
/ / [0, 1, 2]
Copy the code

Return a keyboard (stunned)

This is a neat piece of code that’s hard to understand, but you’ll be surprised when it returns a graphical keyboard

// Return a keyboard graphic with a string
(_= >[..."`1234567890-=~~QWERTYUIOP[]\\~ASDFGHJKL; '~~ZXCVBNM,./~"].map(x= >(o+=` /${b='_'.repeat(w=x<y?2:'667699'[x=["BS"."TAB"."CAPS"."ENTER"][p++]||'SHIFT',p])}\ \ | `,m+=y+(x+' ').slice(0,w)+y+y,n+=y+b+y+y,l+='__'+b)[73]&&(k.push(l,m,n,o),l=' ',m=n=o=y),m=n=o=y='|',p=l=k=[])&&k.join`
`) ()Copy the code

Here’s what it looks like: