<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/sources/30seconds/best-of/2022" -->

---
title: Best 30 seconds of code posts of 2022 | daily.dev
description: The most upvoted 30 seconds of code posts of 2022, curated by the daily.dev community.
canonical: https://daily.dev/sources/30seconds/best-of/2022
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:url: https://daily.dev/sources/30seconds/best-of/2022
og:type: website
og:site_name: daily.dev
og:title: Best 30 seconds of code posts of 2022 | daily.dev
og:description: The most upvoted 30 seconds of code posts of 2022, curated by the daily.dev community.
og:image: https://media.daily.dev/image/upload/s--VAY5ToZt--/f_auto/v1724209435/public/daily.dev%20-%20open%20graph
---

# Best of 30 seconds of code — 2022

1. 1  
[](/posts/30-seconds-of-code-szl7yiimd "30 seconds of code")  
[![30seconds's profile](https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/197d6538eea141d7a2fb4177234e9d89)](/sources/[source]/best-of/[year])  
1m read time • Apr 13, 2022  
### 30 seconds of code  
Use Array.map() and Array.filter() to map each element to a rank using the provided comparator function. Use Array.prototype.reverse() to reverse unique values in an array based on a provided comparators function. Array Finds the lowest index at which a value should be inserted into an array in order to maintain its sorting order.

+2  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/a3066ab9051c0448b78207604f81fe5c)  
77  
4
2. 2  
[](/posts/30-seconds-of-code-pj0utkwhw "30 seconds of code")  
[![30seconds's profile](https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/197d6538eea141d7a2fb4177234e9d89)](/sources/[source]/best-of/[year])  
1m read time • Jul 16, 2022  
### 30 seconds of code  
Get elements bigger than viewport JavaScript, Browser Returns an array of HTML elements whose width is larger than that of the viewport's. Smoothly scrolls the element on which it's called into the visible area of the browser window. Subtract the two values to get the width of the vertical scrollbar.

+2  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/97c4d1d55c458eba0426cf16fc427f50)  
60  
5
3. 3  
[](/posts/30-seconds-of-code-6ztve671 "30 seconds of code")  
[![30seconds's profile](https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/197d6538eea141d7a2fb4177234e9d89)](/sources/[source]/best-of/[year])  
1m read time • Jan 09, 2022  
### 30 seconds of code  
Knowing what DNS stands for and the types of DNS records are pretty useful. Most web developers go about their day-to-day without having to deal with DNS records. A DNS is made up of multiple records of different types, each one with its own purpose. Here's a breakdown of the most commonly-used ones.

+3  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/6faf1d3ac65188c5918fe9f395f92742)  
57  
4
4. 4  
[](/posts/tip-use-clamp-in-css-for-responsive-typography-7kfzzwbz3 "Tip: Use clamp() in CSS for responsive typography")  
[![30seconds's profile](https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/197d6538eea141d7a2fb4177234e9d89)](/sources/[source]/best-of/[year])  
1m read time • Dec 28, 2022  
### Tip: Use clamp() in CSS for responsive typography  
Use clamp() in CSS to create responsive typography with a single line of code. All you need to do is set the minimum, maximum and preferred value and the browser will do the rest. For a more complex example, take a look at the Fluid typography snippet.

+1  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/bed537fbc9afea507679d6e5027d6a1d)  
35  
3
5. 5  
[](/posts/30-seconds-of-code-nwpylsf4f "30 seconds of code")  
[![30seconds's profile](https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/197d6538eea141d7a2fb4177234e9d89)](/sources/[source]/best-of/[year])  
1m read time • Apr 20, 2022  
### 30 seconds of code  
Use Array.prototype.map() to create the transpose of the given two-dimensional array. Use Array to group array elements based on function JavaScript, Array Creates an array of elements, ungrouping the elements in an array produced by zip and applying the provided function.

+2  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/a3066ab9051c0448b78207604f81fe5c)  
26
6. 6  
[](/posts/30-seconds-of-code-qjyllnmh "30 seconds of code")  
[![30seconds's profile](https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/197d6538eea141d7a2fb4177234e9d89)](/sources/[source]/best-of/[year])  
1m read time • Oct 16, 2022  
### 30 seconds of code  
30 seconds of code is much better at presenting HTML in a consistent manner. But default browser styles are not particularly great in most cases, which is why there are tons of CSS resets out there. Here's my opinionated CSS reset, along with an explanation of why I chose to include each rule.

+3  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/124b6454e46b66ba11781fee2507f930)  
21
7. 7  
[](/posts/tip-get-the-last-element-of-a-javascript-array-pe5mouaur "Tip: Get the last element of a JavaScript array")  
[![30seconds's profile](https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/197d6538eea141d7a2fb4177234e9d89)](/sources/[source]/best-of/[year])  
1m read time • Aug 28, 2022  
### Tip: Get the last element of a JavaScript array  
JavaScript, Array Returns the symmetric difference between two arrays after applying the provided function to each array element of both. This technique is interesting, but has a couple of caveats.

+2  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/bdaa87ed6f02d23c90eafca2a9c73d2a)  
21  
3
8. 8  
[](/posts/what-is-the-difference-between-object-freeze-and-object-seal-in-javascript--lfiu8ba0p "What is the difference between Object.freeze() and Object.seal() in JavaScript?")  
[![30seconds's profile](https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/197d6538eea141d7a2fb4177234e9d89)](/sources/[source]/best-of/[year])  
1m read time • Feb 06, 2022  
### What is the difference between Object.freeze() and Object.seal() in JavaScript?  
Object.freeze() and Object.seal() serve as ways to prevent a JavaScript object from being altered. Both methods perform a shallow freeze/seal on the object. nested objects and arrays are not frozen or sealed and can be mutated. To prevent this, you can deep freeze objects, as described in this related article.

+1  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/1b79415594be608fac9778a32ef02f30)  
16  
2
9. 9  
[](/posts/can-i-validate-an-email-address-in-javascript--iqfs0haq "Can I validate an email address in JavaScript?")  
[![30seconds's profile](https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/197d6538eea141d7a2fb4177234e9d89)](/sources/[source]/best-of/[year])  
2m read time • Oct 05, 2022  
### Can I validate an email address in JavaScript?  
Theoretically, email addresses can be validated using a regular expression. The only way to do so is to send an email and check the response. Even if you could validate an email address, there’s no way of knowing if this address is in fact currently in use.

+3  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/7342c8affc1ae67fffbb82fada5d4dac)  
15  
1
10. 10  
[](/posts/javascript-array-filtering-tips-okntujfsb "JavaScript array filtering tips")  
[![30seconds's profile](https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/197d6538eea141d7a2fb4177234e9d89)](/sources/[source]/best-of/[year])  
2m read time • Sep 28, 2022  
### JavaScript array filtering tips  
If you are looking for a single result in an array, you can use Array.prototype.find() instead. This will return the first element that satisfies the condition, or undefined if no such element exists. If you don't mind mutating the original array, it can be significantly faster than the previous one.

+2  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/97c4d1d55c458eba0426cf16fc427f50)  
14
11. 11  
[](/posts/what-is-the-difference-between-async-and-defer-in-script-loading--tcnxhlxjl "What is the difference between async and defer in script loading?")  
[![30seconds's profile](https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/197d6538eea141d7a2fb4177234e9d89)](/sources/[source]/best-of/[year])  
2m read time • Sep 04, 2022  
### What is the difference between async and defer in script loading?  
A plain <script> tag without attributes is what most people tend to start with. This implements the default browser behavior. The HTML will be parsed until the script tag is encountered. At this point, HTML parsing will be paused and the script will be loaded. The script will then be executed before HTML parsing can resume.

+3  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/1cc7561b987559bb5dde9ce5e2524858)  
14
12. 12  
[](/posts/30-seconds-of-code-xrx-zrtvn "30 seconds of code")  
[![30seconds's profile](https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/197d6538eea141d7a2fb4177234e9d89)](/sources/[source]/best-of/[year])  
1m read time • Aug 14, 2022  
### 30 seconds of code  
Use a CSS custom property, --aspect-ratio, to define the desired aspect ratio. Set the container element to position: relative and height: 0\. Calculate its height using the calc() function and the custom property.

+1  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/8a093cfbf340185f39d2ba3f6574f420)  
14  
1
13. 13  
[](/posts/lazy-loading-image-tfdatyl35 "Lazy-loading image")  
[![30seconds's profile](https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/197d6538eea141d7a2fb4177234e9d89)](/sources/[source]/best-of/[year])  
1m read time • Jul 29, 2022  
### Lazy-loading image  
Use the useState() hook to create a stateful value that indicates if the image has been loaded. If not, create a new and use to observer the element. Use the return value of the hook to clean up when the component unmounts.  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/bed537fbc9afea507679d6e5027d6a1d)  
14  
1
14. 14  
[](/posts/30-seconds-of-code--qf8tqza5 "30 seconds of code")  
[![30seconds's profile](https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/197d6538eea141d7a2fb4177234e9d89)](/sources/[source]/best-of/[year])  
1m read time • Mar 31, 2022  
### 30 seconds of code  
Use a for...of loop to iterate over the values of the given iterable. Creates a generator that iterates over an iterable, flattening nested iterables. Use Symbol.iterator to check if each value is an iteration. If it is, use the yield\* expression to recursively delegate to the same generator function.

+1  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/a3066ab9051c0448b78207604f81fe5c)  
13
15. 15  
[](/posts/how-can-i-check-for-a-blank-value-in-javascript--zolsdm0vz "How can I check for a blank value in JavaScript?")  
[![30seconds's profile](https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/197d6538eea141d7a2fb4177234e9d89)](/sources/[source]/best-of/[year])  
1m read time • Sep 25, 2022  
### How can I check for a blank value in JavaScript?  
JavaScript doesn't have a built-in way to check if a value is blank, but it's easy to create one. The inspiration for this comes from Rails' blank? method, but modified to fit JavaScript's needs. This can be easily checked by using Object.keys() for both types of values.

+2  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/706990954bb10d6632b46a0939ddd052)  
12  
1
16. 16  
[](/posts/what-s-the-difference-between-root-and-html-in-css---hi0iexh7 "What's the difference between :root and html in CSS?")  
[![30seconds's profile](https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/197d6538eea141d7a2fb4177234e9d89)](/sources/[source]/best-of/[year])  
1m read time • May 22, 2022  
### What's the difference between :root and html in CSS?  
CSS has two ways to target the root element of an HTML document - the :root pseudo-class and the html selector. The :root selector has a higher specificity than thehtml selector. This is because :root is a pseudo- class selector, while html is a type selector.

+2  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/c8b57c16409f92f321fa2a103a1f1a85)  
12
17. 17  
[](/posts/window-location-cheat-sheet-6z-sekuvs "Window.location Cheat Sheet")  
[![30seconds's profile](https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/197d6538eea141d7a2fb4177234e9d89)](/sources/[source]/best-of/[year])  
1m read time • Dec 21, 2022  
### Window.location Cheat Sheet  
JavaScript, Browser Creates an object containing the parameters of the current URL. Browser Parses an HTTP Cookie header string, returning an object of all cookie name-value pairs. The object is particularly useful when working with a page's URL information.

+1  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/d4d1a9c3a8f165b82261f6beaa0d6fac)  
11
18. 18  
[](/posts/30-seconds-of-code-hbyso6pqg "30 seconds of code")  
[![30seconds's profile](https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/197d6538eea141d7a2fb4177234e9d89)](/sources/[source]/best-of/[year])  
1m read time • Sep 07, 2022  
### 30 seconds of code  
Maximum subarray Finds a contiguous subarray with the largest sum within an array of numbers. Set maxSumto -Infinity to make sure that the highest negative value is returned, if all values are negative.

+1  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/1e3e7b822dc351b5a38905eb076c11f4)  
11
19. 19  
[](/posts/replace-all-occurrences-of-a-string-in-javascript-chcg1c-ui "Replace all occurrences of a string in JavaScript")  
[![30seconds's profile](https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/197d6538eea141d7a2fb4177234e9d89)](/sources/[source]/best-of/[year])  
1m read time • Jul 04, 2022  
### Replace all occurrences of a string in JavaScript  
String.prototype.replaceAll() can be used to replace all occurrences of a string in another string with relative ease. It's supported by all JavaScript engines, old and new, and is very similar to String. prototype.replace all() . While this method doesn't replace all. occurrences, it supports regular expressions. The only issue is that special characters need to be escaped, so that they are matched correctly.

+1  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/396537cbf5dc05eac07ed17a456670a7)  
10

[See all 30 seconds of code archives](/sources/30seconds/best-of)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@graph":[{"@type":"CollectionPage","@id":"https://daily.dev/sources/30seconds/best-of/2022#page","url":"https://daily.dev/sources/30seconds/best-of/2022","name":"Best 30 seconds of code Posts of 2022","description":"The most upvoted 30 seconds of code posts of 2022, curated by the daily.dev community.","isPartOf":{"@type":"WebSite","url":"https://daily.dev"}},{"@type":"ItemList","@id":"https://daily.dev/sources/30seconds/best-of/2022#items","numberOfItems":19,"itemListElement":[{"@type":"ListItem","position":1,"url":"https://daily.dev/posts/30-seconds-of-code-szl7yiimd","name":"30 seconds of code"},{"@type":"ListItem","position":2,"url":"https://daily.dev/posts/30-seconds-of-code-pj0utkwhw","name":"30 seconds of code"},{"@type":"ListItem","position":3,"url":"https://daily.dev/posts/30-seconds-of-code-6ztve671","name":"30 seconds of code"},{"@type":"ListItem","position":4,"url":"https://daily.dev/posts/tip-use-clamp-in-css-for-responsive-typography-7kfzzwbz3","name":"Tip: Use clamp() in CSS for responsive typography"},{"@type":"ListItem","position":5,"url":"https://daily.dev/posts/30-seconds-of-code-nwpylsf4f","name":"30 seconds of code"},{"@type":"ListItem","position":6,"url":"https://daily.dev/posts/30-seconds-of-code-qjyllnmh","name":"30 seconds of code"},{"@type":"ListItem","position":7,"url":"https://daily.dev/posts/tip-get-the-last-element-of-a-javascript-array-pe5mouaur","name":"Tip: Get the last element of a JavaScript array"},{"@type":"ListItem","position":8,"url":"https://daily.dev/posts/what-is-the-difference-between-object-freeze-and-object-seal-in-javascript--lfiu8ba0p","name":"What is the difference between Object.freeze() and Object.seal() in JavaScript?"},{"@type":"ListItem","position":9,"url":"https://daily.dev/posts/can-i-validate-an-email-address-in-javascript--iqfs0haq","name":"Can I validate an email address in JavaScript?"},{"@type":"ListItem","position":10,"url":"https://daily.dev/posts/javascript-array-filtering-tips-okntujfsb","name":"JavaScript array filtering tips"},{"@type":"ListItem","position":11,"url":"https://daily.dev/posts/what-is-the-difference-between-async-and-defer-in-script-loading--tcnxhlxjl","name":"What is the difference between async and defer in script loading?"},{"@type":"ListItem","position":12,"url":"https://daily.dev/posts/30-seconds-of-code-xrx-zrtvn","name":"30 seconds of code"},{"@type":"ListItem","position":13,"url":"https://daily.dev/posts/lazy-loading-image-tfdatyl35","name":"Lazy-loading image"},{"@type":"ListItem","position":14,"url":"https://daily.dev/posts/30-seconds-of-code--qf8tqza5","name":"30 seconds of code"},{"@type":"ListItem","position":15,"url":"https://daily.dev/posts/how-can-i-check-for-a-blank-value-in-javascript--zolsdm0vz","name":"How can I check for a blank value in JavaScript?"},{"@type":"ListItem","position":16,"url":"https://daily.dev/posts/what-s-the-difference-between-root-and-html-in-css---hi0iexh7","name":"What's the difference between :root and html in CSS?"},{"@type":"ListItem","position":17,"url":"https://daily.dev/posts/window-location-cheat-sheet-6z-sekuvs","name":"Window.location Cheat Sheet"},{"@type":"ListItem","position":18,"url":"https://daily.dev/posts/30-seconds-of-code-hbyso6pqg","name":"30 seconds of code"},{"@type":"ListItem","position":19,"url":"https://daily.dev/posts/replace-all-occurrences-of-a-string-in-javascript-chcg1c-ui","name":"Replace all occurrences of a string in JavaScript"}]},{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Sources","item":"https://daily.dev/sources"},{"@type":"ListItem","position":3,"name":"30 seconds of code","item":"https://daily.dev/sources/30seconds"},{"@type":"ListItem","position":4,"name":"Best of","item":"https://daily.dev/sources/30seconds/best-of"},{"@type":"ListItem","position":5,"name":"2022"}]}]}
```

