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

---
title: Best 30 seconds of code posts of 2021 | daily.dev
description: The most upvoted 30 seconds of code posts of 2021, curated by the daily.dev community.
canonical: https://daily.dev/sources/30seconds/best-of/2021
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:url: https://daily.dev/sources/30seconds/best-of/2021
og:type: website
og:site_name: daily.dev
og:title: Best 30 seconds of code posts of 2021 | daily.dev
og:description: The most upvoted 30 seconds of code posts of 2021, 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 — 2021

1. 1  
[](/posts/30-seconds-of-code-f9yeujmu0 "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 15, 2021  
### 30 seconds of code  
Use the useState() hook with a function toinitial its value lazily. Creates a stateful value that is persisted to sessionStorage. Use a try...catch block and Storage.getItem() to try and get the value. If no value is found, use Storage.setItems() to store the value and use it as the initial state.

+1  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/43d23aa392d8c0e2d94bc3932fe189ec)  
45  
2
2. 2  
[](/posts/is-javascript-pass-by-value-or-pass-by-reference--c7qiyxqa0 "Is JavaScript pass-by-value or pass-by-reference?")  
[![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 05, 2021  
### Is JavaScript pass-by-value or pass-by-reference?  
JavaScript is always pass-by-value. This means everything in JavaScript is a value type and function arguments are always passed by value. That being said, object types are a bit more confusing. Object types are reference types which are passed byvalue. The subtle difference here lies in the fact that an object reference is not the same as passing an object by reference.

+1  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/6433d1377ad7f534b336f6d92b11e0fb)  
43  
6
3. 3  
[](/posts/javascript-data-structures-z0xlrc4-q "JavaScript Data Structures")  
[![30seconds's profile](https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/197d6538eea141d7a2fb4177234e9d89)](/sources/[source]/best-of/[year])  
3m read time • Aug 22, 2021  
### JavaScript Data Structures  
A tree is a data structure consisting of a set of linked nodes that represent a hierarchical tree structure. Each node is linked to others via parent-children relationship. The first node in the tree is the root, whereas nodes without any children are the leaves. The main operations of a tree data structure are:insert, remove and find.

+2  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/d4798dda5e7bc00bbf27e34157dbce78)  
34
4. 4  
[](/posts/javascript-data-structures-dozbgfere "JavaScript Data Structures")  
[![30seconds's profile](https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/197d6538eea141d7a2fb4177234e9d89)](/sources/[source]/best-of/[year])  
3m read time • Aug 08, 2021  
### JavaScript Data Structures  
A linked list is a linear data structure that represents a collection of elements, where each element points to the next one. The main properties of a linked list data structure are:size: The number of elements in the linked list head: The first element in the link list tail: The last element in linked list.

+2  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/edf329a90e33617c5a8536ce9f2531d4)  
30  
2
5. 5  
[](/posts/30-seconds-of-code-cdb3ckfhq "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 04, 2021  
### 30 seconds of code  
Use the useLayoutEffect() with an empty array as the second argument to execute the provided callback only once when the component is mounted. Use Window.getComputedStyle() to get the overflow value of the body element and store it in a variable. Enables body scroll locking.

+1  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/43d23aa392d8c0e2d94bc3932fe189ec)  
19  
2
6. 6  
[](/posts/javascript-data-structures-730tso2-z "JavaScript Data Structures")  
[![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, 2021  
### JavaScript Data Structures  
A queue is a linear data structure that behaves like a real-world queue. It follows a first in, first out (FIFO) order of operations. New items are added to the end of the queue, whereas items are removed from the start. The main operations of a queue data structure are: enqueue, dequeue, peek and isEmpty.

+2  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/d4798dda5e7bc00bbf27e34157dbce78)  
19
7. 7  
[](/posts/what-is-the-only-value-not-equal-to-itself-in-javascript--u08-91dst "What is the only value not equal to itself 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 • Dec 12, 2021  
### What is the only value not equal to itself in JavaScript?  
NaN (Not-a-Number) is the only JavaScript value not equal to itself when comparing with any of the comparison operators. NaN is often the result of meaningless or invalid math computations, so it doesn't make sense for two NaN values to be considered equal.

+2  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/84819d36f5bc515939969760cf4b1ee8)  
18
8. 8  
[](/posts/javascript-data-structures-kbuygsrwq "JavaScript Data Structures")  
[![30seconds's profile](https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/197d6538eea141d7a2fb4177234e9d89)](/sources/[source]/best-of/[year])  
4m read time • Aug 31, 2021  
### JavaScript Data Structures  
A binary search tree is a data structure consisting of a set of orderd linked nodes that represent a hierarchical tree structure. Each node is linked to others via parent-children relationship. Any given node can have at most two children (left and right) The first node in the binary searchTree is the root, whereas nodes without any children are the leaves.

+3  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/a1c19774c5bc06c27c11280d9912d9af)  
17
9. 9  
[](/posts/30-seconds-of-code-cm7urzrkz "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 17, 2021  
### 30 seconds of code  
Use the useState() hook to create a state variable for the values in the form. Create a function that will be called with an appropriate event by a form field and update the state variable accordingly. Creates a stateful value from the fields in a form.

+1  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/43d23aa392d8c0e2d94bc3932fe189ec)  
16
10. 10  
[](/posts/how-do-i-compare-two-objects-in-javascript--ej6jlf-iq "How do I compare two objects 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 • Sep 26, 2021  
### How do I compare two objects in JavaScript?  
The equals helper function is used to compare two objects. It can be useful in some situations, but it can have its own pitfalls. The most common of these has to do with similar, but not equal, values that result in the same serialized string. This is because arrays and objects in JavaScript are compared by reference.

+1  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/a9acb76c5913fb1463108e31c76cf000)  
15  
1
11. 11  
[](/posts/javascript-console-log-tips-tricks-smp7ee4fl "JavaScript console.log() tips & tricks")  
[![30seconds's profile](https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/197d6538eea141d7a2fb4177234e9d89)](/sources/[source]/best-of/[year])  
• Feb 25, 2021  
### JavaScript console.log() tips & tricks  
console.trace() works the exact same as console.log() , but it also outputs the entire stack trace.console.group() allows you to group logs into collapsable structures. console.assert() provides a handy way to only log something as an error when an assertion fails (i.e. when the first argument is false)

+2  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/1654c12ac1dc23ae6b8b1c86ce7fb096)  
13
12. 12  
[](/posts/30-seconds-of-code-enx2fudni "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, 2021  
### 30 seconds of code  
Use the useState() hook to create a state variable that holds the pressed state of the given key. Define two handler functions that update the state variable on key down or key up accordingly. Use the useEffect() hook and EventTarget.addEventListener() to handle the 'keydown' and 'keyup' events.

+1  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/43d23aa392d8c0e2d94bc3932fe189ec)  
11
13. 13  
[](/posts/30-seconds-of-code-m0qneixfi "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 30, 2021  
### 30 seconds of code  
Use the useState() hook to create a state variable that holds the error. Use the useEffect() Hook to throw the error whenever it's truthy. The useCallback() hook is used to update the state and return the cached function. Creates an error dispatcher.

+2  
![Post Cover image](https://media.daily.dev/image/upload/t_mobile_feed/v1/posts/43d23aa392d8c0e2d94bc3932fe189ec)  
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/2021#page","url":"https://daily.dev/sources/30seconds/best-of/2021","name":"Best 30 seconds of code Posts of 2021","description":"The most upvoted 30 seconds of code posts of 2021, curated by the daily.dev community.","isPartOf":{"@type":"WebSite","url":"https://daily.dev"}},{"@type":"ItemList","@id":"https://daily.dev/sources/30seconds/best-of/2021#items","numberOfItems":13,"itemListElement":[{"@type":"ListItem","position":1,"url":"https://daily.dev/posts/30-seconds-of-code-f9yeujmu0","name":"30 seconds of code"},{"@type":"ListItem","position":2,"url":"https://daily.dev/posts/is-javascript-pass-by-value-or-pass-by-reference--c7qiyxqa0","name":"Is JavaScript pass-by-value or pass-by-reference?"},{"@type":"ListItem","position":3,"url":"https://daily.dev/posts/javascript-data-structures-z0xlrc4-q","name":"JavaScript Data Structures"},{"@type":"ListItem","position":4,"url":"https://daily.dev/posts/javascript-data-structures-dozbgfere","name":"JavaScript Data Structures"},{"@type":"ListItem","position":5,"url":"https://daily.dev/posts/30-seconds-of-code-cdb3ckfhq","name":"30 seconds of code"},{"@type":"ListItem","position":6,"url":"https://daily.dev/posts/javascript-data-structures-730tso2-z","name":"JavaScript Data Structures"},{"@type":"ListItem","position":7,"url":"https://daily.dev/posts/what-is-the-only-value-not-equal-to-itself-in-javascript--u08-91dst","name":"What is the only value not equal to itself in JavaScript?"},{"@type":"ListItem","position":8,"url":"https://daily.dev/posts/javascript-data-structures-kbuygsrwq","name":"JavaScript Data Structures"},{"@type":"ListItem","position":9,"url":"https://daily.dev/posts/30-seconds-of-code-cm7urzrkz","name":"30 seconds of code"},{"@type":"ListItem","position":10,"url":"https://daily.dev/posts/how-do-i-compare-two-objects-in-javascript--ej6jlf-iq","name":"How do I compare two objects in JavaScript?"},{"@type":"ListItem","position":11,"url":"https://daily.dev/posts/javascript-console-log-tips-tricks-smp7ee4fl","name":"JavaScript console.log() tips \u0026 tricks"},{"@type":"ListItem","position":12,"url":"https://daily.dev/posts/30-seconds-of-code-enx2fudni","name":"30 seconds of code"},{"@type":"ListItem","position":13,"url":"https://daily.dev/posts/30-seconds-of-code-m0qneixfi","name":"30 seconds of code"}]},{"@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":"2021"}]}]}
```

