@ -6,7 +6,7 @@
|
||||
|
||||
* Visit [our website](https://30secondsofcode.org) to view our snippet collection.
|
||||
* Search for snippets that suit your needs. You can search by name, tag, language or using a snippet's description. Just start typing a term and see what comes up.
|
||||
* Browse the [Snippet collection](https://30secondsofcode.org/list/p/1) to see all the snippets or click individual tags at the top of the same page to narrow down your search to a specific tag.
|
||||
* Browse all [snippets](https://30secondsofcode.org/snippes/p/1) or individual [snippet collections](https://30secondsofcode.org/collections/p/1) for each topic.
|
||||
* Click on each snippet card to view the whole snippet, including code, explanation and examples.
|
||||
* You can copy code blocks on any snippet card, using the copy button at the top right.
|
||||
* If you like the project, give it a star. It means a lot to the people maintaining it.
|
||||
|
||||
@ -2,7 +2,7 @@ slug: css/centering
|
||||
name: CSS Centering
|
||||
featured: true
|
||||
snippetIds:
|
||||
- articles/s/css-centering
|
||||
- css/s/centering
|
||||
- css/s/flexbox-centering
|
||||
- css/s/grid-centering
|
||||
- css/s/transform-centering
|
||||
|
||||
@ -2,15 +2,15 @@ slug: js/array-initialization
|
||||
name: JavaScript Array Initialization
|
||||
featured: true
|
||||
snippetIds:
|
||||
- articles/s/js-array-initialization
|
||||
- js/s/array-initialization
|
||||
- js/s/initialize-array-with-values
|
||||
- js/s/initialize-array-with-range
|
||||
- js/s/initialize-array-with-range-right
|
||||
- js/s/initialize-array-with-reversed-range
|
||||
- js/s/initialize-mapped-array
|
||||
- js/s/initialize-array-while
|
||||
- js/s/initialize-array-until
|
||||
- js/s/initialize2-d-array
|
||||
- js/s/initialize-nd-array
|
||||
- js/s/initialize-2d-array
|
||||
- js/s/initialize-n-dimensional-array
|
||||
splash: laptop-lamp.png
|
||||
description: >-
|
||||
When it comes to array initialization in JavaScript, there are as many options
|
||||
|
||||
@ -2,15 +2,15 @@ slug: js/array-methods
|
||||
name: JavaScript Array Methods
|
||||
featured: true
|
||||
snippetIds:
|
||||
- articles/s/js-array-map-vs-foreach
|
||||
- articles/s/4-javascript-array-methods
|
||||
- articles/s/javascript-array-comparison
|
||||
- articles/s/javascript-index-for-of-loop
|
||||
- articles/s/javascript-for-loop-early-break
|
||||
- articles/s/js-array-initialization
|
||||
- articles/s/code-anatomy-chaining-reduce-for-loop
|
||||
- articles/s/javascript-async-array-loops
|
||||
- articles/s/js-typecheck-array
|
||||
- js/s/array-map-vs-foreach
|
||||
- js/s/4-array-methods
|
||||
- js/s/array-comparison
|
||||
- js/s/index-for-of-loop
|
||||
- js/s/for-loop-early-break
|
||||
- js/s/array-initialization
|
||||
- js/s/code-anatomy-chaining-reduce-for-loop
|
||||
- js/s/async-array-loops
|
||||
- js/s/typecheck-array
|
||||
splash: laptop-lamp.png
|
||||
description: >-
|
||||
JavaScript arrays offer a lot of data manipulation and transformation methods.
|
||||
|
||||
@ -2,18 +2,18 @@ slug: js/array-set-operations
|
||||
name: JavaScript Array Set Operations
|
||||
featured: true
|
||||
snippetIds:
|
||||
- js/s/union
|
||||
- js/s/intersection
|
||||
- js/s/difference
|
||||
- js/s/symmetric-difference
|
||||
- js/s/union-by
|
||||
- js/s/intersection-by
|
||||
- js/s/difference-by
|
||||
- js/s/symmetric-difference-by
|
||||
- js/s/union-with
|
||||
- js/s/intersection-with
|
||||
- js/s/difference-with
|
||||
- js/s/symmetric-difference-with
|
||||
- js/s/array-union
|
||||
- js/s/array-intersection
|
||||
- js/s/array-difference
|
||||
- js/s/array-symmetric-difference
|
||||
- js/s/mapped-array-union
|
||||
- js/s/mapped-array-intersection
|
||||
- js/s/mapped-array-difference
|
||||
- js/s/mapped-array-symmetric-difference
|
||||
- js/s/function-based-array-union
|
||||
- js/s/function-based-array-intersection
|
||||
- js/s/function-based-array-difference
|
||||
- js/s/function-based-array-symmetric-difference
|
||||
splash: touch-flower.png
|
||||
description: >-
|
||||
Mathematical set operations can be easily applied to JavaScript arrays. Learn
|
||||
|
||||
@ -2,18 +2,18 @@ slug: js/array-tricks
|
||||
name: JavaScript Array Tricks
|
||||
featured: true
|
||||
snippetIds:
|
||||
- js/s/unique-elements
|
||||
- js/s/compact
|
||||
- js/s/any
|
||||
- js/s/all
|
||||
- js/s/none
|
||||
- js/s/difference
|
||||
- js/s/union
|
||||
- js/s/intersection
|
||||
- js/s/head
|
||||
- js/s/tail
|
||||
- js/s/last
|
||||
- js/s/flatten
|
||||
- js/s/unique-elements-in-array
|
||||
- js/s/compact-array
|
||||
- js/s/any-array-element-is-truthy
|
||||
- js/s/all-array-elements-are-truthy
|
||||
- js/s/all-array-elements-are-falsy
|
||||
- js/s/array-difference
|
||||
- js/s/array-union
|
||||
- js/s/array-intersection
|
||||
- js/s/array-head
|
||||
- js/s/array-tail
|
||||
- js/s/last-array-element
|
||||
- js/s/flatten-array
|
||||
splash: terrarium.png
|
||||
description: >-
|
||||
Arrays are one of the most common data types in JavaScript. While there are
|
||||
|
||||
@ -2,10 +2,10 @@ slug: js/arrow-functions
|
||||
name: JavaScript Arrow Functions
|
||||
featured: true
|
||||
snippetIds:
|
||||
- articles/s/javascript-arrow-functions
|
||||
- articles/s/javascript-arrow-regular-function-differences
|
||||
- articles/s/javascript-arrow-function-event-listeners
|
||||
- articles/s/javascript-this
|
||||
- js/s/arrow-functions
|
||||
- js/s/arrow-regular-function-differences
|
||||
- js/s/arrow-function-event-listeners
|
||||
- js/s/this
|
||||
splash: coffee-float.png
|
||||
description: >-
|
||||
Arrow functions are one of the most useful tools in a JavaScript developer's
|
||||
|
||||
@ -2,17 +2,17 @@ slug: js/comparison
|
||||
name: JavaScript Comparison
|
||||
featured: true
|
||||
snippetIds:
|
||||
- articles/s/javascript-equality
|
||||
- articles/s/javascript-value-not-equal-to-itself
|
||||
- articles/s/javascript-object-comparison
|
||||
- articles/s/javascript-array-comparison
|
||||
- articles/s/javascript-date-comparison
|
||||
- articles/s/javascript-object-is-triple-equals
|
||||
- js/s/equals
|
||||
- js/s/matches
|
||||
- js/s/matches-with
|
||||
- js/s/have-same-contents
|
||||
- js/s/is-same-date
|
||||
- js/s/equality
|
||||
- js/s/value-not-equal-to-itself
|
||||
- js/s/object-comparison
|
||||
- js/s/array-comparison
|
||||
- js/s/date-comparison
|
||||
- js/s/object-is-triple-equals
|
||||
- js/s/deep-equality-comparison
|
||||
- js/s/match-object-properties
|
||||
- js/s/function-based-object-property-matches
|
||||
- js/s/arrays-have-same-contents
|
||||
- js/s/date-is-same-as-another-date
|
||||
splash: succulent.png
|
||||
description: >-
|
||||
Comparing values in JavaScript is one of the most common tasks. Yet, there are
|
||||
|
||||
@ -2,18 +2,18 @@ slug: js/css-manipulation
|
||||
name: CSS Manipulation with JavaScript
|
||||
featured: true
|
||||
snippetIds:
|
||||
- js/s/toggle-class
|
||||
- js/s/add-class
|
||||
- js/s/remove-class
|
||||
- js/s/has-class
|
||||
- js/s/toggle-html-element-class
|
||||
- js/s/add-class-to-html-element
|
||||
- js/s/remove-class-from-html-element
|
||||
- js/s/html-element-has-class
|
||||
- js/s/get-style
|
||||
- js/s/set-style
|
||||
- js/s/add-styles
|
||||
- js/s/set-style-for-html-element
|
||||
- js/s/add-styles-to-html-element
|
||||
- js/s/inject-css
|
||||
- js/s/hide
|
||||
- js/s/show
|
||||
- js/s/hide-html-elements
|
||||
- js/s/show-html-elements
|
||||
- js/s/remove-attributes
|
||||
- js/s/prefix
|
||||
- js/s/prefix-css-property
|
||||
splash: planning.png
|
||||
description: >-
|
||||
Unlock the power of manipulating CSS in the browser, using JavaScript. From
|
||||
|
||||
@ -4,17 +4,17 @@ miniName: Data Structures
|
||||
parent: js
|
||||
featured: true
|
||||
snippetIds:
|
||||
- articles/s/js-native-data-structures
|
||||
- articles/s/js-data-structures-queue
|
||||
- articles/s/js-data-structures-stack
|
||||
- articles/s/js-data-structures-linked-list
|
||||
- articles/s/js-data-structures-doubly-linked-list
|
||||
- articles/s/js-data-structures-graph
|
||||
- articles/s/js-data-structures-tree
|
||||
- articles/s/js-data-structures-binary-tree
|
||||
- articles/s/js-data-structures-binary-search-tree
|
||||
- articles/s/js-frequency-map-data-structure
|
||||
- articles/s/big-o-cheatsheet
|
||||
- js/s/native-data-structures
|
||||
- js/s/data-structures-queue
|
||||
- js/s/data-structures-stack
|
||||
- js/s/data-structures-linked-list
|
||||
- js/s/data-structures-doubly-linked-list
|
||||
- js/s/data-structures-graph
|
||||
- js/s/data-structures-tree
|
||||
- js/s/data-structures-binary-tree
|
||||
- js/s/data-structures-binary-search-tree
|
||||
- js/s/frequency-map-data-structure
|
||||
- js/s/big-o-cheatsheet
|
||||
splash: purple-flower.png
|
||||
description: >-
|
||||
The JavaScript data structures collection includes implementations and
|
||||
|
||||
@ -2,12 +2,12 @@ slug: js/dom-manipulation
|
||||
name: JavaScript DOM Manipulation
|
||||
featured: true
|
||||
snippetIds:
|
||||
- articles/s/js-create-element
|
||||
- js/s/create-html-elements
|
||||
- js/s/create-element
|
||||
- js/s/remove-element
|
||||
- js/s/insert-after
|
||||
- js/s/insert-before
|
||||
- js/s/render-element
|
||||
- js/s/remove-dom-element
|
||||
- js/s/insert-html-string-after-element
|
||||
- js/s/insert-html-string-before-element
|
||||
- js/s/render-dom-element
|
||||
splash: hiking-balance.png
|
||||
description: >-
|
||||
DOM manipulation is a core skill for any web developer. Learn how to
|
||||
|
||||
@ -9,14 +9,14 @@ snippetIds:
|
||||
- js/s/element-contains
|
||||
- js/s/find-closest-anchor
|
||||
- js/s/get-images
|
||||
- articles/s/javascript-query-selector-shorthand
|
||||
- js/s/query-selector-shorthand
|
||||
- js/s/element-is-visible-in-viewport
|
||||
- articles/s/select-focused-dom-element
|
||||
- js/s/select-focused-dom-element
|
||||
- js/s/get-elements-bigger-than-viewport
|
||||
- articles/s/js-element-from-point
|
||||
- js/s/element-from-point
|
||||
- js/s/element-is-focused
|
||||
- js/s/get-vertical-offset
|
||||
- articles/s/javascript-store-dom-items
|
||||
- js/s/store-dom-items
|
||||
splash: laptop-plants-1.png
|
||||
description: >-
|
||||
Querying the DOM is an essential skill every web developer should have. Learn
|
||||
|
||||
@ -2,19 +2,19 @@ slug: js/event-handling
|
||||
name: JavaScript Event Handling
|
||||
featured: true
|
||||
snippetIds:
|
||||
- js/s/on
|
||||
- js/s/off
|
||||
- js/s/add-event-listener
|
||||
- js/s/remove-event-listener
|
||||
- js/s/listen-once
|
||||
- js/s/on-click-outside
|
||||
- articles/s/javascript-event-bubbling-capturing-delegation
|
||||
- js/s/event-bubbling-capturing-delegation
|
||||
- js/s/add-event-listener-all
|
||||
- js/s/remove-event-listener-all
|
||||
- js/s/add-multiple-events
|
||||
- articles/s/javascript-listen-once
|
||||
- articles/s/passive-scroll-listener-performance
|
||||
- js/s/remove-multiple-event-listeners
|
||||
- js/s/add-multiple-event-listeners
|
||||
- js/s/execute-event-handler-once
|
||||
- js/s/passive-scroll-listener-performance
|
||||
- js/s/on-scroll-stop
|
||||
- articles/s/javascript-arrow-function-event-listeners
|
||||
- articles/s/detect-caps-lock-is-on
|
||||
- js/s/arrow-function-event-listeners
|
||||
- js/s/detect-caps-lock-is-on
|
||||
- js/s/trigger-event
|
||||
- js/s/on-user-input-change
|
||||
splash: monsterra.png
|
||||
|
||||
@ -2,23 +2,23 @@ slug: js/generators
|
||||
name: JavaScript Generator Functions
|
||||
featured: true
|
||||
snippetIds:
|
||||
- articles/s/javascript-range-generator
|
||||
- js/s/generator-for-range
|
||||
- js/s/range-generator
|
||||
- js/s/repeat-generator
|
||||
- js/s/cycle-generator
|
||||
- js/s/generator-to-array
|
||||
- js/s/is-generator-function
|
||||
- js/s/chunkify
|
||||
- js/s/chunk-iterable
|
||||
- js/s/date-range-generator
|
||||
- js/s/index-of-substrings
|
||||
- js/s/walk-through
|
||||
- js/s/walk-through-object
|
||||
- js/s/generate-while
|
||||
- js/s/generate-until
|
||||
- js/s/left-substr-generator
|
||||
- js/s/right-substr-generator
|
||||
- articles/s/javascript-make-iterable
|
||||
- articles/s/javascript-enum
|
||||
- articles/s/javascript-object-array-proxy
|
||||
- js/s/left-substring-generator
|
||||
- js/s/right-substring-generator
|
||||
- js/s/make-iterable
|
||||
- js/s/enum
|
||||
- js/s/object-array-proxy
|
||||
splash: generator.png
|
||||
description: >-
|
||||
JavaScript generator functions introduce a way to define an iterative
|
||||
|
||||
@ -7,7 +7,7 @@ snippetIds:
|
||||
- js/s/http-put
|
||||
- js/s/http-delete
|
||||
- articles/s/http-status-codes-cheatsheet
|
||||
- articles/s/cors-explained
|
||||
- js/s/cors-explained
|
||||
splash: laptop-plants-1.png
|
||||
description: >-
|
||||
Making network requests is a crucial part of most web applications. This
|
||||
|
||||
@ -4,39 +4,39 @@ miniName: Interview Questions
|
||||
featured: true
|
||||
parent: js
|
||||
snippetIds:
|
||||
- articles/s/javascript-equality
|
||||
- articles/s/javascript-truthy-falsy-values
|
||||
- articles/s/javascript-expression-statement
|
||||
- articles/s/javascript-undeclared-undefined-null
|
||||
- articles/s/javascript-variable-hoisting
|
||||
- articles/s/javascript-variable-scope
|
||||
- articles/s/js-callbacks
|
||||
- articles/s/javascript-pass-by-reference-or-pass-by-value
|
||||
- articles/s/javascript-promises
|
||||
- articles/s/javascript-sync-async
|
||||
- articles/s/javascript-classical-vs-prototypal-inheritance
|
||||
- articles/s/javascript-this
|
||||
- articles/s/javascript-shallow-deep-clone
|
||||
- articles/s/javascript-copy-array
|
||||
- articles/s/javascript-object-comparison
|
||||
- articles/s/javascript-array-comparison
|
||||
- articles/s/javascript-array-includes-value
|
||||
- articles/s/js-array-map-vs-foreach
|
||||
- articles/s/javascript-event-bubbling-capturing-delegation
|
||||
- articles/s/javascript-value-not-equal-to-itself
|
||||
- articles/s/javascript-blank-value
|
||||
- articles/s/javascript-string-immutability
|
||||
- articles/s/js-static-instance-methods
|
||||
- articles/s/javascript-arrow-functions
|
||||
- articles/s/javascript-pure-functions
|
||||
- articles/s/javascript-recursion
|
||||
- articles/s/javascript-use-strict
|
||||
- articles/s/js-async-defer
|
||||
- articles/s/javascript-closures
|
||||
- articles/s/javascript-iife
|
||||
- articles/s/javascript-event-loop-explained
|
||||
- articles/s/javascript-prefix-postfix-operators
|
||||
- articles/s/cors-explained
|
||||
- js/s/equality
|
||||
- js/s/truthy-falsy-values
|
||||
- js/s/expression-statement
|
||||
- js/s/undeclared-undefined-null
|
||||
- js/s/variable-hoisting
|
||||
- js/s/variable-scope
|
||||
- js/s/callbacks
|
||||
- js/s/pass-by-reference-or-pass-by-value
|
||||
- js/s/promises
|
||||
- js/s/sync-async
|
||||
- js/s/classical-vs-prototypal-inheritance
|
||||
- js/s/this
|
||||
- js/s/shallow-deep-clone
|
||||
- js/s/copy-array
|
||||
- js/s/object-comparison
|
||||
- js/s/array-comparison
|
||||
- js/s/array-includes-value
|
||||
- js/s/array-map-vs-foreach
|
||||
- js/s/event-bubbling-capturing-delegation
|
||||
- js/s/value-not-equal-to-itself
|
||||
- js/s/blank-value
|
||||
- js/s/string-immutability
|
||||
- js/s/static-instance-methods
|
||||
- js/s/arrow-functions
|
||||
- js/s/pure-functions
|
||||
- js/s/recursion
|
||||
- js/s/use-strict
|
||||
- js/s/async-defer
|
||||
- js/s/closures
|
||||
- js/s/iife
|
||||
- js/s/event-loop-explained
|
||||
- js/s/prefix-postfix-operators
|
||||
- js/s/cors-explained
|
||||
splash: interview.png
|
||||
description: >-
|
||||
Preparing for a job interview requires studying to refresh your knowledge and
|
||||
|
||||
@ -7,10 +7,10 @@ snippetIds:
|
||||
- js/s/not
|
||||
- js/s/xor
|
||||
- js/s/nor
|
||||
- js/s/when
|
||||
- js/s/conditionally-apply-function
|
||||
- js/s/both
|
||||
- js/s/either
|
||||
- js/s/complement
|
||||
- js/s/function-logical-or
|
||||
- js/s/logical-complement
|
||||
splash: chess-pawns.png
|
||||
description: >-
|
||||
Boolean logic and logical operations might not come up that often in
|
||||
|
||||
@ -2,10 +2,10 @@ slug: js/object-cloning
|
||||
name: JavaScript Object Cloning
|
||||
featured: true
|
||||
snippetIds:
|
||||
- articles/s/javascript-shallow-deep-clone
|
||||
- js/s/shallow-clone
|
||||
- js/s/deep-clone
|
||||
- articles/s/javascript-copy-array
|
||||
- js/s/shallow-deep-clone
|
||||
- js/s/shallow-clone-object
|
||||
- js/s/deep-clone-object
|
||||
- js/s/copy-array
|
||||
splash: coffee-phone-tray.png
|
||||
description: >-
|
||||
JavaScript objects and arrays are a little tricky to clone. Luckily, this
|
||||
|
||||
@ -2,10 +2,10 @@ slug: js/object-key-selection
|
||||
name: JavaScript Object Key Selection
|
||||
featured: true
|
||||
snippetIds:
|
||||
- js/s/pick
|
||||
- js/s/omit
|
||||
- js/s/pick-by
|
||||
- js/s/omit-by
|
||||
- js/s/pick-object-keys
|
||||
- js/s/omit-object-keys
|
||||
- js/s/pick-matching-object-keys
|
||||
- js/s/omit-matching-object-keys
|
||||
splash: succulent-cluster.png
|
||||
description: >-
|
||||
Sometimes, JavaScript objects come with a lot of keys and you only need a few
|
||||
|
||||
@ -4,10 +4,10 @@ featured: true
|
||||
snippetIds:
|
||||
- js/s/lowerize
|
||||
- js/s/upperize
|
||||
- js/s/map-keys
|
||||
- js/s/rename-keys
|
||||
- js/s/deep-map-keys
|
||||
- js/s/symbolize-keys
|
||||
- js/s/map-object-keys
|
||||
- js/s/rename-object-keys
|
||||
- js/s/deep-map-object-keys
|
||||
- js/s/symbolize-object-keys
|
||||
splash: plant-window.png
|
||||
description: >-
|
||||
Sometimes, object keys aren't exactly what you want them to be. Luckily, this
|
||||
|
||||
@ -2,16 +2,16 @@ slug: js/performance
|
||||
name: JavaScript Performance Optimization
|
||||
featured: true
|
||||
snippetIds:
|
||||
- articles/s/javascript-store-dom-items
|
||||
- articles/s/passive-scroll-listener-performance
|
||||
- articles/s/js-textcontent-or-innertext
|
||||
- articles/s/javascript-memoization
|
||||
- articles/s/js-array-filtering-tips
|
||||
- articles/s/js-fast-remove-array-element
|
||||
- articles/s/code-anatomy-optimizing-recursion
|
||||
- articles/s/javascript-switch-object
|
||||
- articles/s/js-dynamic-properties-are-slow
|
||||
- articles/s/big-o-cheatsheet
|
||||
- js/s/store-dom-items
|
||||
- js/s/passive-scroll-listener-performance
|
||||
- js/s/textcontent-or-innertext
|
||||
- js/s/memoization
|
||||
- js/s/array-filtering-tips
|
||||
- js/s/fast-remove-array-element
|
||||
- js/s/code-anatomy-optimizing-recursion
|
||||
- js/s/switch-object
|
||||
- js/s/dynamic-properties-are-slow
|
||||
- js/s/big-o-cheatsheet
|
||||
splash: do-more-computer.png
|
||||
description: >-
|
||||
Enhance your website's performance with our collection of tips and tricks on
|
||||
|
||||
@ -2,12 +2,12 @@ slug: js/promises
|
||||
name: JavaScript Promises
|
||||
featured: true
|
||||
snippetIds:
|
||||
- articles/s/javascript-promises
|
||||
- articles/s/async-javascript-cheatsheet
|
||||
- articles/s/javascript-sync-async
|
||||
- articles/s/javascript-promise-then-catch
|
||||
- articles/s/javascript-promise-then-finally
|
||||
- articles/s/javascript-await-timeout
|
||||
- js/s/promises
|
||||
- js/s/async-cheatsheet
|
||||
- js/s/sync-async
|
||||
- js/s/promise-then-catch
|
||||
- js/s/promise-then-finally
|
||||
- js/s/await-timeout
|
||||
- js/s/debounce-promise
|
||||
- js/s/run-promises-in-series
|
||||
splash: blue-computer.png
|
||||
|
||||
@ -2,15 +2,15 @@ slug: js/proxy
|
||||
name: JavaScript Proxy
|
||||
featured: true
|
||||
snippetIds:
|
||||
- articles/s/js-proxy-introduction
|
||||
- articles/s/js-dynamic-getter-setter-proxy
|
||||
- articles/s/js-immutable-object-proxy
|
||||
- articles/s/javascript-singleton-proxy
|
||||
- articles/s/js-dynamic-getter-chain-proxy
|
||||
- articles/s/javascript-object-array-proxy
|
||||
- articles/s/js-proxy-array-negative-index
|
||||
- articles/s/js-cannot-extend-proxy-object
|
||||
- articles/s/js-typecheck-proxy
|
||||
- js/s/proxy-introduction
|
||||
- js/s/dynamic-getter-setter-proxy
|
||||
- js/s/immutable-object-proxy
|
||||
- js/s/singleton-proxy
|
||||
- js/s/dynamic-getter-chain-proxy
|
||||
- js/s/object-array-proxy
|
||||
- js/s/proxy-array-negative-index
|
||||
- js/s/cannot-extend-proxy-object
|
||||
- js/s/typecheck-proxy
|
||||
splash: laptop-coffee.png
|
||||
description: >-
|
||||
The JavaScript Proxy object can be a very powerful tool to extend the
|
||||
|
||||
@ -4,8 +4,8 @@ featured: true
|
||||
snippetIds:
|
||||
- js/s/random-number-in-range
|
||||
- js/s/random-integer-in-range
|
||||
- js/s/random-int-array-in-range
|
||||
- js/s/random-alpha-numeric
|
||||
- js/s/random-integer-array-in-range
|
||||
- js/s/random-alphanumeric
|
||||
- js/s/random-boolean
|
||||
- js/s/random-hex-color-code
|
||||
- js/s/random-gauss
|
||||
|
||||
@ -2,19 +2,19 @@ slug: js/url
|
||||
name: URLs in JavaScript
|
||||
featured: true
|
||||
snippetIds:
|
||||
- articles/s/javascript-modify-url-without-reload
|
||||
- articles/s/js-construct-url
|
||||
- articles/s/js-edit-url-params
|
||||
- articles/s/javascript-reload-page
|
||||
- articles/s/js-window-location-cheatsheet
|
||||
- js/s/modify-url-without-reload
|
||||
- js/s/construct-url
|
||||
- js/s/edit-url-params
|
||||
- js/s/reload-page
|
||||
- js/s/window-location-cheatsheet
|
||||
- js/s/current-url
|
||||
- js/s/https-redirect
|
||||
- js/s/get-url-parameters
|
||||
- js/s/get-base-url
|
||||
- js/s/redirect
|
||||
- js/s/redirect-to-url
|
||||
- js/s/is-absolute-url
|
||||
- js/s/is-same-origin
|
||||
- js/s/url-join
|
||||
- js/s/join-url-segments
|
||||
splash: pots-and-plants.png
|
||||
description: >-
|
||||
URLs are an integral part of the web, which makes working with them an
|
||||
|
||||
@ -2,7 +2,7 @@ slug: js/web-storage-essentials
|
||||
name: Web Storage Essentials
|
||||
featured: true
|
||||
snippetIds:
|
||||
- articles/s/cookies-local-storage-session
|
||||
- js/s/cookies-local-storage-session
|
||||
- js/s/parse-cookie
|
||||
- js/s/serialize-cookie
|
||||
- js/s/is-local-storage-enabled
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
slug: list
|
||||
slug: snippets
|
||||
name: Code Snippets
|
||||
featured: false
|
||||
splash: laptop-plant.png
|
||||
|
||||
@ -4,9 +4,9 @@ miniName: Rendering
|
||||
parent: react
|
||||
featured: true
|
||||
snippetIds:
|
||||
- articles/s/react-rendering-basics
|
||||
- articles/s/react-rendering-optimization
|
||||
- articles/s/react-rendering-state
|
||||
- react/s/rendering-basics
|
||||
- react/s/rendering-optimization
|
||||
- react/s/rendering-state
|
||||
splash: glasses-comic.png
|
||||
description: >-
|
||||
Understanding of the rendering process is a crucial piece of knowledge when
|
||||
|
||||
@ -4,10 +4,10 @@ shortName: Testing
|
||||
parent: react
|
||||
featured: true
|
||||
snippetIds:
|
||||
- articles/s/testing-stateful-ui-components
|
||||
- articles/s/testing-async-react-components
|
||||
- articles/s/testing-react-portals
|
||||
- articles/s/testing-redux-connected-components
|
||||
- react/s/testing-stateful-ui-components
|
||||
- react/s/testing-async-components
|
||||
- react/s/testing-portals
|
||||
- react/s/testing-redux-connected-components
|
||||
splash: headphones.png
|
||||
description: >-
|
||||
Testing React can be challenging, both in terms of figuring out the correct
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
title: 8 tips for accessible websites
|
||||
shortTitle: Accessibility tips
|
||||
type: story
|
||||
language: javascript
|
||||
language: html
|
||||
tags: [accessibility,webdev]
|
||||
author: chalarangelo
|
||||
cover: accessibility
|
||||
|
||||
@ -1,65 +0,0 @@
|
||||
---
|
||||
title: How can I implement a sleep function in JavaScript?
|
||||
shortTitle: Sleep function
|
||||
type: question
|
||||
language: javascript
|
||||
tags: [date,promise]
|
||||
author: chalarangelo
|
||||
cover: sleepy-cat
|
||||
excerpt: Learn all the different ways you can implement a `sleep()` function in JavaScript.
|
||||
dateModified: 2021-11-06T20:51:47+03:00
|
||||
---
|
||||
|
||||
JavaScript does not come with a `sleep()` function out of the box. That's probably a good idea considering the environments where it runs and the trouble it could cause if used incorrectly. The closest equivalent is the `setTimeout()` function, but there are other, less common ways to implement a function that will pause execution for a specified amount of time.
|
||||
|
||||
### setTimeout
|
||||
|
||||
JavaScript's `setTimeout()` sets a timer which executes some code once the timer expires. Only the code inside the `setTimeout()` callback will execute after the timer expires. This can lead to nesting issues, as well as code executing out of order if you are not careful.
|
||||
|
||||
```js
|
||||
const printNums = () => {
|
||||
console.log(1);
|
||||
setTimeout(() => console.log(2), 500);
|
||||
console.log(3);
|
||||
};
|
||||
|
||||
printNums(); // Logs: 1, 3, 2 (2 logs after 500ms)
|
||||
```
|
||||
|
||||
### Synchronous version
|
||||
|
||||
While strongly discouraged, `Date.prototype.getTime()` can be used inside a `while` loop to pause execution for a set amount of time. You can easily define a synchronous `sleep()` function like this:
|
||||
|
||||
```js
|
||||
const sleepSync = (ms) => {
|
||||
const end = new Date().getTime() + ms;
|
||||
while (new Date().getTime() < end) { /* do nothing */ }
|
||||
}
|
||||
|
||||
const printNums = () => {
|
||||
console.log(1);
|
||||
sleepSync(500);
|
||||
console.log(2);
|
||||
console.log(3);
|
||||
};
|
||||
|
||||
printNums(); // Logs: 1, 2, 3 (2 and 3 log after 500ms)
|
||||
```
|
||||
|
||||
### Asynchronous version
|
||||
|
||||
A less intrusive way to go about implementing a `sleep()` function is to utilize the `async` and `await` keywords added in JavaScript ES6, a `Promise` and `setTimeout()`. Note that the resulting function must be executed in an `async` function and has to be called with `await`:
|
||||
|
||||
```js
|
||||
const sleep = (ms) =>
|
||||
new Promise(resolve => setTimeout(resolve, ms));
|
||||
|
||||
const printNums = async() => {
|
||||
console.log(1);
|
||||
await sleep(500);
|
||||
console.log(2);
|
||||
console.log(3);
|
||||
};
|
||||
|
||||
printNums(); // Logs: 1, 2, 3 (2 and 3 log after 500ms)
|
||||
```
|
||||
@ -19,4 +19,4 @@ a[href]:not([class]) {
|
||||
}
|
||||
```
|
||||
|
||||
As a bonus tip, you can use [the previous tip about selecting any link](/articles/s/css-select-any-link) to further enhance this solution.
|
||||
As a bonus tip, you can use [the previous tip about selecting any link](/css/s/select-any-link) to further enhance this solution.
|
||||
@ -24,7 +24,7 @@ array.includes(3, 3); // false
|
||||
|
||||
### Objects
|
||||
|
||||
Unlike primitive values, you can't use `Array.prototype.includes()` to check if an array includes an object. This comes down to how JavaScript compares values and the fact that [objects are reference types](/articles/s/javascript-pass-by-reference-or-pass-by-value). I highly recommend reading the previous article about [object comparison](/articles/s/javascript-object-comparison), as I won't be going into detail on how to compare objects here.
|
||||
Unlike primitive values, you can't use `Array.prototype.includes()` to check if an array includes an object. This comes down to how JavaScript compares values and the fact that [objects are reference types](/js/s/pass-by-reference-or-pass-by-value). I highly recommend reading the previous article about [object comparison](/js/s/object-comparison), as I won't be going into detail on how to compare objects here.
|
||||
|
||||
Due to this difference between primitive values and objects, you can't use `Array.prototype.includes()` to check if an array includes an object. However, provided you implement a [deep equality function](/js/s/equals), you can use `Array.prototype.some()` to check if any object matches the shape of another object.
|
||||
|
||||
@ -28,4 +28,4 @@ const doubledNumbers = numbers.map(num => num * 2);
|
||||
|
||||
The way I like to distinguish them is by remembering that `Array.prototype.map()` represents a **transformation**, whereas `Array.prototype.forEach()` represents an **iteration**. Hopefully, one of these explanations will click for you and help you remember the difference between the two.
|
||||
|
||||
As a closing note, I would like to remind you that the humble `for` loop can be more efficient in some cases, such as [breaking out of a loop early](/articles/s/javascript-for-loop-early-break). Always pick the right tool for the job, as ES6 has a method for almost every use case.
|
||||
As a closing note, I would like to remind you that the humble `for` loop can be more efficient in some cases, such as [breaking out of a loop early](/js/s/for-loop-early-break). Always pick the right tool for the job, as ES6 has a method for almost every use case.
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user