chunk_id stringlengths 36 36 | source stringclasses 35
values | source_url stringlengths 0 290 | upstream_license stringclasses 1
value | document_id stringlengths 36 36 | chunk_index int64 0 324k | retrieved_at stringclasses 2
values | chunker_version stringclasses 4
values | content_hash stringlengths 15 64 | content stringlengths 50 44.7k | namespace stringclasses 9
values | source_name stringclasses 35
values | raw_text stringlengths 50 44.7k | cleaned_text stringlengths 50 44.7k | tags stringclasses 49
values | collection_name stringclasses 11
values |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
dab7982b-dd36-4985-bbd9-f0bc134d709e | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/euclidean-distance.md | unknown | 984ca5be-421c-485d-8fa0-171675a2b45e | 0 | SemanticChunker@1.0.0 | df8309aa4dbe0c6aa13b9208a06709f5243c973e58bb03593f1c5ce6994a9ffa | ---
title: Calculate the Euclidean distance in JavaScript
language: javascript
tags: [math,algorithm]
cover: ancient-greek-building
excerpt: Use JavaScript's `Math.hypot()` to calculate the Euclidean distance between two points.
listed: true
dateModified: 2023-12-18
--- | unknown | unknown | ---
title: Calculate the Euclidean distance in JavaScript
language: javascript
tags: [math,algorithm]
cover: ancient-greek-building
excerpt: Use JavaScript's `Math.hypot()` to calculate the Euclidean distance between two points.
listed: true
dateModified: 2023-12-18
--- | ---
title: Calculate the Euclidean distance in JavaScript
language: javascript
tags: [math,algorithm]
cover: ancient-greek-building
excerpt: Use JavaScript's `Math.hypot()` to calculate the Euclidean distance between two points.
listed: true
dateModified: 2023-12-18
--- | code_snippets | ||
64745b9e-3ba1-4bd4-a0d8-480604b83e78 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/eslint-refactor-for-in.md | unknown | 9a477676-aa32-41d0-8367-33754bd2255d | 2 | SemanticChunker@1.0.0 | 32be972a7ba5cfec9e8205207531b30411c29347e58d855370fda7f1aa33caba | [`Object.keys()` > `Object.values()`]
## `Object.values()`
`Object.values()` is very similar to `Object.keys()`, but returns the values instead of the keys, which might be what you are really using the keys for:
```js
const data = [3, 4];
// Iterate over the values
Object.values(data).forEach(v => console.log(v));
/... | unknown | unknown | [`Object.keys()` > `Object.values()`]
## `Object.values()`
`Object.values()` is very similar to `Object.keys()`, but returns the values instead of the keys, which might be what you are really using the keys for:
```js
const data = [3, 4];
// Iterate over the values
Object.values(data).forEach(v => console.log(v));
/... | [`Object.keys()` > `Object.values()`]
## `Object.values()`
`Object.values()` is very similar to `Object.keys()`, but returns the values instead of the keys, which might be what you are really using the keys for:
```js
const data = [3, 4];
// Iterate over the values
Object.values(data).forEach(v => console.log(v));
/... | code_snippets | ||
905fa0f7-a897-4a25-9c85-564ee110f803 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/eslint-refactor-for-in.md | unknown | 9a477676-aa32-41d0-8367-33754bd2255d | 1 | SemanticChunker@1.0.0 | 5153eefacc551db37c96eb0191ecbfe8f2e97fae403f22c395ace687f21249b9 | [`Object.keys()`]
## `Object.keys()`
`Object.keys()` has the exact same behavior as a `for...in` loop, so it can be used as a drop-in replacement:
```js
const data = [3, 4];
// Same as for (let k in data) console.log(k)
Object.keys(data).forEach(k => console.log(k));
// 0 1
``` | unknown | unknown | [`Object.keys()`]
## `Object.keys()`
`Object.keys()` has the exact same behavior as a `for...in` loop, so it can be used as a drop-in replacement:
```js
const data = [3, 4];
// Same as for (let k in data) console.log(k)
Object.keys(data).forEach(k => console.log(k));
// 0 1
``` | [`Object.keys()`]
## `Object.keys()`
`Object.keys()` has the exact same behavior as a `for...in` loop, so it can be used as a drop-in replacement:
```js
const data = [3, 4];
// Same as for (let k in data) console.log(k)
Object.keys(data).forEach(k => console.log(k));
// 0 1
``` | code_snippets | ||
c82d6477-9bd3-4371-a80c-24c06387831e | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/eslint-refactor-for-in.md | unknown | 9a477676-aa32-41d0-8367-33754bd2255d | 0 | SemanticChunker@1.0.0 | 16fe3de98e402bf4591284edce01b6a8995c70e214f908d1fab2a6a9ac98390c | ---
title: Refactoring your for...in loops to avoid ESLint warnings
shortTitle: Refactoring for...in loops to avoid ESLint warnings
language: javascript
tags: [array,iterator,eslint]
cover: typing
excerpt: ESLint is a really useful tool, but sometimes it gets in the way. Learn how to refactor code to get rid of a commo... | unknown | unknown | ---
title: Refactoring your for...in loops to avoid ESLint warnings
shortTitle: Refactoring for...in loops to avoid ESLint warnings
language: javascript
tags: [array,iterator,eslint]
cover: typing
excerpt: ESLint is a really useful tool, but sometimes it gets in the way. Learn how to refactor code to get rid of a commo... | ---
title: Refactoring your for...in loops to avoid ESLint warnings
shortTitle: Refactoring for...in loops to avoid ESLint warnings
language: javascript
tags: [array,iterator,eslint]
cover: typing
excerpt: ESLint is a really useful tool, but sometimes it gets in the way. Learn how to refactor code to get rid of a commo... | code_snippets | ||
ce028d80-63b4-4d0d-97bf-b540bdf53b01 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/eslint-refactor-for-in.md | unknown | 9a477676-aa32-41d0-8367-33754bd2255d | 3 | SemanticChunker@1.0.0 | 802cc2ec242925145009ae390bed3d18611d57491b44449c4225d1c60a204f04 | [`Object.keys()` > `Object.entries()`]
## `Object.entries()`
Finally, if you need both key and value, `Object.entries()` has you covered:
```js
const data = [3, 4];
// Iterate over the data, returning key-value pairs
Object.entries(data).forEach(e => console.log(e[0], e[1]));
// [0, 3] [1, 4]
``` | unknown | unknown | [`Object.keys()` > `Object.entries()`]
## `Object.entries()`
Finally, if you need both key and value, `Object.entries()` has you covered:
```js
const data = [3, 4];
// Iterate over the data, returning key-value pairs
Object.entries(data).forEach(e => console.log(e[0], e[1]));
// [0, 3] [1, 4]
``` | [`Object.keys()` > `Object.entries()`]
## `Object.entries()`
Finally, if you need both key and value, `Object.entries()` has you covered:
```js
const data = [3, 4];
// Iterate over the data, returning key-value pairs
Object.entries(data).forEach(e => console.log(e[0], e[1]));
// [0, 3] [1, 4]
``` | code_snippets | ||
1f5db315-8744-49c4-8c4c-c122b1cd43eb | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/eslint-prettier-configuration.md | unknown | a9505e26-076f-49d1-bdb0-d63aea140b25 | 1 | SemanticChunker@1.0.0 | 72c7ab94ed6339e80fef357c2ca4f3c4bcb75d8a854ee934db8b11d588780bf1 | export default [
js.configs.recommended,
eslintConfigPrettier,
{
languageOptions: {
ecmaVersion: 2025,
sourceType: 'module',
globals: { ...globals },
},
rules: {
'no-duplicate-imports': 'error',
'no-useless-computed-key': 'error',
'no-console': 'off',
eqeqeq: ['error', 'smart'],
curly: 'off',
'object-sho... | unknown | unknown | export default [
js.configs.recommended,
eslintConfigPrettier,
{
languageOptions: {
ecmaVersion: 2025,
sourceType: 'module',
globals: { ...globals },
},
rules: {
'no-duplicate-imports': 'error',
'no-useless-computed-key': 'error',
'no-console': 'off',
eqeqeq: ['error', 'smart'],
curly: 'off',
'object-sho... | export default [
js.configs.recommended,
eslintConfigPrettier,
{
languageOptions: {
ecmaVersion: 2025,
sourceType: 'module',
globals: { ...globals },
},
rules: {
'no-duplicate-imports': 'error',
'no-useless-computed-key': 'error',
'no-console': 'off',
eqeqeq: ['error', 'smart'],
curly: 'off',
'object-sho... | code_snippets | ||
27a437f4-2e15-4cf4-a9d3-2d97d56ae5b4 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/eslint-prettier-configuration.md | unknown | a9505e26-076f-49d1-bdb0-d63aea140b25 | 3 | SemanticChunker@1.0.0 | 788fb9c1d4e1ec4a4c422f50714a5ecb907a155a0253ce3bdb39ee25eea68145 | [Package versions > ESLint]
## ESLint
ESLint has a ton of rules and I can't possibly hope to cover them all myself. The defaults are pretty good, but some tweaks are necessary to make it match my taste and the setup most of the teams I work with use.
> [!NOTE]
>
> As I've recently moved to ESLint `9.x.x`, some of th... | unknown | unknown | [Package versions > ESLint]
## ESLint
ESLint has a ton of rules and I can't possibly hope to cover them all myself. The defaults are pretty good, but some tweaks are necessary to make it match my taste and the setup most of the teams I work with use.
> [!NOTE]
>
> As I've recently moved to ESLint `9.x.x`, some of th... | [Package versions > ESLint]
## ESLint
ESLint has a ton of rules and I can't possibly hope to cover them all myself. The defaults are pretty good, but some tweaks are necessary to make it match my taste and the setup most of the teams I work with use.
> [!NOTE]
>
> As I've recently moved to ESLint `9.x.x`, some of th... | code_snippets | ||
3c8f6d6d-930f-4019-af24-7c242ea7f994 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/eslint-prettier-configuration.md | unknown | a9505e26-076f-49d1-bdb0-d63aea140b25 | 0 | SemanticChunker@1.0.0 | f6418f7f41e83a9076ed243559346094ed3da741807d77b745959131d2530b9f | ---
title: ESLint & Prettier configuration
language: javascript
tags: [webdev]
cover: interior-12
excerpt: Instead of reconfiguring ESLint and Prettier every time, I use this configuration to get started quickly.
listed: true
dateModified: 2025-05-28
---
I've been starting a lot of new mini projects lately, and I beli... | unknown | unknown | ---
title: ESLint & Prettier configuration
language: javascript
tags: [webdev]
cover: interior-12
excerpt: Instead of reconfiguring ESLint and Prettier every time, I use this configuration to get started quickly.
listed: true
dateModified: 2025-05-28
---
I've been starting a lot of new mini projects lately, and I beli... | ---
title: ESLint & Prettier configuration
language: javascript
tags: [webdev]
cover: interior-12
excerpt: Instead of reconfiguring ESLint and Prettier every time, I use this configuration to get started quickly.
listed: true
dateModified: 2025-05-28
---
I've been starting a lot of new mini projects lately, and I beli... | code_snippets | ||
5a25a465-54b5-44b1-a37f-ce93853683b1 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/eslint-prettier-configuration.md | unknown | a9505e26-076f-49d1-bdb0-d63aea140b25 | 5 | SemanticChunker@1.0.0 | b99ce3076bbaf2080d6cb836870078c15e5202bc6705c821f50d8da6ef625446 | [ESLint > Rules]
### Rules
- `no-duplicate-imports`: Disallow duplicate imports.
- `no-useless-computed-key`: Disallow unnecessary computed property keys.
- `no-console`: Allow `console` statements (you may want to change this in production).
- `eqeqeq`: Enforce strict equality checks (`===` and `!==`).
- `curly`: Di... | unknown | unknown | [ESLint > Rules]
### Rules
- `no-duplicate-imports`: Disallow duplicate imports.
- `no-useless-computed-key`: Disallow unnecessary computed property keys.
- `no-console`: Allow `console` statements (you may want to change this in production).
- `eqeqeq`: Enforce strict equality checks (`===` and `!==`).
- `curly`: Di... | [ESLint > Rules]
### Rules
- `no-duplicate-imports`: Disallow duplicate imports.
- `no-useless-computed-key`: Disallow unnecessary computed property keys.
- `no-console`: Allow `console` statements (you may want to change this in production).
- `eqeqeq`: Enforce strict equality checks (`===` and `!==`).
- `curly`: Di... | code_snippets | ||
9e6d99e0-3dc6-448d-bd26-fcaa7bb0926c | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/eslint-prettier-configuration.md | unknown | a9505e26-076f-49d1-bdb0-d63aea140b25 | 4 | SemanticChunker@1.0.0 | 1ff18f25dc01010b1b0ea48ec08c655a7da277e688c6662ff0a932f116821112 | [ESLint > General setup]
### General setup
- `js.configs.recommended`: Use the recommended ESLint configuration.
- `eslintConfigPrettier`: Compatibility with Prettier.
- `languageOptions`:
- `ecmaVersion`: Set to `2025` to use the latest JavaScript features.
- `sourceType`: Set to `module` to use ESM.
- `globals`:... | unknown | unknown | [ESLint > General setup]
### General setup
- `js.configs.recommended`: Use the recommended ESLint configuration.
- `eslintConfigPrettier`: Compatibility with Prettier.
- `languageOptions`:
- `ecmaVersion`: Set to `2025` to use the latest JavaScript features.
- `sourceType`: Set to `module` to use ESM.
- `globals`:... | [ESLint > General setup]
### General setup
- `js.configs.recommended`: Use the recommended ESLint configuration.
- `eslintConfigPrettier`: Compatibility with Prettier.
- `languageOptions`:
- `ecmaVersion`: Set to `2025` to use the latest JavaScript features.
- `sourceType`: Set to `module` to use ESM.
- `globals`:... | code_snippets | ||
bfb94b34-b773-4234-8d69-6e1d480728a3 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/eslint-prettier-configuration.md | unknown | a9505e26-076f-49d1-bdb0-d63aea140b25 | 2 | SemanticChunker@1.0.0 | bd3115108a80d9dfb21cf3b958ded9dbaa692b97e08ed3361d7888772dd40db9 | [Package versions]
## Package versions
This setup is optimized for **ESLint & Prettier**, and revised for versions `9.24.0` and `3.5.3` of `eslint` and `prettier`, respectively. I also use `eslint-config-prettier` for compatibility between the two tools.
Finally, you'll notice an imported `globals.js` file. I'm not ... | unknown | unknown | [Package versions]
## Package versions
This setup is optimized for **ESLint & Prettier**, and revised for versions `9.24.0` and `3.5.3` of `eslint` and `prettier`, respectively. I also use `eslint-config-prettier` for compatibility between the two tools.
Finally, you'll notice an imported `globals.js` file. I'm not ... | [Package versions]
## Package versions
This setup is optimized for **ESLint & Prettier**, and revised for versions `9.24.0` and `3.5.3` of `eslint` and `prettier`, respectively. I also use `eslint-config-prettier` for compatibility between the two tools.
Finally, you'll notice an imported `globals.js` file. I'm not ... | code_snippets | ||
fdb1d3e6-3b63-4d7b-a9cb-72cd32fc0582 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/eslint-prettier-configuration.md | unknown | a9505e26-076f-49d1-bdb0-d63aea140b25 | 6 | SemanticChunker@1.0.0 | f6a65dda82953668713e527159c600898566b4246d486573bcc602925f34df78 | [Package versions > Prettier]
## Prettier
Prettier's setup is really straightforward:
- `printWidth`: I don't like long lines of code, hence the `80` character limit.
- `arrowParens`: I prefer to avoid parentheses in arrow function if possible.
- `singleQuote`: Single quotes seem less cluttered to me.
- `tabWidth`: ... | unknown | unknown | [Package versions > Prettier]
## Prettier
Prettier's setup is really straightforward:
- `printWidth`: I don't like long lines of code, hence the `80` character limit.
- `arrowParens`: I prefer to avoid parentheses in arrow function if possible.
- `singleQuote`: Single quotes seem less cluttered to me.
- `tabWidth`: ... | [Package versions > Prettier]
## Prettier
Prettier's setup is really straightforward:
- `printWidth`: I don't like long lines of code, hence the `80` character limit.
- `arrowParens`: I prefer to avoid parentheses in arrow function if possible.
- `singleQuote`: Single quotes seem less cluttered to me.
- `tabWidth`: ... | code_snippets | ||
73f5ba6d-864b-438a-9e1c-d9ce9bc825eb | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/escape-reg-exp.md | unknown | a6c0ecde-4fb3-44b2-af77-8e7f329792b2 | 0 | SemanticChunker@1.0.0 | 604f6b98a04ab181f91e2d0ede0e78d89d15774abbc34799cedfd011625b6e14 | ---
title: Escape a regular expression in JavaScript
shortTitle: Escape RegExp
language: javascript
tags: [string,regexp]
cover: frog-blue-flower
excerpt: Learn how to escape a string to use in a regular expression.
listed: true
dateModified: 2024-05-29
---
Regular expressions are a powerful tool for pattern matching ... | unknown | unknown | ---
title: Escape a regular expression in JavaScript
shortTitle: Escape RegExp
language: javascript
tags: [string,regexp]
cover: frog-blue-flower
excerpt: Learn how to escape a string to use in a regular expression.
listed: true
dateModified: 2024-05-29
---
Regular expressions are a powerful tool for pattern matching ... | ---
title: Escape a regular expression in JavaScript
shortTitle: Escape RegExp
language: javascript
tags: [string,regexp]
cover: frog-blue-flower
excerpt: Learn how to escape a string to use in a regular expression.
listed: true
dateModified: 2024-05-29
---
Regular expressions are a powerful tool for pattern matching ... | code_snippets | ||
0c5fca5c-adef-4d72-a0b3-0de91dd7e4fe | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/escape-unescape-html.md | unknown | 2bac09cb-43ba-44a4-ba22-7a17993f4aa8 | 0 | SemanticChunker@1.0.0 | d06cdaf9fa2bbfcff83758857ea757245e1bcd3aeaddfb6aead1704e4978cf7d | ---
title: Escape or unescape HTML using JavaScript
shortTitle: Escape or unescape HTML
language: javascript
tags: [string,regexp]
cover: above-the-rocks
excerpt: Learn how to convert text into HTML-safe strings and vice versa using JavaScript.
listed: true
dateModified: 2024-02-18
---
Escaping and unescaping HTML is ... | unknown | unknown | ---
title: Escape or unescape HTML using JavaScript
shortTitle: Escape or unescape HTML
language: javascript
tags: [string,regexp]
cover: above-the-rocks
excerpt: Learn how to convert text into HTML-safe strings and vice versa using JavaScript.
listed: true
dateModified: 2024-02-18
---
Escaping and unescaping HTML is ... | ---
title: Escape or unescape HTML using JavaScript
shortTitle: Escape or unescape HTML
language: javascript
tags: [string,regexp]
cover: above-the-rocks
excerpt: Learn how to convert text into HTML-safe strings and vice versa using JavaScript.
listed: true
dateModified: 2024-02-18
---
Escaping and unescaping HTML is ... | code_snippets | ||
1aa0d644-e586-40ca-8f26-3022a5a433ff | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/escape-unescape-html.md | unknown | 2bac09cb-43ba-44a4-ba22-7a17993f4aa8 | 1 | SemanticChunker@1.0.0 | 3cf4c35abdcc5f997d63fec7f19311179c9468e2889fd55df78af64b08a7f6d1 | [Escape HTML]
## Escape HTML
Using `String.prototype.replace()` with a **regular expression** that matches the characters that need to be escaped, you can replace each character instance with its associated escaped character using a dictionary object.
```js
const escapeHTML = str =>
str.replace(
/[&<>'"]/g,
tag =... | unknown | unknown | [Escape HTML]
## Escape HTML
Using `String.prototype.replace()` with a **regular expression** that matches the characters that need to be escaped, you can replace each character instance with its associated escaped character using a dictionary object.
```js
const escapeHTML = str =>
str.replace(
/[&<>'"]/g,
tag =... | [Escape HTML]
## Escape HTML
Using `String.prototype.replace()` with a **regular expression** that matches the characters that need to be escaped, you can replace each character instance with its associated escaped character using a dictionary object.
```js
const escapeHTML = str =>
str.replace(
/[&<>'"]/g,
tag =... | code_snippets | ||
3fb435d4-03b0-4b7b-9a68-f5952e857e78 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/escape-unescape-html.md | unknown | 2bac09cb-43ba-44a4-ba22-7a17993f4aa8 | 2 | SemanticChunker@1.0.0 | a5ff60466e28a502df35a74f04121317d2af36f3cda940115c0cfe186196961d | [Escape HTML > Unescape HTML]
## Unescape HTML
Unescaping HTML is the reverse operation of escaping HTML. Again, using `String.prototype.replace()` with an appropriate regular expression should suffice.
```js
const unescapeHTML = str =>
str.replace(
/&|<|>|'|"/g,
tag =>
({
'&': '&',
'<... | unknown | unknown | [Escape HTML > Unescape HTML]
## Unescape HTML
Unescaping HTML is the reverse operation of escaping HTML. Again, using `String.prototype.replace()` with an appropriate regular expression should suffice.
```js
const unescapeHTML = str =>
str.replace(
/&|<|>|'|"/g,
tag =>
({
'&': '&',
'<... | [Escape HTML > Unescape HTML]
## Unescape HTML
Unescaping HTML is the reverse operation of escaping HTML. Again, using `String.prototype.replace()` with an appropriate regular expression should suffice.
```js
const unescapeHTML = str =>
str.replace(
/&|<|>|'|"/g,
tag =>
({
'&': '&',
'<... | code_snippets | ||
0b68ba7e-7fe5-4308-92de-d35992399594 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/equality.md | unknown | f6c0ad4f-5414-4351-988e-e59e463a6002 | 1 | SemanticChunker@1.0.0 | f362cd308e71ebcccf40dad3575b2e3f7113c5dd21744ebe40f6583964449aa4 | As you can see from the examples above, using the triple equals (`===`) operator is far more predictable and intuitive than the double equals (`==`) operator. Therefore, we recommend you use the triple equals (`===`) operator for most cases, unless you are entirely certain you want type coercion to be applied to the co... | unknown | unknown | As you can see from the examples above, using the triple equals (`===`) operator is far more predictable and intuitive than the double equals (`==`) operator. Therefore, we recommend you use the triple equals (`===`) operator for most cases, unless you are entirely certain you want type coercion to be applied to the co... | As you can see from the examples above, using the triple equals (`===`) operator is far more predictable and intuitive than the double equals (`==`) operator. Therefore, we recommend you use the triple equals (`===`) operator for most cases, unless you are entirely certain you want type coercion to be applied to the co... | code_snippets | ||
8db50ae0-1a03-475a-a52b-c9e27109305c | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/equality.md | unknown | f6c0ad4f-5414-4351-988e-e59e463a6002 | 0 | SemanticChunker@1.0.0 | c22026635a1aeb690624e85c757e9c80c7eb2f913cefa4079da9efa4c4d06618 | ---
title: What is the difference between JavaScript's equality operators?
shortTitle: JavaScript equality operators
language: javascript
tags: [type,comparison]
cover: beach-pineapple
excerpt: Learn all you need to know about the differences between JavaScript's double equals and triple equals operators.
listed: true
... | unknown | unknown | ---
title: What is the difference between JavaScript's equality operators?
shortTitle: JavaScript equality operators
language: javascript
tags: [type,comparison]
cover: beach-pineapple
excerpt: Learn all you need to know about the differences between JavaScript's double equals and triple equals operators.
listed: true
... | ---
title: What is the difference between JavaScript's equality operators?
shortTitle: JavaScript equality operators
language: javascript
tags: [type,comparison]
cover: beach-pineapple
excerpt: Learn all you need to know about the differences between JavaScript's double equals and triple equals operators.
listed: true
... | code_snippets | ||
5336b3cb-3649-4f61-aeac-d53336e604a6 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/enum.md | unknown | e893769c-a102-4e4f-9683-c5b0845aafd1 | 0 | SemanticChunker@1.0.0 | 315b0f6d44382a7dc3ddbd0d6eea0d67d219aa2f26d00a7a5e78abd57038294b | ---
title: How can I define an enum in JavaScript?
shortTitle: Enum implementation
language: javascript
tags: [object,class,symbol,generator]
cover: book-chair
excerpt: Enums are part of TypeScript, but what about defining enums in plain old JavaScript? Here are a few way you can do that.
listed: true
dateModified: 202... | unknown | unknown | ---
title: How can I define an enum in JavaScript?
shortTitle: Enum implementation
language: javascript
tags: [object,class,symbol,generator]
cover: book-chair
excerpt: Enums are part of TypeScript, but what about defining enums in plain old JavaScript? Here are a few way you can do that.
listed: true
dateModified: 202... | ---
title: How can I define an enum in JavaScript?
shortTitle: Enum implementation
language: javascript
tags: [object,class,symbol,generator]
cover: book-chair
excerpt: Enums are part of TypeScript, but what about defining enums in plain old JavaScript? Here are a few way you can do that.
listed: true
dateModified: 202... | code_snippets | ||
b32b3ae0-e345-4b12-b40b-7769e7c08853 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/enum.md | unknown | e893769c-a102-4e4f-9683-c5b0845aafd1 | 1 | SemanticChunker@1.0.0 | b6a831ae2e605692c0e1a347318e7894b9c08696ddfd4753000999958bf0fda3 | Using an ES6 symbol is the obvious solution here, as it will not pollute the result of `Object.keys()` and it will never conflict with any values in the enum. Taking this one step further, `Symbol.iterator` would be a great choice as it would allow for the enum to be considered iterable and make it even more useful. Pu... | unknown | unknown | Using an ES6 symbol is the obvious solution here, as it will not pollute the result of `Object.keys()` and it will never conflict with any values in the enum. Taking this one step further, `Symbol.iterator` would be a great choice as it would allow for the enum to be considered iterable and make it even more useful. Pu... | Using an ES6 symbol is the obvious solution here, as it will not pollute the result of `Object.keys()` and it will never conflict with any values in the enum. Taking this one step further, `Symbol.iterator` would be a great choice as it would allow for the enum to be considered iterable and make it even more useful. Pu... | code_snippets | ||
345d64a7-e8b4-47b2-b8cb-78787e93ff83 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/encode-decode-strings-with-base64.md | unknown | 65da1162-7d0f-4f3d-949b-ec51bbaba1cf | 3 | SemanticChunker@1.0.0 | a6177d454382dfc97b6692cd4d043316323cc06c92b019954b9bdb2dfb24fd10 | [Encode a string with Base64 > Compatibility & older Node.js versions]
## Compatibility & older Node.js versions
<baseline-support featureId="base64encodedecode">
</baseline-support>
Luckily, both `btoa()` and `atob()` are supported in all modern browsers and **Node.js since version 16.0.0**.
If, however, you need ... | unknown | unknown | [Encode a string with Base64 > Compatibility & older Node.js versions]
## Compatibility & older Node.js versions
<baseline-support featureId="base64encodedecode">
</baseline-support>
Luckily, both `btoa()` and `atob()` are supported in all modern browsers and **Node.js since version 16.0.0**.
If, however, you need ... | [Encode a string with Base64 > Compatibility & older Node.js versions]
## Compatibility & older Node.js versions
<baseline-support featureId="base64encodedecode">
</baseline-support>
Luckily, both `btoa()` and `atob()` are supported in all modern browsers and **Node.js since version 16.0.0**.
If, however, you need ... | code_snippets | ||
a920cf4a-5109-4915-b59f-72d26897dbdb | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/encode-decode-strings-with-base64.md | unknown | 65da1162-7d0f-4f3d-949b-ec51bbaba1cf | 2 | SemanticChunker@1.0.0 | 00a244522284cc3d47f2d96a2ddf53617d11f40f429e696194050d38f516a8ec | [Encode a string with Base64 > Decode a Base64 encoded string]
## Decode a Base64 encoded string
Conversely, the `atob()` method decodes a string of data which has been encoded using base-64 encoding.
```js
const stringToDecode = 'Zm9vYmFy';
const decodedString = atob(stringToDecode); // 'foobar'
``` | unknown | unknown | [Encode a string with Base64 > Decode a Base64 encoded string]
## Decode a Base64 encoded string
Conversely, the `atob()` method decodes a string of data which has been encoded using base-64 encoding.
```js
const stringToDecode = 'Zm9vYmFy';
const decodedString = atob(stringToDecode); // 'foobar'
``` | [Encode a string with Base64 > Decode a Base64 encoded string]
## Decode a Base64 encoded string
Conversely, the `atob()` method decodes a string of data which has been encoded using base-64 encoding.
```js
const stringToDecode = 'Zm9vYmFy';
const decodedString = atob(stringToDecode); // 'foobar'
``` | code_snippets | ||
d227e529-f014-4bb2-b1be-f69dcb29a620 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/encode-decode-strings-with-base64.md | unknown | 65da1162-7d0f-4f3d-949b-ec51bbaba1cf | 1 | SemanticChunker@1.0.0 | f3a43fda62bcd2e450410f4f30af9326127a5728383837f4eec59e40ffd7b4d6 | [Encode a string with Base64]
## Encode a string with Base64
The `btoa()` method creates a base-64 encoded string from a String object in which each character in the string is treated as a byte of binary data.
```js
const stringToEncode = 'foobar';
const encodedString = btoa(stringToEncode); // 'Zm9vYmFy'
``` | unknown | unknown | [Encode a string with Base64]
## Encode a string with Base64
The `btoa()` method creates a base-64 encoded string from a String object in which each character in the string is treated as a byte of binary data.
```js
const stringToEncode = 'foobar';
const encodedString = btoa(stringToEncode); // 'Zm9vYmFy'
``` | [Encode a string with Base64]
## Encode a string with Base64
The `btoa()` method creates a base-64 encoded string from a String object in which each character in the string is treated as a byte of binary data.
```js
const stringToEncode = 'foobar';
const encodedString = btoa(stringToEncode); // 'Zm9vYmFy'
``` | code_snippets | ||
da88d273-9002-4005-832b-7b6b658014e4 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/encode-decode-strings-with-base64.md | unknown | 65da1162-7d0f-4f3d-949b-ec51bbaba1cf | 0 | SemanticChunker@1.0.0 | b42f102d7f140cd56000d3b37dbe9dc1a17f53a3da0497fb6eab2bc0bb39f37f | ---
title: How to encode and decode strings with Base64 in JavaScript
shortTitle: Encode & decode strings with base64
language: javascript
tags: [string]
excerpt: Encode and decode strings with Base64 in JavaScript easily with these code snippets.
cover: thread
listed: true
dateModified: 2023-12-25
---
Converting stri... | unknown | unknown | ---
title: How to encode and decode strings with Base64 in JavaScript
shortTitle: Encode & decode strings with base64
language: javascript
tags: [string]
excerpt: Encode and decode strings with Base64 in JavaScript easily with these code snippets.
cover: thread
listed: true
dateModified: 2023-12-25
---
Converting stri... | ---
title: How to encode and decode strings with Base64 in JavaScript
shortTitle: Encode & decode strings with base64
language: javascript
tags: [string]
excerpt: Encode and decode strings with Base64 in JavaScript easily with these code snippets.
cover: thread
listed: true
dateModified: 2023-12-25
---
Converting stri... | code_snippets | ||
17f2199f-fc1e-4ec6-a814-81cbf5332016 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/encodeuri-encodeuricomponent.md | unknown | a5aaf08b-9ec5-4264-bd91-12f9032236eb | 1 | SemanticChunker@1.0.0 | 6a56056f561bf468328c5977a0d8f93a78ccc165c65d28a9b318403f57353d77 | [encodeURIComponent()]
## encodeURIComponent()
The [`encodeURIComponent()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) function encodes everything in the given string, except `A-Z a-z 0-9 - _ . ! ~ * ' ( )`. You should use this function if the string you are e... | unknown | unknown | [encodeURIComponent()]
## encodeURIComponent()
The [`encodeURIComponent()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) function encodes everything in the given string, except `A-Z a-z 0-9 - _ . ! ~ * ' ( )`. You should use this function if the string you are e... | [encodeURIComponent()]
## encodeURIComponent()
The [`encodeURIComponent()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) function encodes everything in the given string, except `A-Z a-z 0-9 - _ . ! ~ * ' ( )`. You should use this function if the string you are e... | code_snippets | ||
87fa0489-5da9-47d3-8c05-ee104f7e137a | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/encodeuri-encodeuricomponent.md | unknown | a5aaf08b-9ec5-4264-bd91-12f9032236eb | 2 | SemanticChunker@1.0.0 | 0ab8c6f1f8ddb7e5da0cca126eab239303aae952c34b03b903443360228dcc1c | [encodeURIComponent() > encodeURI()]
## encodeURI()
The [`encodeURI()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI) function encodes everything in the given string, except `A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #`. You should use this function if the string ... | unknown | unknown | [encodeURIComponent() > encodeURI()]
## encodeURI()
The [`encodeURI()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI) function encodes everything in the given string, except `A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #`. You should use this function if the string ... | [encodeURIComponent() > encodeURI()]
## encodeURI()
The [`encodeURI()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI) function encodes everything in the given string, except `A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #`. You should use this function if the string ... | code_snippets | ||
f71fc4b5-cdb3-459c-92bc-aed65b58fb90 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/encodeuri-encodeuricomponent.md | unknown | a5aaf08b-9ec5-4264-bd91-12f9032236eb | 0 | SemanticChunker@1.0.0 | 1b6917ae76ab97a0c771af12dbb9a7e28ed4bacfc3f3d5276d9d2eb0264c359b | ---
title: What is the difference between encodeURI() and encodeURIComponent() in JavaScript?
shortTitle: encodeURI() vs encodeURIComponent()
language: javascript
tags: [browser]
cover: laptop-view
excerpt: JavaScript provides two methods for encoding characters to URL-safe strings. Do you know when to use each one?
li... | unknown | unknown | ---
title: What is the difference between encodeURI() and encodeURIComponent() in JavaScript?
shortTitle: encodeURI() vs encodeURIComponent()
language: javascript
tags: [browser]
cover: laptop-view
excerpt: JavaScript provides two methods for encoding characters to URL-safe strings. Do you know when to use each one?
li... | ---
title: What is the difference between encodeURI() and encodeURIComponent() in JavaScript?
shortTitle: encodeURI() vs encodeURIComponent()
language: javascript
tags: [browser]
cover: laptop-view
excerpt: JavaScript provides two methods for encoding characters to URL-safe strings. Do you know when to use each one?
li... | code_snippets | ||
1f60fa05-72f8-47f9-84bd-c7cfdb2c020a | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/empty-array.md | unknown | dcd198d7-711c-4793-8e49-e1853bd7699c | 4 | SemanticChunker@1.0.0 | 3c1e6880bcaa6d0e6b5ac1f22c711bf19250e18713e9e034387258619e983d27 | [Assign it to an empty array > Use Array.prototype.pop()]
## Use Array.prototype.pop()
Last but not least, using `Array.prototype.pop()` is another, more old-fashioned option. It's generally more verbose and less performant, so I'd rather use one of the previous methods instead.
```js
let a = [1, 2, 3, 4];
while (a.... | unknown | unknown | [Assign it to an empty array > Use Array.prototype.pop()]
## Use Array.prototype.pop()
Last but not least, using `Array.prototype.pop()` is another, more old-fashioned option. It's generally more verbose and less performant, so I'd rather use one of the previous methods instead.
```js
let a = [1, 2, 3, 4];
while (a.... | [Assign it to an empty array > Use Array.prototype.pop()]
## Use Array.prototype.pop()
Last but not least, using `Array.prototype.pop()` is another, more old-fashioned option. It's generally more verbose and less performant, so I'd rather use one of the previous methods instead.
```js
let a = [1, 2, 3, 4];
while (a.... | code_snippets | ||
73af2b24-fbbb-4137-bbdf-76be93e6b9b7 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/empty-array.md | unknown | dcd198d7-711c-4793-8e49-e1853bd7699c | 3 | SemanticChunker@1.0.0 | 17fccafea7a7bae74bc261ff121e1f93e217f4f8ea577654e35bd82d9347fce4 | [Assign it to an empty array > Use Array.prototype.splice()]
## Use Array.prototype.splice()
`Array.prototype.splice()` can also be a useful alternative when trying to empty an array. While it has no other downsides compared to the previous method, it doesn't seem to perform as well, so that might be something to con... | unknown | unknown | [Assign it to an empty array > Use Array.prototype.splice()]
## Use Array.prototype.splice()
`Array.prototype.splice()` can also be a useful alternative when trying to empty an array. While it has no other downsides compared to the previous method, it doesn't seem to perform as well, so that might be something to con... | [Assign it to an empty array > Use Array.prototype.splice()]
## Use Array.prototype.splice()
`Array.prototype.splice()` can also be a useful alternative when trying to empty an array. While it has no other downsides compared to the previous method, it doesn't seem to perform as well, so that might be something to con... | code_snippets | ||
9b70164e-00f8-4e42-bba0-f52f44ded63d | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/empty-array.md | unknown | dcd198d7-711c-4793-8e49-e1853bd7699c | 0 | SemanticChunker@1.0.0 | 4f7b5fcbfdcb7ab19d11b10224f9c843a10cbad959f9b7f70038dbf6a1c38601 | ---
title: How do I empty an array in JavaScript?
shortTitle: Empty an array
language: javascript
tags: [array]
cover: coconuts
excerpt: You can use a lot of different techniques to empty an array in JavaScript. See which ones best suits your needs with this quick guide.
listed: true
dateModified: 2021-06-12
---
When ... | unknown | unknown | ---
title: How do I empty an array in JavaScript?
shortTitle: Empty an array
language: javascript
tags: [array]
cover: coconuts
excerpt: You can use a lot of different techniques to empty an array in JavaScript. See which ones best suits your needs with this quick guide.
listed: true
dateModified: 2021-06-12
---
When ... | ---
title: How do I empty an array in JavaScript?
shortTitle: Empty an array
language: javascript
tags: [array]
cover: coconuts
excerpt: You can use a lot of different techniques to empty an array in JavaScript. See which ones best suits your needs with this quick guide.
listed: true
dateModified: 2021-06-12
---
When ... | code_snippets | ||
d9c9dc31-9195-4309-a12c-2359d37ce33f | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/empty-array.md | unknown | dcd198d7-711c-4793-8e49-e1853bd7699c | 2 | SemanticChunker@1.0.0 | 1fb02075f209616fe89370a1663d2eafd1dc5ff6e2db7018ad782c035dfcce7f | [Assign it to an empty array > Set its length to 0]
## Set its length to 0
A better option is to set the `length` of the array to `0`. This option is also pretty fast and has the additional benefit of working for `const` variables.
```js
let a = [1, 2, 3, 4];
a.length = 0;
``` | unknown | unknown | [Assign it to an empty array > Set its length to 0]
## Set its length to 0
A better option is to set the `length` of the array to `0`. This option is also pretty fast and has the additional benefit of working for `const` variables.
```js
let a = [1, 2, 3, 4];
a.length = 0;
``` | [Assign it to an empty array > Set its length to 0]
## Set its length to 0
A better option is to set the `length` of the array to `0`. This option is also pretty fast and has the additional benefit of working for `const` variables.
```js
let a = [1, 2, 3, 4];
a.length = 0;
``` | code_snippets | ||
e4608fe8-2c2b-488f-bef1-42f33feaa55c | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/empty-array.md | unknown | dcd198d7-711c-4793-8e49-e1853bd7699c | 1 | SemanticChunker@1.0.0 | 4d06fa6ea83853b9604ec70ab09fdc5e70485f243ef45cce23bb578142f6bff8 | [Assign it to an empty array]
## Assign it to an empty array
You can assign your variable to an empty array (`[]`) in order to clear it. While this option is rather fast, you should be mindful of references to the original array, as they will remain unchanged. Moreover, it doesn't work for arrays declared as `const`.... | unknown | unknown | [Assign it to an empty array]
## Assign it to an empty array
You can assign your variable to an empty array (`[]`) in order to clear it. While this option is rather fast, you should be mindful of references to the original array, as they will remain unchanged. Moreover, it doesn't work for arrays declared as `const`.... | [Assign it to an empty array]
## Assign it to an empty array
You can assign your variable to an empty array (`[]`) in order to clear it. While this option is rather fast, you should be mindful of references to the original array, as they will remain unchanged. Moreover, it doesn't work for arrays declared as `const`.... | code_snippets | ||
fe87fd26-deda-410f-9fe2-fa3a6851bbf3 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/fast-remove-array-element.md | unknown | 8bd15986-707f-4999-b542-1568e2810542 | 0 | SemanticChunker@1.0.0 | 7e2f6d33b027bac1a59be6fdbf6ab518cb876558de48fde9d4e43e1bc0496a81 | ---
title: Faster element removal in unordered JavaScript arrays
shortTitle: Faster element removal in unordered arrays
language: javascript
tags: [array]
cover: purple-flower-bunch
excerpt: Are you performing a lot of array operations? Maybe element removal is a performance bottleneck you can avoid.
listed: true
dateM... | unknown | unknown | ---
title: Faster element removal in unordered JavaScript arrays
shortTitle: Faster element removal in unordered arrays
language: javascript
tags: [array]
cover: purple-flower-bunch
excerpt: Are you performing a lot of array operations? Maybe element removal is a performance bottleneck you can avoid.
listed: true
dateM... | ---
title: Faster element removal in unordered JavaScript arrays
shortTitle: Faster element removal in unordered arrays
language: javascript
tags: [array]
cover: purple-flower-bunch
excerpt: Are you performing a lot of array operations? Maybe element removal is a performance bottleneck you can avoid.
listed: true
dateM... | code_snippets | ||
04d9d402-75ed-4538-9efb-0b66e9ecdca4 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/event-loop-explained.md | unknown | 06b739fe-0126-4e65-98f9-2b9dd8d6c062 | 1 | SemanticChunker@1.0.0 | 27847dde1b91068bd714655d95d37bb9161f7809c1a5f5c4c64b5a02415d142a | [The Call Stack]
## The Call Stack
The **Call Stack** is a data structure that keeps track of the execution of JavaScript code. As the name suggests, it's a stack, thus a LIFO (Last In, First Out) data structure in memory. Each function that's executed is represented as a frame in the Call Stack and placed on top of ... | unknown | unknown | [The Call Stack]
## The Call Stack
The **Call Stack** is a data structure that keeps track of the execution of JavaScript code. As the name suggests, it's a stack, thus a LIFO (Last In, First Out) data structure in memory. Each function that's executed is represented as a frame in the Call Stack and placed on top of ... | [The Call Stack]
## The Call Stack
The **Call Stack** is a data structure that keeps track of the execution of JavaScript code. As the name suggests, it's a stack, thus a LIFO (Last In, First Out) data structure in memory. Each function that's executed is represented as a frame in the Call Stack and placed on top of ... | code_snippets | ||
1fd722f3-cdf6-4c1c-8a1f-b98f2d7cff14 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/event-loop-explained.md | unknown | 06b739fe-0126-4e65-98f9-2b9dd8d6c062 | 7 | SemanticChunker@1.0.0 | 0a3af27a30b3cc99393444aee9dc4e7427a71c363140805f683c2fc286830682 | [The Call Stack > Summary]
## Summary
- The **Event Loop** is responsible for executing the JavaScript code. It first evaluates and executes the script, then processes **Tasks** and **Microtasks**.
- **Tasks** and **Microtasks** are scheduled, synchronous blocks of code. They are executed one at a time, and are place... | unknown | unknown | [The Call Stack > Summary]
## Summary
- The **Event Loop** is responsible for executing the JavaScript code. It first evaluates and executes the script, then processes **Tasks** and **Microtasks**.
- **Tasks** and **Microtasks** are scheduled, synchronous blocks of code. They are executed one at a time, and are place... | [The Call Stack > Summary]
## Summary
- The **Event Loop** is responsible for executing the JavaScript code. It first evaluates and executes the script, then processes **Tasks** and **Microtasks**.
- **Tasks** and **Microtasks** are scheduled, synchronous blocks of code. They are executed one at a time, and are place... | code_snippets | ||
6cc33689-3d36-456d-8646-6838bd82ab8e | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/event-loop-explained.md | unknown | 06b739fe-0126-4e65-98f9-2b9dd8d6c062 | 5 | SemanticChunker@1.0.0 | a618e94372ac289f33161631a8d022460cad03b53ffaea343f7828cc7808cfb2 | [The Call Stack > A practical example]
## A practical example
To better understand the Event Loop, let's look at a practical example, incorporating all of the above concepts:
```js
console.log('Script start');
setTimeout(() => console.log('setTimeout()'), 0);
Promise.resolve()
.then(() => console.log('Promise.the... | unknown | unknown | [The Call Stack > A practical example]
## A practical example
To better understand the Event Loop, let's look at a practical example, incorporating all of the above concepts:
```js
console.log('Script start');
setTimeout(() => console.log('setTimeout()'), 0);
Promise.resolve()
.then(() => console.log('Promise.the... | [The Call Stack > A practical example]
## A practical example
To better understand the Event Loop, let's look at a practical example, incorporating all of the above concepts:
```js
console.log('Script start');
setTimeout(() => console.log('setTimeout()'), 0);
Promise.resolve()
.then(() => console.log('Promise.the... | code_snippets | ||
6d263aaf-3844-404d-9555-02cc7319d83d | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/event-loop-explained.md | unknown | 06b739fe-0126-4e65-98f9-2b9dd8d6c062 | 3 | SemanticChunker@1.0.0 | 9594cb75e3d5307f340a8820b3b737fc68c0815e9b1185431bfe282ccb134dfe | [The Call Stack > Microtasks and the Microtask Queue]
## Microtasks and the Microtask Queue
**Microtasks** are similar to Tasks in that they're scheduled, synchronous blocks of code with exclusive access to the Call Stack while executing. Additionally, they are stored in their own FIFO (First In, First Out) data stru... | unknown | unknown | [The Call Stack > Microtasks and the Microtask Queue]
## Microtasks and the Microtask Queue
**Microtasks** are similar to Tasks in that they're scheduled, synchronous blocks of code with exclusive access to the Call Stack while executing. Additionally, they are stored in their own FIFO (First In, First Out) data stru... | [The Call Stack > Microtasks and the Microtask Queue]
## Microtasks and the Microtask Queue
**Microtasks** are similar to Tasks in that they're scheduled, synchronous blocks of code with exclusive access to the Call Stack while executing. Additionally, they are stored in their own FIFO (First In, First Out) data stru... | code_snippets | ||
ab1fc083-43c6-46bd-8de9-18c2b54604ec | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/event-loop-explained.md | unknown | 06b739fe-0126-4e65-98f9-2b9dd8d6c062 | 6 | SemanticChunker@1.0.0 | fe5f8d99ecd791506ae70b9fca6816114d2f9bd255cc1ffa269026e231add118 | [The Call Stack > A practical example]
1. The Call Stack is initially empty. The Event Loop begins evaluating the script.
2. `console.log()` is pushed to the Call Stack and executed, logging `'Script start'`.
3. `setTimeout()` is pushed to the Call Stack and executed. This creates a new Task for its callback function ... | unknown | unknown | [The Call Stack > A practical example]
1. The Call Stack is initially empty. The Event Loop begins evaluating the script.
2. `console.log()` is pushed to the Call Stack and executed, logging `'Script start'`.
3. `setTimeout()` is pushed to the Call Stack and executed. This creates a new Task for its callback function ... | [The Call Stack > A practical example]
1. The Call Stack is initially empty. The Event Loop begins evaluating the script.
2. `console.log()` is pushed to the Call Stack and executed, logging `'Script start'`.
3. `setTimeout()` is pushed to the Call Stack and executed. This creates a new Task for its callback function ... | code_snippets | ||
b616166d-270c-4f11-857b-17a70e2afdf5 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/event-loop-explained.md | unknown | 06b739fe-0126-4e65-98f9-2b9dd8d6c062 | 8 | SemanticChunker@1.0.0 | 05e43fc65bdb909c7da242468223cede6d0d445e831169d865dacd2f41b3ad7b | [The Call Stack > Notes]
## Notes
- The script evaluation step of the Event Loop is in itself treated similarly to a Task.
- The second argument of `setTimeout()` indicates a minimum time until execution, not a guaranteed time. This is due to the fact that Tasks execute in order and that Microtasks may be executed in... | unknown | unknown | [The Call Stack > Notes]
## Notes
- The script evaluation step of the Event Loop is in itself treated similarly to a Task.
- The second argument of `setTimeout()` indicates a minimum time until execution, not a guaranteed time. This is due to the fact that Tasks execute in order and that Microtasks may be executed in... | [The Call Stack > Notes]
## Notes
- The script evaluation step of the Event Loop is in itself treated similarly to a Task.
- The second argument of `setTimeout()` indicates a minimum time until execution, not a guaranteed time. This is due to the fact that Tasks execute in order and that Microtasks may be executed in... | code_snippets | ||
b8145840-89b7-4cca-a8f9-417fc2937770 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/event-loop-explained.md | unknown | 06b739fe-0126-4e65-98f9-2b9dd8d6c062 | 2 | SemanticChunker@1.0.0 | 5cc607fa1539b7b68bf70058c4518ed35b58d2dccbb2ac6add3b61c2546551cf | [The Call Stack > Tasks and the Task Queue]
## Tasks and the Task Queue
**Tasks** are scheduled, synchronous blocks of code. While executing, they have exclusive access to the Call Stack and can also enqueue other tasks. Between Tasks, the browser can perform rendering updates. Tasks are stored in the **Task Queue**,... | unknown | unknown | [The Call Stack > Tasks and the Task Queue]
## Tasks and the Task Queue
**Tasks** are scheduled, synchronous blocks of code. While executing, they have exclusive access to the Call Stack and can also enqueue other tasks. Between Tasks, the browser can perform rendering updates. Tasks are stored in the **Task Queue**,... | [The Call Stack > Tasks and the Task Queue]
## Tasks and the Task Queue
**Tasks** are scheduled, synchronous blocks of code. While executing, they have exclusive access to the Call Stack and can also enqueue other tasks. Between Tasks, the browser can perform rendering updates. Tasks are stored in the **Task Queue**,... | code_snippets | ||
cd5ea598-0f6c-48ab-99dd-46d9b168434c | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/event-loop-explained.md | unknown | 06b739fe-0126-4e65-98f9-2b9dd8d6c062 | 4 | SemanticChunker@1.0.0 | f0ec0842a2dfeb256e5043b9da56ef2f77202078ef2485c9d81a2846d6822245 | [The Call Stack > The Event Loop]
## The Event Loop
Finally, the **Event Loop** is a loop that keeps running and checks if the Call Stack is empty. It processes Tasks and Microtasks, by placing them in the Call Stack one at a time and also controls the rendering process. It's made up of four key steps:
1. **Script e... | unknown | unknown | [The Call Stack > The Event Loop]
## The Event Loop
Finally, the **Event Loop** is a loop that keeps running and checks if the Call Stack is empty. It processes Tasks and Microtasks, by placing them in the Call Stack one at a time and also controls the rendering process. It's made up of four key steps:
1. **Script e... | [The Call Stack > The Event Loop]
## The Event Loop
Finally, the **Event Loop** is a loop that keeps running and checks if the Call Stack is empty. It processes Tasks and Microtasks, by placing them in the Call Stack one at a time and also controls the rendering process. It's made up of four key steps:
1. **Script e... | code_snippets | ||
e85fc803-445e-4371-95be-dc42b2653740 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/event-loop-explained.md | unknown | 06b739fe-0126-4e65-98f9-2b9dd8d6c062 | 0 | SemanticChunker@1.0.0 | a745a1039ee48786d7c4af2543c1a02154c19dd0ccd1c9f5aa02c59ee45c0798 | ---
title: What is the Event Loop in JavaScript?
shortTitle: Event loop explained
language: javascript
tags: [browser,event]
cover: tranquility
excerpt: The Event Loop is a source of confusion for many developers, but it's a fundamental piece of the JavaScript engine.
listed: true
dateModified: 2022-08-21
---
The Even... | unknown | unknown | ---
title: What is the Event Loop in JavaScript?
shortTitle: Event loop explained
language: javascript
tags: [browser,event]
cover: tranquility
excerpt: The Event Loop is a source of confusion for many developers, but it's a fundamental piece of the JavaScript engine.
listed: true
dateModified: 2022-08-21
---
The Even... | ---
title: What is the Event Loop in JavaScript?
shortTitle: Event loop explained
language: javascript
tags: [browser,event]
cover: tranquility
excerpt: The Event Loop is a source of confusion for many developers, but it's a fundamental piece of the JavaScript engine.
listed: true
dateModified: 2022-08-21
---
The Even... | code_snippets | ||
386a7579-3aaf-442e-aaf2-13e8c94a9fd7 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/evil-closures.md | unknown | e506f26c-4325-4eaf-a061-18ded9ef955f | 2 | SemanticChunker@1.0.0 | 1f5116f3eed712430ebbcc3ccf143db781ecac72757ee8a60f5efd104559bfb3 | [Hidden state > Access to context]
## Access to context
Not all closures are created equal. In fact, there are perfectly valid use-cases of closures that can make life a lot easier. For example, **accessing shared constants** should be considered pretty safe. After all, if you want truly pure functions you shouldn't ... | unknown | unknown | [Hidden state > Access to context]
## Access to context
Not all closures are created equal. In fact, there are perfectly valid use-cases of closures that can make life a lot easier. For example, **accessing shared constants** should be considered pretty safe. After all, if you want truly pure functions you shouldn't ... | [Hidden state > Access to context]
## Access to context
Not all closures are created equal. In fact, there are perfectly valid use-cases of closures that can make life a lot easier. For example, **accessing shared constants** should be considered pretty safe. After all, if you want truly pure functions you shouldn't ... | code_snippets | ||
44f7bf99-1800-44c1-b411-02d9865ed3e6 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/evil-closures.md | unknown | e506f26c-4325-4eaf-a061-18ded9ef955f | 3 | SemanticChunker@1.0.0 | 304786504a8556788b65ea3e50b2af8ebfdbcec31d6202e729d6953f6c044416 | [Hidden state > Conclusion]
## Conclusion
Closures in themselves are just another language feature that you have to wrap your head around. As a rule of thumb, use them sparingly, clarify your code's intent and provide escape hatches to reduce potential error surface.
When used correctly, they can be another tool in ... | unknown | unknown | [Hidden state > Conclusion]
## Conclusion
Closures in themselves are just another language feature that you have to wrap your head around. As a rule of thumb, use them sparingly, clarify your code's intent and provide escape hatches to reduce potential error surface.
When used correctly, they can be another tool in ... | [Hidden state > Conclusion]
## Conclusion
Closures in themselves are just another language feature that you have to wrap your head around. As a rule of thumb, use them sparingly, clarify your code's intent and provide escape hatches to reduce potential error surface.
When used correctly, they can be another tool in ... | code_snippets | ||
7414674a-42fb-498b-ad21-73cac199e62b | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/evil-closures.md | unknown | e506f26c-4325-4eaf-a061-18ded9ef955f | 0 | SemanticChunker@1.0.0 | 06aac6cd33cf426ca418fa8d56ad1d535b97e51e5693ef1bd6ed384458930a3b | ---
title: Are JavaScript closures inherently evil?
shortTitle: Closures and hidden state
language: javascript
tags: [function,closure]
cover: silver-flat-screen
excerpt: Closures are used frequently, yet often misunderstood. Understanding them in depth is crucial to be able to write clean, maintainable code.
listed: t... | unknown | unknown | ---
title: Are JavaScript closures inherently evil?
shortTitle: Closures and hidden state
language: javascript
tags: [function,closure]
cover: silver-flat-screen
excerpt: Closures are used frequently, yet often misunderstood. Understanding them in depth is crucial to be able to write clean, maintainable code.
listed: t... | ---
title: Are JavaScript closures inherently evil?
shortTitle: Closures and hidden state
language: javascript
tags: [function,closure]
cover: silver-flat-screen
excerpt: Closures are used frequently, yet often misunderstood. Understanding them in depth is crucial to be able to write clean, maintainable code.
listed: t... | code_snippets | ||
a302ee3d-9e8f-4c9d-90e8-194db9f209b0 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/evil-closures.md | unknown | e506f26c-4325-4eaf-a061-18ded9ef955f | 1 | SemanticChunker@1.0.0 | 608d598519f28622c20258d3faca31b33f2f78e95512dc800f214eccffd2f133 | [Hidden state]
## Hidden state
The main argument against closures is that of hidden state. Hidden state refers to obscuring the state of an object or, in this case, a function. The argument is that **internal mutable state** can create unpredictable behavior and unexpected results. Due to this, it's often said that h... | unknown | unknown | [Hidden state]
## Hidden state
The main argument against closures is that of hidden state. Hidden state refers to obscuring the state of an object or, in this case, a function. The argument is that **internal mutable state** can create unpredictable behavior and unexpected results. Due to this, it's often said that h... | [Hidden state]
## Hidden state
The main argument against closures is that of hidden state. Hidden state refers to obscuring the state of an object or, in this case, a function. The argument is that **internal mutable state** can create unpredictable behavior and unexpected results. Due to this, it's often said that h... | code_snippets | ||
8fd18c22-f884-452b-a654-9e8259c36275 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/expression-statement.md | unknown | 2e5ed4ac-a1cc-405a-aada-48ce538fb46a | 0 | SemanticChunker@1.0.0 | 2290fc943fe6b53469e5cceeeb622d0b048ae47aee244858201ec31400605845 | ---
title: What is the difference between an expression and a statement in JavaScript?
shortTitle: Expressions and statements
language: javascript
tags: [type]
cover: forest-balcony
excerpt: JavaScript distinguishes expressions and statements. Learn their differences in this short article.
listed: true
dateModified: 20... | unknown | unknown | ---
title: What is the difference between an expression and a statement in JavaScript?
shortTitle: Expressions and statements
language: javascript
tags: [type]
cover: forest-balcony
excerpt: JavaScript distinguishes expressions and statements. Learn their differences in this short article.
listed: true
dateModified: 20... | ---
title: What is the difference between an expression and a statement in JavaScript?
shortTitle: Expressions and statements
language: javascript
tags: [type]
cover: forest-balcony
excerpt: JavaScript distinguishes expressions and statements. Learn their differences in this short article.
listed: true
dateModified: 20... | code_snippets | ||
292637dd-b6c7-46e2-a1e3-e29c373b24fd | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/event-listener-once.md | unknown | 191218c3-807a-473f-b244-f58177e62c60 | 0 | SemanticChunker@1.0.0 | 14d3f4e0a5b3d2603e749f113d7c074efd36d3133e1611c125a8595200414388 | ---
title: How can I listen for an event only once in JavaScript?
shortTitle: Execute event handler only once
language: javascript
tags: [browser,event]
cover: fruit-feast
excerpt: Learn how to add an event listener that is executed at most once.
listed: true
dateModified: 2023-11-01
--- | unknown | unknown | ---
title: How can I listen for an event only once in JavaScript?
shortTitle: Execute event handler only once
language: javascript
tags: [browser,event]
cover: fruit-feast
excerpt: Learn how to add an event listener that is executed at most once.
listed: true
dateModified: 2023-11-01
--- | ---
title: How can I listen for an event only once in JavaScript?
shortTitle: Execute event handler only once
language: javascript
tags: [browser,event]
cover: fruit-feast
excerpt: Learn how to add an event listener that is executed at most once.
listed: true
dateModified: 2023-11-01
--- | code_snippets | ||
cc7ebf34-ef76-42d9-a42a-5e98aa323b50 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/event-listener-once.md | unknown | 191218c3-807a-473f-b244-f58177e62c60 | 3 | SemanticChunker@1.0.0 | 53e489ddcbe935d11f8d6dd7c35a8cdc2ebdb8d9133385aa9ec37df1158596cf | [Event listener options > jQuery]
## jQuery
Back in the day, **jQuery** was all the rage due in no small part to it easy to use event handling API. We would usually use [`$.one()`](https://api.jquery.com/one/) to create an event handler that would execute at most once for a given event per element.
```js
$('#my-btn'... | unknown | unknown | [Event listener options > jQuery]
## jQuery
Back in the day, **jQuery** was all the rage due in no small part to it easy to use event handling API. We would usually use [`$.one()`](https://api.jquery.com/one/) to create an event handler that would execute at most once for a given event per element.
```js
$('#my-btn'... | [Event listener options > jQuery]
## jQuery
Back in the day, **jQuery** was all the rage due in no small part to it easy to use event handling API. We would usually use [`$.one()`](https://api.jquery.com/one/) to create an event handler that would execute at most once for a given event per element.
```js
$('#my-btn'... | code_snippets | ||
d2921163-189b-48ca-b0dd-6321fd0edd11 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/event-listener-once.md | unknown | 191218c3-807a-473f-b244-f58177e62c60 | 2 | SemanticChunker@1.0.0 | 62a223c435e0a8aa842bdcffe63e648839e922b84f76aabe22cadde646ee425f | [Event listener options > Using a flag]
## Using a flag
If you are targeting **older browsers**, you can use a **flag-based implementation**. This relies on the event listener changing the status of the `fired` flag, thus resulting in subsequent calls to the event handler being ignored.
```js
const listenOnce = (el,... | unknown | unknown | [Event listener options > Using a flag]
## Using a flag
If you are targeting **older browsers**, you can use a **flag-based implementation**. This relies on the event listener changing the status of the `fired` flag, thus resulting in subsequent calls to the event handler being ignored.
```js
const listenOnce = (el,... | [Event listener options > Using a flag]
## Using a flag
If you are targeting **older browsers**, you can use a **flag-based implementation**. This relies on the event listener changing the status of the `fired` flag, thus resulting in subsequent calls to the event handler being ignored.
```js
const listenOnce = (el,... | code_snippets | ||
f1a1657f-5916-4e2d-b54a-0318da965110 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/event-listener-once.md | unknown | 191218c3-807a-473f-b244-f58177e62c60 | 1 | SemanticChunker@1.0.0 | a9173d9409f1788387d15b0e52025a0333238ee7a3658e107632f620fe44981c | [Event listener options]
## Event listener options
`EventTarget.addEventListener()` expects an `options` object parameter, which allows you to pass a few different flags, one of which is `once`. Setting `once` to `true` results in the event handler being executed **at most once** for a given event per element.
```ht... | unknown | unknown | [Event listener options]
## Event listener options
`EventTarget.addEventListener()` expects an `options` object parameter, which allows you to pass a few different flags, one of which is `once`. Setting `once` to `true` results in the event handler being executed **at most once** for a given event per element.
```ht... | [Event listener options]
## Event listener options
`EventTarget.addEventListener()` expects an `options` object parameter, which allows you to pass a few different flags, one of which is `once`. Setting `once` to `true` results in the event handler being executed **at most once** for a given event per element.
```ht... | code_snippets | ||
d0afd32e-775a-490e-84d6-d75aba27d922 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/expand-tabs.md | unknown | df609109-00fb-4406-b854-cceec0a90e7a | 0 | SemanticChunker@1.0.0 | 6a25ac72d39b9a336719711b5ce61127c86bdece6c9c7256c1fb33798a61063c | ---
title: Expand tabs into spaces
language: javascript
tags: [string,regexp]
cover: naming-conventions
excerpt: Convert tabs to spaces, allowing you to control the number of spaces each tab corresponds to.
listed: true
dateModified: 2024-08-08
---
Tabs vs. spaces is a long-standing debate in the programming community... | unknown | unknown | ---
title: Expand tabs into spaces
language: javascript
tags: [string,regexp]
cover: naming-conventions
excerpt: Convert tabs to spaces, allowing you to control the number of spaces each tab corresponds to.
listed: true
dateModified: 2024-08-08
---
Tabs vs. spaces is a long-standing debate in the programming community... | ---
title: Expand tabs into spaces
language: javascript
tags: [string,regexp]
cover: naming-conventions
excerpt: Convert tabs to spaces, allowing you to control the number of spaces each tab corresponds to.
listed: true
dateModified: 2024-08-08
---
Tabs vs. spaces is a long-standing debate in the programming community... | code_snippets | ||
454aa1e4-2b91-4235-8abd-0be14366c2ec | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/extend-hex.md | unknown | 6f339d95-eab4-49df-a4b0-81cec267bfe4 | 0 | SemanticChunker@1.0.0 | 6b21b46cbbba2c257f96c569e2b0cc941886a71ef6302b776a5e41975d42ed66 | ---
title: How can I extend a 3-digit color code to a 6-digit color code in JavaScript?
shortTitle: Extend 3-digit color code
language: javascript
tags: [string]
cover: laptop-plants
excerpt: Learn how to convert a 3-digit RGB notated hexadecimal color-code to the 6-digit form.
listed: true
dateModified: 2024-02-03
---... | unknown | unknown | ---
title: How can I extend a 3-digit color code to a 6-digit color code in JavaScript?
shortTitle: Extend 3-digit color code
language: javascript
tags: [string]
cover: laptop-plants
excerpt: Learn how to convert a 3-digit RGB notated hexadecimal color-code to the 6-digit form.
listed: true
dateModified: 2024-02-03
---... | ---
title: How can I extend a 3-digit color code to a 6-digit color code in JavaScript?
shortTitle: Extend 3-digit color code
language: javascript
tags: [string]
cover: laptop-plants
excerpt: Learn how to convert a 3-digit RGB notated hexadecimal color-code to the 6-digit form.
listed: true
dateModified: 2024-02-03
---... | code_snippets | ||
2843d270-6b9f-406f-9418-a974484db9ca | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/event-bubbling-capturing-delegation.md | unknown | 3a58bdbb-d792-41c6-8581-39d185d13b69 | 0 | SemanticChunker@1.0.0 | 7e82c93adfb52bb495f3f48140bb940648dcfc9420aeedbc0c2fa76bb1ad6b18 | ---
title: Understanding event bubbling, capturing and delegation in JavaScript
shortTitle: Event bubbling, capturing and delegation
language: javascript
tags: [browser,event]
cover: fishermen
excerpt: Understand how events work in JavaScript and learn when to use event bubbling, event capturing and event delegation wi... | unknown | unknown | ---
title: Understanding event bubbling, capturing and delegation in JavaScript
shortTitle: Event bubbling, capturing and delegation
language: javascript
tags: [browser,event]
cover: fishermen
excerpt: Understand how events work in JavaScript and learn when to use event bubbling, event capturing and event delegation wi... | ---
title: Understanding event bubbling, capturing and delegation in JavaScript
shortTitle: Event bubbling, capturing and delegation
language: javascript
tags: [browser,event]
cover: fishermen
excerpt: Understand how events work in JavaScript and learn when to use event bubbling, event capturing and event delegation wi... | code_snippets | ||
54ddcabf-ccff-40fc-b92b-8288b9ac6631 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/event-bubbling-capturing-delegation.md | unknown | 3a58bdbb-d792-41c6-8581-39d185d13b69 | 2 | SemanticChunker@1.0.0 | 38e363f8514fe7725ed15494b4d26a9bd8fc3cf98b32fff4a9ab4030b84be2cb | [Event bubbling > Event capturing]
## Event capturing
Capturing is the exact opposite of bubbling, meaning that the outer event handlers are fired before the most specific handler (i.e. the one on the `button`). Note that all capturing event handlers are run first, then all the bubbling event handlers.
You can use e... | unknown | unknown | [Event bubbling > Event capturing]
## Event capturing
Capturing is the exact opposite of bubbling, meaning that the outer event handlers are fired before the most specific handler (i.e. the one on the `button`). Note that all capturing event handlers are run first, then all the bubbling event handlers.
You can use e... | [Event bubbling > Event capturing]
## Event capturing
Capturing is the exact opposite of bubbling, meaning that the outer event handlers are fired before the most specific handler (i.e. the one on the `button`). Note that all capturing event handlers are run first, then all the bubbling event handlers.
You can use e... | code_snippets | ||
7e08ac31-bcc4-48fd-8d06-e0259a3a12e4 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/event-bubbling-capturing-delegation.md | unknown | 3a58bdbb-d792-41c6-8581-39d185d13b69 | 3 | SemanticChunker@1.0.0 | 66e35d8f875757da5bb3244c2f29c267046036a3f05de977d59b2734e6b240e5 | [Event bubbling > Event propagation]
## Event propagation
Having explained event bubbling and capturing, we can now explain the three phases of event propagation:
- During the **capture phase**, the event starts from `Window` and moves down to `Document`, the root element and through ancestors of the target element.... | unknown | unknown | [Event bubbling > Event propagation]
## Event propagation
Having explained event bubbling and capturing, we can now explain the three phases of event propagation:
- During the **capture phase**, the event starts from `Window` and moves down to `Document`, the root element and through ancestors of the target element.... | [Event bubbling > Event propagation]
## Event propagation
Having explained event bubbling and capturing, we can now explain the three phases of event propagation:
- During the **capture phase**, the event starts from `Window` and moves down to `Document`, the root element and through ancestors of the target element.... | code_snippets | ||
a662b708-68dd-44bf-bd3b-0bae1a9d42e7 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/event-bubbling-capturing-delegation.md | unknown | 3a58bdbb-d792-41c6-8581-39d185d13b69 | 4 | SemanticChunker@1.0.0 | 1f644578150bb60ceeb4b92c60390eecea1a303d1f17b393c2d0b04434ae56bd | [Event bubbling > Event delegation]
## Event delegation
Event delegation refers to the idea of delegating event listening to parent elements instead of adding event listeners directly to the event targets. Using this technique, the parent can catch and handle the bubbling events as necessary.
```js
window.addEventLi... | unknown | unknown | [Event bubbling > Event delegation]
## Event delegation
Event delegation refers to the idea of delegating event listening to parent elements instead of adding event listeners directly to the event targets. Using this technique, the parent can catch and handle the bubbling events as necessary.
```js
window.addEventLi... | [Event bubbling > Event delegation]
## Event delegation
Event delegation refers to the idea of delegating event listening to parent elements instead of adding event listeners directly to the event targets. Using this technique, the parent can catch and handle the bubbling events as necessary.
```js
window.addEventLi... | code_snippets | ||
abf6f962-a822-4a04-af43-2a71f3b0c986 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/event-bubbling-capturing-delegation.md | unknown | 3a58bdbb-d792-41c6-8581-39d185d13b69 | 1 | SemanticChunker@1.0.0 | d30c9e4ea2668f8a4f4454367f514b7c8eb54c41b37fd081cc890d8485d381b8 | [Event bubbling]
## Event bubbling
Bubbling means that the event propagates from the target element (i.e. the `button` the user clicked) up through its ancestor tree, starting from the nearest one. By default, all events bubble.
To better understand event bubbling, consider the following HTML example, which we will ... | unknown | unknown | [Event bubbling]
## Event bubbling
Bubbling means that the event propagates from the target element (i.e. the `button` the user clicked) up through its ancestor tree, starting from the nearest one. By default, all events bubble.
To better understand event bubbling, consider the following HTML example, which we will ... | [Event bubbling]
## Event bubbling
Bubbling means that the event propagates from the target element (i.e. the `button` the user clicked) up through its ancestor tree, starting from the nearest one. By default, all events bubble.
To better understand event bubbling, consider the following HTML example, which we will ... | code_snippets | ||
3d0c7bf3-5b8c-4aec-aaf3-638e0eebbdb3 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/event-driven-vanilla-js-signals.md | unknown | 2aadaa5a-4d01-450b-aa8a-0e66dab49c91 | 1 | SemanticChunker@1.0.0 | 63c1f4dc1587050fd2b1757d84ae75ad43917e512a8fae03aa2ffc677b739b10 | [Signals]
## Signals
In most reactive-programming libraries nowadays, there's a concept of **signals**. A signal is a fancy name for an **observable**, a stream of values that can be listened to. When a signal changes, it notifies all its listeners, which can then react to the change.
We can implement a very simple ... | unknown | unknown | [Signals]
## Signals
In most reactive-programming libraries nowadays, there's a concept of **signals**. A signal is a fancy name for an **observable**, a stream of values that can be listened to. When a signal changes, it notifies all its listeners, which can then react to the change.
We can implement a very simple ... | [Signals]
## Signals
In most reactive-programming libraries nowadays, there's a concept of **signals**. A signal is a fancy name for an **observable**, a stream of values that can be listened to. When a signal changes, it notifies all its listeners, which can then react to the change.
We can implement a very simple ... | code_snippets | ||
5a9d9640-8655-4751-aa40-4715af580a15 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/event-driven-vanilla-js-signals.md | unknown | 2aadaa5a-4d01-450b-aa8a-0e66dab49c91 | 0 | SemanticChunker@1.0.0 | 4e33379a5fc83f6da6175decc11a336b7100ab1b9ec70f4f6e0f12ba921ecdce | ---
title: Implementing signals and reactivity with vanilla JavaScript
shortTitle: Signals and reactivity
language: javascript
tags: [object,event,class]
cover: metro-tunnel
excerpt: Let's demystify signals and reactivity, one of the most popular patterns in modern JavaScript, using event-driven programming!
listed: tr... | unknown | unknown | ---
title: Implementing signals and reactivity with vanilla JavaScript
shortTitle: Signals and reactivity
language: javascript
tags: [object,event,class]
cover: metro-tunnel
excerpt: Let's demystify signals and reactivity, one of the most popular patterns in modern JavaScript, using event-driven programming!
listed: tr... | ---
title: Implementing signals and reactivity with vanilla JavaScript
shortTitle: Signals and reactivity
language: javascript
tags: [object,event,class]
cover: metro-tunnel
excerpt: Let's demystify signals and reactivity, one of the most popular patterns in modern JavaScript, using event-driven programming!
listed: tr... | code_snippets | ||
6b6d2f83-ffd3-4bcd-b641-9554c2a68a2b | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/event-driven-vanilla-js-signals.md | unknown | 2aadaa5a-4d01-450b-aa8a-0e66dab49c91 | 5 | SemanticChunker@1.0.0 | 41acf1bd4f19312a53c1162a2d821d41f5b5efc873f2fca3e1610cb3bcb1c6da | [Signals > Conclusion]
## Conclusion
In just **under 70 lines of code**, we've demystified signals and reactivity, one of the most popular patterns in modern JavaScript. We've seen how to implement signals, effects, and computed values, using simple concepts and familiar APIs. This is a great exercise to understand h... | unknown | unknown | [Signals > Conclusion]
## Conclusion
In just **under 70 lines of code**, we've demystified signals and reactivity, one of the most popular patterns in modern JavaScript. We've seen how to implement signals, effects, and computed values, using simple concepts and familiar APIs. This is a great exercise to understand h... | [Signals > Conclusion]
## Conclusion
In just **under 70 lines of code**, we've demystified signals and reactivity, one of the most popular patterns in modern JavaScript. We've seen how to implement signals, effects, and computed values, using simple concepts and familiar APIs. This is a great exercise to understand h... | code_snippets | ||
988b051b-e75e-4dda-97ed-c7e1daa7e85e | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/event-driven-vanilla-js-signals.md | unknown | 2aadaa5a-4d01-450b-aa8a-0e66dab49c91 | 2 | SemanticChunker@1.0.0 | f4e28eab363a9d0f06f4b7f296319818063dcdaa437b9fa07d2b4c941d5a706c | [Signals > Effect]
## Effect
Most reactive libraries, also provide a way to create **effects**, a fancier name for **observers**. An effect is a function that is called whenever any signal it depends on changes. This is a very powerful concept, as it allows us to **react to changes** in the system in a very declarati... | unknown | unknown | [Signals > Effect]
## Effect
Most reactive libraries, also provide a way to create **effects**, a fancier name for **observers**. An effect is a function that is called whenever any signal it depends on changes. This is a very powerful concept, as it allows us to **react to changes** in the system in a very declarati... | [Signals > Effect]
## Effect
Most reactive libraries, also provide a way to create **effects**, a fancier name for **observers**. An effect is a function that is called whenever any signal it depends on changes. This is a very powerful concept, as it allows us to **react to changes** in the system in a very declarati... | code_snippets | ||
c523e75d-5412-42da-be16-1571c3431935 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/event-driven-vanilla-js-signals.md | unknown | 2aadaa5a-4d01-450b-aa8a-0e66dab49c91 | 4 | SemanticChunker@1.0.0 | 6d0b602d6da156b5c79e43fe90939985d8133c6e335fdc0000f299f383decb59 | [Signals > Computed values]
```js
class ComputedValue extends EventTarget {
#signal;
#effect;
constructor(callback, dependencies = []) {
super();
this.#signal = new Signal(callback());
this.#effect = new Effect(() => {
this.#signal.value = callback();
}, dependencies);
}
get value() {
return this.#signal.... | unknown | unknown | [Signals > Computed values]
```js
class ComputedValue extends EventTarget {
#signal;
#effect;
constructor(callback, dependencies = []) {
super();
this.#signal = new Signal(callback());
this.#effect = new Effect(() => {
this.#signal.value = callback();
}, dependencies);
}
get value() {
return this.#signal.... | [Signals > Computed values]
```js
class ComputedValue extends EventTarget {
#signal;
#effect;
constructor(callback, dependencies = []) {
super();
this.#signal = new Signal(callback());
this.#effect = new Effect(() => {
this.#signal.value = callback();
}, dependencies);
}
get value() {
return this.#signal.... | code_snippets | ||
f0550695-fcda-422d-a9ba-bb506b5960db | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/event-driven-vanilla-js-signals.md | unknown | 2aadaa5a-4d01-450b-aa8a-0e66dab49c91 | 3 | SemanticChunker@1.0.0 | 049a1874896d9990e7b8b74eed24909780348f1c1cd4901466a2a130bca392f7 | [Signals > Computed values]
## Computed values
A problematic situation arises when we want to **compute a value from a signal**, then create an effect that reacts to it changing. Computed values are essentially signals that depend on other signals, but they don't have a direct value. Instead, they compute their value... | unknown | unknown | [Signals > Computed values]
## Computed values
A problematic situation arises when we want to **compute a value from a signal**, then create an effect that reacts to it changing. Computed values are essentially signals that depend on other signals, but they don't have a direct value. Instead, they compute their value... | [Signals > Computed values]
## Computed values
A problematic situation arises when we want to **compute a value from a signal**, then create an effect that reacts to it changing. Computed values are essentially signals that depend on other signals, but they don't have a direct value. Instead, they compute their value... | code_snippets | ||
558cc303-b02a-4ea9-b02a-5da8c054082f | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/factorial.md | unknown | 86257cd8-d5be-4ba8-8154-498e498c0dae | 0 | SemanticChunker@1.0.0 | bcb16f5e88ae84dc7eed3e1859b0676d14067a018a7b9dcef4c4c2c07c0fcb67 | ---
title: Calculate the factorial of a number using JavaScript
shortTitle: Factorial of number
language: javascript
tags: [math,algorithm,recursion]
cover: flower-vase
excerpt: Calculate the factorial of a number, using two different approaches.
listed: true
dateModified: 2024-08-18
---
The [factorial of a number](ht... | unknown | unknown | ---
title: Calculate the factorial of a number using JavaScript
shortTitle: Factorial of number
language: javascript
tags: [math,algorithm,recursion]
cover: flower-vase
excerpt: Calculate the factorial of a number, using two different approaches.
listed: true
dateModified: 2024-08-18
---
The [factorial of a number](ht... | ---
title: Calculate the factorial of a number using JavaScript
shortTitle: Factorial of number
language: javascript
tags: [math,algorithm,recursion]
cover: flower-vase
excerpt: Calculate the factorial of a number, using two different approaches.
listed: true
dateModified: 2024-08-18
---
The [factorial of a number](ht... | code_snippets | ||
76ce7345-d817-48ae-b6d5-9315afd9c52d | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/factorial.md | unknown | 86257cd8-d5be-4ba8-8154-498e498c0dae | 2 | SemanticChunker@1.0.0 | 8bddd36452607063fcc3b9c03abd2e497a409d09e72cf89750efc5130c4f1774 | [Iterative approach > Recursive approach]
## Recursive approach
The **recursive** approach is more elegant and concise, but it **can be less efficient** due to the overhead of function calls. Instead of a loop, it uses a **function that calls itself** with a smaller input until it reaches the **base case** of `n <= 1... | unknown | unknown | [Iterative approach > Recursive approach]
## Recursive approach
The **recursive** approach is more elegant and concise, but it **can be less efficient** due to the overhead of function calls. Instead of a loop, it uses a **function that calls itself** with a smaller input until it reaches the **base case** of `n <= 1... | [Iterative approach > Recursive approach]
## Recursive approach
The **recursive** approach is more elegant and concise, but it **can be less efficient** due to the overhead of function calls. Instead of a loop, it uses a **function that calls itself** with a smaller input until it reaches the **base case** of `n <= 1... | code_snippets | ||
ed33cb02-53f5-4932-bd10-042f055062dd | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/factorial.md | unknown | 86257cd8-d5be-4ba8-8154-498e498c0dae | 1 | SemanticChunker@1.0.0 | 6853bef5d396608a1a2ea6ea6210eaf672a9d991ae0e7f299a49f26688d95e39 | [Iterative approach]
## Iterative approach
The **iterative** approach calculates the factorial of a number using a `for` **loop**, updating a `result` variable with the product of all numbers from `2` to `n`. While it's not the prettiest code you'll write, it gets the job done, it's **efficient and easy to understand... | unknown | unknown | [Iterative approach]
## Iterative approach
The **iterative** approach calculates the factorial of a number using a `for` **loop**, updating a `result` variable with the product of all numbers from `2` to `n`. While it's not the prettiest code you'll write, it gets the job done, it's **efficient and easy to understand... | [Iterative approach]
## Iterative approach
The **iterative** approach calculates the factorial of a number using a `for` **loop**, updating a `result` variable with the product of all numbers from `2` to `n`. While it's not the prettiest code you'll write, it gets the job done, it's **efficient and easy to understand... | code_snippets | ||
6a1e72a0-b8cf-4b62-83d1-4f13f6a5f448 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/first-last-n-elements.md | unknown | e103029b-cd78-4d7f-9e37-3a33d8842bdf | 0 | SemanticChunker@1.0.0 | e05c5a371263ef5be0c43c656dbef4818cae4c44fafc7e7e3536cb64e2effcc3 | ---
title: Get the first or last n elements in a JavaScript array
shortTitle: First or last n elements of an array
language: javascript
tags: [array]
cover: fort-lamp
excerpt: Learn how to retrieve the first or last `n` elements in a JavaScript array with a single line of code.
listed: true
dateModified: 2023-10-04
---... | unknown | unknown | ---
title: Get the first or last n elements in a JavaScript array
shortTitle: First or last n elements of an array
language: javascript
tags: [array]
cover: fort-lamp
excerpt: Learn how to retrieve the first or last `n` elements in a JavaScript array with a single line of code.
listed: true
dateModified: 2023-10-04
---... | ---
title: Get the first or last n elements in a JavaScript array
shortTitle: First or last n elements of an array
language: javascript
tags: [array]
cover: fort-lamp
excerpt: Learn how to retrieve the first or last `n` elements in a JavaScript array with a single line of code.
listed: true
dateModified: 2023-10-04
---... | code_snippets | ||
92153569-4ce1-41b4-83c7-d56537f50c66 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/flat-iterator.md | unknown | 1e5bcd13-5756-47a3-84f9-3b43dd35bbaf | 0 | SemanticChunker@1.0.0 | dc2aec9b3e626ad0f311bc68701761cbd5c8ee3a1056e6b683f992f95f94101d | ---
title: How can I iterate over nested iterables in JavaScript?
shortTitle: Flat iterator
language: javascript
tags: [array,iterator,generator,recursion]
cover: sail-away-2
excerpt: Ever wanted to iterate over nested iterables in JavaScript? Here's how.
listed: true
dateModified: 2022-07-25
---
JavaScript's `Symbol.... | unknown | unknown | ---
title: How can I iterate over nested iterables in JavaScript?
shortTitle: Flat iterator
language: javascript
tags: [array,iterator,generator,recursion]
cover: sail-away-2
excerpt: Ever wanted to iterate over nested iterables in JavaScript? Here's how.
listed: true
dateModified: 2022-07-25
---
JavaScript's `Symbol.... | ---
title: How can I iterate over nested iterables in JavaScript?
shortTitle: Flat iterator
language: javascript
tags: [array,iterator,generator,recursion]
cover: sail-away-2
excerpt: Ever wanted to iterate over nested iterables in JavaScript? Here's how.
listed: true
dateModified: 2022-07-25
---
JavaScript's `Symbol.... | code_snippets | ||
2493e64f-d5a2-4416-bdd6-b9b5e93f79dd | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/first-last-date-of-month.md | unknown | cacbcdd3-c80b-4df4-8d8d-7742dbeba2a4 | 2 | SemanticChunker@1.0.0 | 1552d0b2cf1a520a817d5f72399a9c8cf55687316adb8b93774938dafe6aeeb7 | [Get the first date of a month > Get the last date of a month]
## Get the last date of a month
In order to get the last date of a month, we can use a clever trick on top of the previous code snippet. Instead of setting the day to `1`, we can set it to `0`. This will give us the **last day of the previous month**. In ... | unknown | unknown | [Get the first date of a month > Get the last date of a month]
## Get the last date of a month
In order to get the last date of a month, we can use a clever trick on top of the previous code snippet. Instead of setting the day to `1`, we can set it to `0`. This will give us the **last day of the previous month**. In ... | [Get the first date of a month > Get the last date of a month]
## Get the last date of a month
In order to get the last date of a month, we can use a clever trick on top of the previous code snippet. Instead of setting the day to `1`, we can set it to `0`. This will give us the **last day of the previous month**. In ... | code_snippets | ||
a50de58a-f44a-42a7-9c4a-b46769e2374c | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/first-last-date-of-month.md | unknown | cacbcdd3-c80b-4df4-8d8d-7742dbeba2a4 | 0 | SemanticChunker@1.0.0 | f9de0f9b7e7c6beb9bcc59425fdc623601a5f8262433a06c57bbf5c4e582dbed | ---
title: Get the first or last date of a month using JavaScript
shortTitle: First or last date of a month
language: javascript
tags: [date]
cover: polar-bear
excerpt: Use the `Date` object and some clever tricks to get the first or last date of a month in JavaScript.
listed: true
dateModified: 2024-02-17
---
Getting... | unknown | unknown | ---
title: Get the first or last date of a month using JavaScript
shortTitle: First or last date of a month
language: javascript
tags: [date]
cover: polar-bear
excerpt: Use the `Date` object and some clever tricks to get the first or last date of a month in JavaScript.
listed: true
dateModified: 2024-02-17
---
Getting... | ---
title: Get the first or last date of a month using JavaScript
shortTitle: First or last date of a month
language: javascript
tags: [date]
cover: polar-bear
excerpt: Use the `Date` object and some clever tricks to get the first or last date of a month in JavaScript.
listed: true
dateModified: 2024-02-17
---
Getting... | code_snippets | ||
ffdebe1c-f7b2-47eb-97a6-73dc69f86015 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/first-last-date-of-month.md | unknown | cacbcdd3-c80b-4df4-8d8d-7742dbeba2a4 | 1 | SemanticChunker@1.0.0 | a02ef502a372accc6f45243710f31200c09f7c484142726f79243ec2ba5d6ab0 | [Get the first date of a month]
## Get the first date of a month
Given any `Date` object, you can use `Date.prototype.getFullYear()` and `Date.prototype.getMonth()` to get the **current year and month** from the given date. In order to get the first date of a month, you just need to create a new `Date` object, using ... | unknown | unknown | [Get the first date of a month]
## Get the first date of a month
Given any `Date` object, you can use `Date.prototype.getFullYear()` and `Date.prototype.getMonth()` to get the **current year and month** from the given date. In order to get the first date of a month, you just need to create a new `Date` object, using ... | [Get the first date of a month]
## Get the first date of a month
Given any `Date` object, you can use `Date.prototype.getFullYear()` and `Date.prototype.getMonth()` to get the **current year and month** from the given date. In order to get the first date of a month, you just need to create a new `Date` object, using ... | code_snippets | ||
627dc9a7-6810-4b5a-8b9e-99f7d21279f1 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/filter-object-keys.md | unknown | 36cb4420-c315-4175-a423-c3684701e8c7 | 2 | SemanticChunker@1.0.0 | 408d3bc8c00a87538b2d57616eb4cdcff1268d2b048e7654412789ecec542898 | [Filter object properties by key > Filter object properties conditionally]
## Filter object properties conditionally
The previous code snippet covers most simple use-cases, but what if we want to filter the object's properties based on a **predicate function**? For example, we might want to filter out all the propert... | unknown | unknown | [Filter object properties by key > Filter object properties conditionally]
## Filter object properties conditionally
The previous code snippet covers most simple use-cases, but what if we want to filter the object's properties based on a **predicate function**? For example, we might want to filter out all the propert... | [Filter object properties by key > Filter object properties conditionally]
## Filter object properties conditionally
The previous code snippet covers most simple use-cases, but what if we want to filter the object's properties based on a **predicate function**? For example, we might want to filter out all the propert... | code_snippets | ||
9d48da06-3a21-402e-bcbe-1e1e5322415c | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/filter-object-keys.md | unknown | 36cb4420-c315-4175-a423-c3684701e8c7 | 0 | SemanticChunker@1.0.0 | 8aee79c4c305240d6dbef02571f537e2ff1800721a041af91c1e1fd1dbf49bcd | ---
title: Filter a JavaScript object's properties by keys or values
shortTitle: Filter object keys or values
language: javascript
tags: [object]
cover: leafy-screens
excerpt: Learn how to transform objects by filtering their properties based on an array of keys or a predicate function.
listed: true
dateModified: 2024-... | unknown | unknown | ---
title: Filter a JavaScript object's properties by keys or values
shortTitle: Filter object keys or values
language: javascript
tags: [object]
cover: leafy-screens
excerpt: Learn how to transform objects by filtering their properties based on an array of keys or a predicate function.
listed: true
dateModified: 2024-... | ---
title: Filter a JavaScript object's properties by keys or values
shortTitle: Filter object keys or values
language: javascript
tags: [object]
cover: leafy-screens
excerpt: Learn how to transform objects by filtering their properties based on an array of keys or a predicate function.
listed: true
dateModified: 2024-... | code_snippets | ||
ad504096-2a25-47cb-8a7f-df23c2c5070a | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/filter-object-keys.md | unknown | 36cb4420-c315-4175-a423-c3684701e8c7 | 1 | SemanticChunker@1.0.0 | 76156248fb9c0dcb9ef2ab260d593f3be7c56053c8a0610c6d40b14f8c67ea79 | [Filter object properties by key]
## Filter object properties by key
Given an object, we want to filter its properties based on an **array of keys**. The result should be a **new object** with only the properties whose keys are included in the array.
To accomplish this, we will leverage `Object.entries()` to get an ... | unknown | unknown | [Filter object properties by key]
## Filter object properties by key
Given an object, we want to filter its properties based on an **array of keys**. The result should be a **new object** with only the properties whose keys are included in the array.
To accomplish this, we will leverage `Object.entries()` to get an ... | [Filter object properties by key]
## Filter object properties by key
Given an object, we want to filter its properties based on an **array of keys**. The result should be a **new object** with only the properties whose keys are included in the array.
To accomplish this, we will leverage `Object.entries()` to get an ... | code_snippets | ||
44286208-b36c-453c-a2a6-c5c519146f14 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/find-matching-bracket-pairs.md | unknown | f5c7335c-0b56-4b5c-b4bf-47b38e5cb02e | 9 | SemanticChunker@1.0.0 | ea6becb96457940c8d92e304fd1109765baddd02185a19f82394ad6a8cb890c3 | [Matching parentheses > Conclusion]
## Conclusion
As you can see, solving the matching bracket pairs problem is straightforward, using a stack-based approach. While this solution is fairly simple, it can easily be expanded upon for more complex tasks, such as incorporating a **tokenizer to parse multi-character token... | unknown | unknown | [Matching parentheses > Conclusion]
## Conclusion
As you can see, solving the matching bracket pairs problem is straightforward, using a stack-based approach. While this solution is fairly simple, it can easily be expanded upon for more complex tasks, such as incorporating a **tokenizer to parse multi-character token... | [Matching parentheses > Conclusion]
## Conclusion
As you can see, solving the matching bracket pairs problem is straightforward, using a stack-based approach. While this solution is fairly simple, it can easily be expanded upon for more complex tasks, such as incorporating a **tokenizer to parse multi-character token... | code_snippets | ||
54c8c3fd-7cfe-4c65-b4a3-ff2c5a284f35 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/find-matching-bracket-pairs.md | unknown | f5c7335c-0b56-4b5c-b4bf-47b38e5cb02e | 7 | SemanticChunker@1.0.0 | 483b66ca5f75b63b250678f7fdcb59212a68da9c291d688724d810e76eb91c95 | [Matching parentheses > Generalizing the solution]
## Generalizing the solution
The solutions we've seen so far handles parentheses and brackets. But, what if I told you it can handle far **more complex inputs**, such as HTML or XML tags? The solution can be generalized to handle any type of bracket pair, as long as ... | unknown | unknown | [Matching parentheses > Generalizing the solution]
## Generalizing the solution
The solutions we've seen so far handles parentheses and brackets. But, what if I told you it can handle far **more complex inputs**, such as HTML or XML tags? The solution can be generalized to handle any type of bracket pair, as long as ... | [Matching parentheses > Generalizing the solution]
## Generalizing the solution
The solutions we've seen so far handles parentheses and brackets. But, what if I told you it can handle far **more complex inputs**, such as HTML or XML tags? The solution can be generalized to handle any type of bracket pair, as long as ... | code_snippets | ||
6402b4d8-46ab-4dea-b417-aca75793b73c | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/find-matching-bracket-pairs.md | unknown | f5c7335c-0b56-4b5c-b4bf-47b38e5cb02e | 8 | SemanticChunker@1.0.0 | c5eb615665dd26edb62f287b7ce7bf9cb3ce1e9b60ca6dd6199a7d2c5a1c12f6 | [Matching parentheses > Generalizing the solution]
> [!TIP]
>
> I'm using [currying](/js/s/currying/) to create a function that accepts the bracket rules and **returns a function** that accepts the string to find the matching bracket pairs. This way, you can easily **reuse the function** with different inputs, given t... | unknown | unknown | [Matching parentheses > Generalizing the solution]
> [!TIP]
>
> I'm using [currying](/js/s/currying/) to create a function that accepts the bracket rules and **returns a function** that accepts the string to find the matching bracket pairs. This way, you can easily **reuse the function** with different inputs, given t... | [Matching parentheses > Generalizing the solution]
> [!TIP]
>
> I'm using [currying](/js/s/currying/) to create a function that accepts the bracket rules and **returns a function** that accepts the string to find the matching bracket pairs. This way, you can easily **reuse the function** with different inputs, given t... | code_snippets | ||
648113e7-19ac-4e47-b2e2-ae05f5366ca0 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/find-matching-bracket-pairs.md | unknown | f5c7335c-0b56-4b5c-b4bf-47b38e5cb02e | 6 | SemanticChunker@1.0.0 | 0daf3738679d4d8181df38811c5d9522b4d98518ca990be83698dfc8b7a694e2 | [Matching parentheses > Using a `Map`]
```js
const findMatchingBrackets = str => {
const bracketMap = {
'(': ')',
'[': ']',
'{': '}',
};
const bracketOpenings = Object.keys(bracketMap);
const bracketClosings = Object.values(bracketMap);
const { stack, pairs } = [...str].reduce(
({ pairs, stack }, char, i) =>... | unknown | unknown | [Matching parentheses > Using a `Map`]
```js
const findMatchingBrackets = str => {
const bracketMap = {
'(': ')',
'[': ']',
'{': '}',
};
const bracketOpenings = Object.keys(bracketMap);
const bracketClosings = Object.values(bracketMap);
const { stack, pairs } = [...str].reduce(
({ pairs, stack }, char, i) =>... | [Matching parentheses > Using a `Map`]
```js
const findMatchingBrackets = str => {
const bracketMap = {
'(': ')',
'[': ']',
'{': '}',
};
const bracketOpenings = Object.keys(bracketMap);
const bracketClosings = Object.values(bracketMap);
const { stack, pairs } = [...str].reduce(
({ pairs, stack }, char, i) =>... | code_snippets | ||
79ecfb80-2b08-45d6-8b2d-816e116a8a44 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/find-matching-bracket-pairs.md | unknown | f5c7335c-0b56-4b5c-b4bf-47b38e5cb02e | 1 | SemanticChunker@1.0.0 | 0b32be60458612dfbae98e2247871b8c70bc1bd741ede99d9b1cde41276829b0 | [Matching parentheses]
## Matching parentheses
Given a simple string with **parentheses**, like `((a + b))`, we want to find the matching pairs of parentheses. In this case, the pairs are `(0, 8)` and `(1, 7)`. The first pair is the outermost parentheses, and the second pair is the innermost parentheses.
Looking clo... | unknown | unknown | [Matching parentheses]
## Matching parentheses
Given a simple string with **parentheses**, like `((a + b))`, we want to find the matching pairs of parentheses. In this case, the pairs are `(0, 8)` and `(1, 7)`. The first pair is the outermost parentheses, and the second pair is the innermost parentheses.
Looking clo... | [Matching parentheses]
## Matching parentheses
Given a simple string with **parentheses**, like `((a + b))`, we want to find the matching pairs of parentheses. In this case, the pairs are `(0, 8)` and `(1, 7)`. The first pair is the outermost parentheses, and the second pair is the innermost parentheses.
Looking clo... | code_snippets | ||
9c0d083a-e496-44bb-9d63-4d55598a22a6 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/find-matching-bracket-pairs.md | unknown | f5c7335c-0b56-4b5c-b4bf-47b38e5cb02e | 2 | SemanticChunker@1.0.0 | edf9e4e923f2e5a1ad1143983ff0ea42e8a06dc7b69bd6d14413b7266f65745f | [Matching parentheses]
> [!TIP]
>
> If you want to **learn more about stacks**, check out the article on the [Stack data structure](/js/s/data-structures-stack) and how it can be implemented in JavaScript. | unknown | unknown | [Matching parentheses]
> [!TIP]
>
> If you want to **learn more about stacks**, check out the article on the [Stack data structure](/js/s/data-structures-stack) and how it can be implemented in JavaScript. | [Matching parentheses]
> [!TIP]
>
> If you want to **learn more about stacks**, check out the article on the [Stack data structure](/js/s/data-structures-stack) and how it can be implemented in JavaScript. | code_snippets | ||
cbfd9da2-834e-4d9e-b105-7dec6cd0fc2e | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/find-matching-bracket-pairs.md | unknown | f5c7335c-0b56-4b5c-b4bf-47b38e5cb02e | 3 | SemanticChunker@1.0.0 | 923c29b71884b7d86520732d323ab2b0ab63564d226be5a298821fd101bc7c57 | [Matching parentheses > Matching bracket pairs]
## Matching bracket pairs
Now that we've seen how to find matching parentheses, we can extend the solution to **matching bracket pairs**. We'll consider the following bracket pairs: `()`, `[]`, and `{}`.
The approach is similar to the one we used for parentheses. The m... | unknown | unknown | [Matching parentheses > Matching bracket pairs]
## Matching bracket pairs
Now that we've seen how to find matching parentheses, we can extend the solution to **matching bracket pairs**. We'll consider the following bracket pairs: `()`, `[]`, and `{}`.
The approach is similar to the one we used for parentheses. The m... | [Matching parentheses > Matching bracket pairs]
## Matching bracket pairs
Now that we've seen how to find matching parentheses, we can extend the solution to **matching bracket pairs**. We'll consider the following bracket pairs: `()`, `[]`, and `{}`.
The approach is similar to the one we used for parentheses. The m... | code_snippets | ||
ccb8e844-4239-4c20-8d12-4f7004971ddd | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/find-matching-bracket-pairs.md | unknown | f5c7335c-0b56-4b5c-b4bf-47b38e5cb02e | 5 | SemanticChunker@1.0.0 | dc023ec2f149312394bf0664bd62399132c4ae99fda8a98bfb0d6dfd671b68f1 | [Matching parentheses > Using a `Map`]
## Using a `Map`
In many of the use cases I've come across, a `Map` is way easier to work with as a result, instead of an array of arrays. The simple fact of the matter is, in most scenarios, you'll want to **retrieve the matching bracket index**, given an opening or closing bra... | unknown | unknown | [Matching parentheses > Using a `Map`]
## Using a `Map`
In many of the use cases I've come across, a `Map` is way easier to work with as a result, instead of an array of arrays. The simple fact of the matter is, in most scenarios, you'll want to **retrieve the matching bracket index**, given an opening or closing bra... | [Matching parentheses > Using a `Map`]
## Using a `Map`
In many of the use cases I've come across, a `Map` is way easier to work with as a result, instead of an array of arrays. The simple fact of the matter is, in most scenarios, you'll want to **retrieve the matching bracket index**, given an opening or closing bra... | code_snippets | ||
cf7ed543-88c2-41b7-bd0f-70730e8149fb | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/find-matching-bracket-pairs.md | unknown | f5c7335c-0b56-4b5c-b4bf-47b38e5cb02e | 4 | SemanticChunker@1.0.0 | 0ea5be96e7ade20ebc0966436d9f4eed68c3ec326ad45e4e0755c56c202fb997 | [Matching parentheses > Matching bracket pairs]
```js
const findMatchingBrackets = str => {
const bracketMap = {
'(': ')',
'[': ']',
'{': '}',
};
const bracketOpenings = Object.keys(bracketMap);
const bracketClosings = Object.values(bracketMap);
const { stack, pairs } = [...str].reduce(
({ pairs, stack }, ch... | unknown | unknown | [Matching parentheses > Matching bracket pairs]
```js
const findMatchingBrackets = str => {
const bracketMap = {
'(': ')',
'[': ']',
'{': '}',
};
const bracketOpenings = Object.keys(bracketMap);
const bracketClosings = Object.values(bracketMap);
const { stack, pairs } = [...str].reduce(
({ pairs, stack }, ch... | [Matching parentheses > Matching bracket pairs]
```js
const findMatchingBrackets = str => {
const bracketMap = {
'(': ')',
'[': ']',
'{': '}',
};
const bracketOpenings = Object.keys(bracketMap);
const bracketClosings = Object.values(bracketMap);
const { stack, pairs } = [...str].reduce(
({ pairs, stack }, ch... | code_snippets | ||
ed24d6dc-d05f-4070-bf9a-f6f4b981ea46 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/find-matching-bracket-pairs.md | unknown | f5c7335c-0b56-4b5c-b4bf-47b38e5cb02e | 0 | SemanticChunker@1.0.0 | 1bc01b0c72997ae77ce982264694577e56e924358cad15fbd19f2f9e88141aca | ---
title: Find matching bracket pairs in a string with JavaScript
shortTitle: Matching bracket pairs
language: javascript
tags: [string,algorithm]
cover: invention-shack
excerpt: A common problem when building parsers is finding matching bracket pairs in a string. Here's how you can solve it with JavaScript.
listed: t... | unknown | unknown | ---
title: Find matching bracket pairs in a string with JavaScript
shortTitle: Matching bracket pairs
language: javascript
tags: [string,algorithm]
cover: invention-shack
excerpt: A common problem when building parsers is finding matching bracket pairs in a string. Here's how you can solve it with JavaScript.
listed: t... | ---
title: Find matching bracket pairs in a string with JavaScript
shortTitle: Matching bracket pairs
language: javascript
tags: [string,algorithm]
cover: invention-shack
excerpt: A common problem when building parsers is finding matching bracket pairs in a string. Here's how you can solve it with JavaScript.
listed: t... | code_snippets | ||
94e13b2e-e8e3-4c4a-9ec8-ce402f05261d | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/for-each-right.md | unknown | 20b17df1-8272-448b-9655-dbbc459b1449 | 0 | SemanticChunker@1.0.0 | 7584301eb27eaae9d67f4bd0532bf60d9d2c30f028d32832033a02a89f2e5d79 | ---
title: Iterate over a JavaScript array from right to left
shortTitle: Iterate over array in reverse
language: javascript
tags: [array]
cover: interior-6
excerpt: Learn how to execute a function for each element of an array, starting from the last one.
listed: true
dateModified: 2023-10-10
---
`Array.prototype.forE... | unknown | unknown | ---
title: Iterate over a JavaScript array from right to left
shortTitle: Iterate over array in reverse
language: javascript
tags: [array]
cover: interior-6
excerpt: Learn how to execute a function for each element of an array, starting from the last one.
listed: true
dateModified: 2023-10-10
---
`Array.prototype.forE... | ---
title: Iterate over a JavaScript array from right to left
shortTitle: Iterate over array in reverse
language: javascript
tags: [array]
cover: interior-6
excerpt: Learn how to execute a function for each element of an array, starting from the last one.
listed: true
dateModified: 2023-10-10
---
`Array.prototype.forE... | code_snippets | ||
6724d6ea-19cc-476c-8a77-de7ec48ed1ed | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/find-remove-compact-whitespace.md | unknown | c05f4d15-936e-404d-9301-6031b720b35c | 1 | SemanticChunker@1.0.0 | e6c0b02ebd10986329ed00b8def3a4bc4f410e7ff9157c6d94f43bff140014b7 | [Check if a string contains any whitespace]
## Check if a string contains any whitespace
You can use `RegExp.prototype.test()` with a simple regular expression (`/\s/`) to check if at least one whitespace character is present in the given string.
```js
const containsWhitespace = str => /\s/.test(str);
containsWhite... | unknown | unknown | [Check if a string contains any whitespace]
## Check if a string contains any whitespace
You can use `RegExp.prototype.test()` with a simple regular expression (`/\s/`) to check if at least one whitespace character is present in the given string.
```js
const containsWhitespace = str => /\s/.test(str);
containsWhite... | [Check if a string contains any whitespace]
## Check if a string contains any whitespace
You can use `RegExp.prototype.test()` with a simple regular expression (`/\s/`) to check if at least one whitespace character is present in the given string.
```js
const containsWhitespace = str => /\s/.test(str);
containsWhite... | code_snippets | ||
6fc05338-272e-40ae-ab02-96b0b360c812 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/find-remove-compact-whitespace.md | unknown | c05f4d15-936e-404d-9301-6031b720b35c | 3 | SemanticChunker@1.0.0 | 51e7abc439c37f46c7cebca3d2b29fa636e9bdb7ca337627acb5866ba811360d | [Check if a string contains any whitespace > Compact whitespaces in a string]
## Compact whitespaces in a string
Similar to the previous example, you can use `String.prototype.replace()` with a regular expression to replace all occurrences of 2 or more whitespace characters with a single space. You can use the `{2,}`... | unknown | unknown | [Check if a string contains any whitespace > Compact whitespaces in a string]
## Compact whitespaces in a string
Similar to the previous example, you can use `String.prototype.replace()` with a regular expression to replace all occurrences of 2 or more whitespace characters with a single space. You can use the `{2,}`... | [Check if a string contains any whitespace > Compact whitespaces in a string]
## Compact whitespaces in a string
Similar to the previous example, you can use `String.prototype.replace()` with a regular expression to replace all occurrences of 2 or more whitespace characters with a single space. You can use the `{2,}`... | code_snippets | ||
79ba6d9f-a768-42cf-9b8c-a08d10018f4a | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/find-remove-compact-whitespace.md | unknown | c05f4d15-936e-404d-9301-6031b720b35c | 2 | SemanticChunker@1.0.0 | 944ad2bd2a389edf75eaace2cb6786907675aec6ed3191cc291767a8350513dc | [Check if a string contains any whitespace > Remove whitespaces from a string]
## Remove whitespaces from a string
To remove whitespaces from a string, you can simply use `String.prototype.replace()`. For the regular expression to match all whitespace characters, you can use the **global flag** (`g`). You should also... | unknown | unknown | [Check if a string contains any whitespace > Remove whitespaces from a string]
## Remove whitespaces from a string
To remove whitespaces from a string, you can simply use `String.prototype.replace()`. For the regular expression to match all whitespace characters, you can use the **global flag** (`g`). You should also... | [Check if a string contains any whitespace > Remove whitespaces from a string]
## Remove whitespaces from a string
To remove whitespaces from a string, you can simply use `String.prototype.replace()`. For the regular expression to match all whitespace characters, you can use the **global flag** (`g`). You should also... | code_snippets | ||
fdcc700b-fa57-4673-a1ff-7f6be0ffa248 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/find-remove-compact-whitespace.md | unknown | c05f4d15-936e-404d-9301-6031b720b35c | 0 | SemanticChunker@1.0.0 | 0e4a88a4c54ace9920fc2a18beb60363639ac2faf67c6026384f2a839770c395 | ---
title: Check, compact or remove whitespaces in a JavaScript string
shortTitle: Check, compact or remove whitespaces
language: javascript
tags: [string,regexp]
cover: tropical-bike
excerpt: Learn how to work with whitespaces in JavaScript strings, using these simple yet powerful regular expression techniques.
listed... | unknown | unknown | ---
title: Check, compact or remove whitespaces in a JavaScript string
shortTitle: Check, compact or remove whitespaces
language: javascript
tags: [string,regexp]
cover: tropical-bike
excerpt: Learn how to work with whitespaces in JavaScript strings, using these simple yet powerful regular expression techniques.
listed... | ---
title: Check, compact or remove whitespaces in a JavaScript string
shortTitle: Check, compact or remove whitespaces
language: javascript
tags: [string,regexp]
cover: tropical-bike
excerpt: Learn how to work with whitespaces in JavaScript strings, using these simple yet powerful regular expression techniques.
listed... | code_snippets | ||
1c3603a1-1e18-46b1-a097-07e0c3c13a00 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/flatten-unflatten-object.md | unknown | 438712c9-68c0-4efd-936e-987153841ce4 | 1 | SemanticChunker@1.0.0 | e2eb81e4d69e7fd04d1ac4e27e56695b27a01a03dd3074f421704ace59f8d121 | [Flatten an object]
## Flatten an object
Given an object, you can flatten it by **converting every leaf node to a flattened path node**. As this might be hard to grasp, here's an example:
```js
const fileSizes = {
package: 256,
src: {
index: 1024,
styles: {
main: 128,
colors: 16
},
},
assets: {
images: {
... | unknown | unknown | [Flatten an object]
## Flatten an object
Given an object, you can flatten it by **converting every leaf node to a flattened path node**. As this might be hard to grasp, here's an example:
```js
const fileSizes = {
package: 256,
src: {
index: 1024,
styles: {
main: 128,
colors: 16
},
},
assets: {
images: {
... | [Flatten an object]
## Flatten an object
Given an object, you can flatten it by **converting every leaf node to a flattened path node**. As this might be hard to grasp, here's an example:
```js
const fileSizes = {
package: 256,
src: {
index: 1024,
styles: {
main: 128,
colors: 16
},
},
assets: {
images: {
... | code_snippets | ||
6eac9c2c-3add-4141-b419-841c1f39b16e | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/flatten-unflatten-object.md | unknown | 438712c9-68c0-4efd-936e-987153841ce4 | 0 | SemanticChunker@1.0.0 | 4a7aa0953dcf0583638f88d0fc1f6619feafc660c8ceeaaf48b52f376ff90547 | ---
title: Flatten or unflatten a JavaScript object
shortTitle: Flatten or unflatten object
language: javascript
tags: [object,recursion]
cover: lighthouse
excerpt: Learn how to manipulate JavaScript objects by flattening or unflattening them.
listed: true
dateModified: 2024-01-03
---
Data representations often differ... | unknown | unknown | ---
title: Flatten or unflatten a JavaScript object
shortTitle: Flatten or unflatten object
language: javascript
tags: [object,recursion]
cover: lighthouse
excerpt: Learn how to manipulate JavaScript objects by flattening or unflattening them.
listed: true
dateModified: 2024-01-03
---
Data representations often differ... | ---
title: Flatten or unflatten a JavaScript object
shortTitle: Flatten or unflatten object
language: javascript
tags: [object,recursion]
cover: lighthouse
excerpt: Learn how to manipulate JavaScript objects by flattening or unflattening them.
listed: true
dateModified: 2024-01-03
---
Data representations often differ... | code_snippets | ||
9ad7f243-f4d0-4b26-8e89-4947621e9b2f | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/flatten-unflatten-object.md | unknown | 438712c9-68c0-4efd-936e-987153841ce4 | 3 | SemanticChunker@1.0.0 | 626d4499d73a76c0aa71691a25c3d882d04f3c93a46b7e628de2f57f15b65026 | [Flatten an object > Unflatten an object]
## Unflatten an object
Reversing the process is a little bit different. Given a key in the flattened object, you need to **split the path at the delimiter** and use the resulting array to **create nested objects**.
In order to do this, we can use `String.prototype.split()` t... | unknown | unknown | [Flatten an object > Unflatten an object]
## Unflatten an object
Reversing the process is a little bit different. Given a key in the flattened object, you need to **split the path at the delimiter** and use the resulting array to **create nested objects**.
In order to do this, we can use `String.prototype.split()` t... | [Flatten an object > Unflatten an object]
## Unflatten an object
Reversing the process is a little bit different. Given a key in the flattened object, you need to **split the path at the delimiter** and use the resulting array to **create nested objects**.
In order to do this, we can use `String.prototype.split()` t... | code_snippets | ||
eec7963a-af5b-4709-bc08-18869e85fb1e | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/flatten-unflatten-object.md | unknown | 438712c9-68c0-4efd-936e-987153841ce4 | 2 | SemanticChunker@1.0.0 | 8d24378a45323b3e64dfe8058ebbd863db6a3452fa59e2d166b4997eeae55ab0 | [Flatten an object]
In the previous example, we used a **delimiter** of `.` to separate the keys, but this can be customized, using an additional argument. Finally, the last argument is used for the recursive calls and should always be omitted unless you want every key to have a prefix.
```js
const flattenObject = (o... | unknown | unknown | [Flatten an object]
In the previous example, we used a **delimiter** of `.` to separate the keys, but this can be customized, using an additional argument. Finally, the last argument is used for the recursive calls and should always be omitted unless you want every key to have a prefix.
```js
const flattenObject = (o... | [Flatten an object]
In the previous example, we used a **delimiter** of `.` to separate the keys, but this can be customized, using an additional argument. Finally, the last argument is used for the recursive calls and should always be omitted unless you want every key to have a prefix.
```js
const flattenObject = (o... | code_snippets | ||
243dbfe0-6dce-4ea6-861b-827c5c0f2223 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/fibonacci.md | unknown | 143ae4bf-86de-4cc8-9a56-f18277adf55a | 0 | SemanticChunker@1.0.0 | 3605db75f1f6b26b0c358d8a42758484a19c2ff0b01c0b18560fc68768ae7022 | ---
title: Generate the Fibonacci sequence in JavaScript
shortTitle: Fibonacci sequence
language: javascript
tags: [math,algorithm,recursion]
cover: matrix-flow
excerpt: Generate an array, containing the Fibonacci sequence, up until the nth term, using two different approaches.
listed: true
dateModified: 2024-08-17
---... | unknown | unknown | ---
title: Generate the Fibonacci sequence in JavaScript
shortTitle: Fibonacci sequence
language: javascript
tags: [math,algorithm,recursion]
cover: matrix-flow
excerpt: Generate an array, containing the Fibonacci sequence, up until the nth term, using two different approaches.
listed: true
dateModified: 2024-08-17
---... | ---
title: Generate the Fibonacci sequence in JavaScript
shortTitle: Fibonacci sequence
language: javascript
tags: [math,algorithm,recursion]
cover: matrix-flow
excerpt: Generate an array, containing the Fibonacci sequence, up until the nth term, using two different approaches.
listed: true
dateModified: 2024-08-17
---... | code_snippets | ||
254eab59-6dd3-4dba-af83-5ce1044d772a | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/fibonacci.md | unknown | 143ae4bf-86de-4cc8-9a56-f18277adf55a | 2 | SemanticChunker@1.0.0 | 5257e2d94d40f140161430afc92d210dcc868376de7a831664076b43454a521d | [Iterative approach > Recursive approach]
## Recursive approach
The **recursive** approach is more elegant and concise, but it **can be less efficient** due to the overhead of function calls. Instead of a loop, it uses a **function that calls itself** with a smaller input until it reaches the **base case** of either ... | unknown | unknown | [Iterative approach > Recursive approach]
## Recursive approach
The **recursive** approach is more elegant and concise, but it **can be less efficient** due to the overhead of function calls. Instead of a loop, it uses a **function that calls itself** with a smaller input until it reaches the **base case** of either ... | [Iterative approach > Recursive approach]
## Recursive approach
The **recursive** approach is more elegant and concise, but it **can be less efficient** due to the overhead of function calls. Instead of a loop, it uses a **function that calls itself** with a smaller input until it reaches the **base case** of either ... | code_snippets | ||
38de3669-8bd6-428a-b523-d4488d32c462 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/fibonacci.md | unknown | 143ae4bf-86de-4cc8-9a56-f18277adf55a | 1 | SemanticChunker@1.0.0 | 20dab898520c4fdcd4997ec1907b93ae7d91515254ece53e9ece06e0915f21d4 | [Iterative approach]
## Iterative approach
The simplest way to calculate the Fibonacci sequence is to use an **iterative** approach. You can use a `for` loop to generate the sequence up to the nth term, using an array to store the values.
```js
const fibonacci = n => {
let fib = [];
for (let i = 0; i < n; i++) {
... | unknown | unknown | [Iterative approach]
## Iterative approach
The simplest way to calculate the Fibonacci sequence is to use an **iterative** approach. You can use a `for` loop to generate the sequence up to the nth term, using an array to store the values.
```js
const fibonacci = n => {
let fib = [];
for (let i = 0; i < n; i++) {
... | [Iterative approach]
## Iterative approach
The simplest way to calculate the Fibonacci sequence is to use an **iterative** approach. You can use a `for` loop to generate the sequence up to the nth term, using an array to store the values.
```js
const fibonacci = n => {
let fib = [];
for (let i = 0; i < n; i++) {
... | code_snippets | ||
41722dd7-c451-4b3b-b96e-648353dcc5a2 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/find-matching-keys.md | unknown | cd81a170-6ec5-43ca-9503-037021bcbcd3 | 3 | SemanticChunker@1.0.0 | 5b77a0792eabed1c87927ff866e1c115c8e069bfdb14f63e0074e9ccc89327b1 | [Find all matching keys > Find last matching key]
## Find last matching key
Recent versions of JavaScript also added `Array.prototype.findLast()` which can be used to find the last matching key.
```js
const findLastKey = (obj, fn) =>
Object.keys(obj).findLast(key => fn(obj[key], key, obj));
findLastKey(
{
barney... | unknown | unknown | [Find all matching keys > Find last matching key]
## Find last matching key
Recent versions of JavaScript also added `Array.prototype.findLast()` which can be used to find the last matching key.
```js
const findLastKey = (obj, fn) =>
Object.keys(obj).findLast(key => fn(obj[key], key, obj));
findLastKey(
{
barney... | [Find all matching keys > Find last matching key]
## Find last matching key
Recent versions of JavaScript also added `Array.prototype.findLast()` which can be used to find the last matching key.
```js
const findLastKey = (obj, fn) =>
Object.keys(obj).findLast(key => fn(obj[key], key, obj));
findLastKey(
{
barney... | code_snippets | ||
5e3fa19c-5bac-4c2f-b2d0-608c57a6659f | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/find-matching-keys.md | unknown | cd81a170-6ec5-43ca-9503-037021bcbcd3 | 0 | SemanticChunker@1.0.0 | 1ca0112bd58d32cc86af3a0205f67d86c255162ad9b6d74d38df03cb12e193e5 | ---
title: Find matching keys in a JavaScript object
shortTitle: Find matching keys
language: javascript
tags: [object]
cover: beach-riders
excerpt: Find all the keys in a JavaScript object that match the given value.
listed: true
dateModified: 2023-12-15
---
JavaScript objects are commonly used as **dictionaries**, w... | unknown | unknown | ---
title: Find matching keys in a JavaScript object
shortTitle: Find matching keys
language: javascript
tags: [object]
cover: beach-riders
excerpt: Find all the keys in a JavaScript object that match the given value.
listed: true
dateModified: 2023-12-15
---
JavaScript objects are commonly used as **dictionaries**, w... | ---
title: Find matching keys in a JavaScript object
shortTitle: Find matching keys
language: javascript
tags: [object]
cover: beach-riders
excerpt: Find all the keys in a JavaScript object that match the given value.
listed: true
dateModified: 2023-12-15
---
JavaScript objects are commonly used as **dictionaries**, w... | code_snippets | ||
8177a651-a5cb-4562-9b26-2b48d320a838 | unknown | file:///home/sanjeev/Downloads/depthapi/datasets/30-seconds-of-code/content/snippets/js/s/find-matching-keys.md | unknown | cd81a170-6ec5-43ca-9503-037021bcbcd3 | 2 | SemanticChunker@1.0.0 | 2f4bafbb79a01a10f92b63f3cdd6c36332ab59a8a63cbee9f03c91416714b873 | [Find all matching keys > Find first matching key]
## Find first matching key
Using `Array.prototype.filter()` is inefficient if you only need the first matching key. In this scenario, `Array.prototype.find()` is the better choice.
```js
const findKey = (obj, fn) =>
Object.keys(obj).find(key => fn(obj[key], key, ob... | unknown | unknown | [Find all matching keys > Find first matching key]
## Find first matching key
Using `Array.prototype.filter()` is inefficient if you only need the first matching key. In this scenario, `Array.prototype.find()` is the better choice.
```js
const findKey = (obj, fn) =>
Object.keys(obj).find(key => fn(obj[key], key, ob... | [Find all matching keys > Find first matching key]
## Find first matching key
Using `Array.prototype.filter()` is inefficient if you only need the first matching key. In this scenario, `Array.prototype.find()` is the better choice.
```js
const findKey = (obj, fn) =>
Object.keys(obj).find(key => fn(obj[key], key, ob... | code_snippets |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.