mirror of
https://github.com/Fechin/reference.git
synced 2026-11-21 05:24:10 +03:00
Release the latest generator
This commit is contained in:
1 parent
b520119d38
commit
15c93e54ee
484 files changed
+4969
-856
No files matched your search
@@ -0,0 +1,19 @@
|
||||
---
|
||||
title: How to check if the code is running in nodejs in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- one-line
|
||||
- js
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to check if the code is running in nodejs only using single line of code in JavaScript.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null;
|
||||
```
|
||||
@@ -0,0 +1,19 @@
|
||||
---
|
||||
title: How to check if the code is running in the browser in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- one-line
|
||||
- js
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to check if the code is running in the browser only using single line of code in JavaScript.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const isBrowser = typeof window === 'object' && typeof document === 'object';
|
||||
```
|
||||
@@ -0,0 +1,27 @@
|
||||
---
|
||||
title: How to clear all cookies in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to clear all cookies only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const clearCookies = () => document.cookie.split(';').forEach(c => document.cookie = c.replace(/^ +/, '').replace(/=.*/, `=;expires=${new Date().toUTCString()};path=/`));
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
clearCookies();
|
||||
```
|
||||
@@ -0,0 +1,29 @@
|
||||
---
|
||||
title: How to convert 3 digits color to 6 digits color in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to convert 3 digits color to 6 digits color only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const toFullHexColor = color => `#${(color.startsWith('#') ? color.slice(1) : color).split('').map(c => `${c}${c}`).join('')}`;
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
toFullHexColor('123'); // '#112233'
|
||||
toFullHexColor('#123'); // '#112233'
|
||||
toFullHexColor('#abc'); // '#aabbcc'
|
||||
```
|
||||
@@ -0,0 +1,29 @@
|
||||
---
|
||||
title: How to convert celsius to fahrenheit in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to convert celsius to fahrenheit only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const celsiusToFahrenheit = celsius => celsius * 9/5 + 32;
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
celsiusToFahrenheit(15); // 59
|
||||
celsiusToFahrenheit(0); // 32
|
||||
celsiusToFahrenheit(-20); // -4
|
||||
```
|
||||
@@ -0,0 +1,21 @@
|
||||
---
|
||||
title: How to convert cookie to object in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to convert cookie to object only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const cookies = document.cookie.split(';').map(item => item.split('=')).reduce((acc, [k, v]) => (acc[k.trim().replace('"', '')] = v) && acc, {});
|
||||
```
|
||||
@@ -0,0 +1,28 @@
|
||||
---
|
||||
title: How to convert fahrenheit to celsius in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to convert fahrenheit to celsius only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const fahrenheitToCelsius = fahrenheit => (fahrenheit - 32) * 5/9;
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
fahrenheitToCelsius(59); // 15
|
||||
fahrenheitToCelsius(32); // 0
|
||||
```
|
||||
@@ -0,0 +1,28 @@
|
||||
---
|
||||
title: How to convert hex to rgb in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to convert hex to rgb only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const hexToRgb = hex => hex.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i, (_, r, g, b) => `#${r}${r}${g}${g}${b}${b}`).substring(1).match(/.{2}/g).map(x => parseInt(x, 16));
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
hexToRgb('#00ffff'); // [0, 255, 255]
|
||||
hexToRgb('#0ff'); // [0, 255, 255]
|
||||
```
|
||||
@@ -0,0 +1,33 @@
|
||||
---
|
||||
title: How to convert rgb color to hex in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to convert rgb color to hex only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const rgbToHex = (red, green, blue) => `#${((1 << 24) + (red << 16) + (green << 8) + blue).toString(16).slice(1)}`;
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const rgbToHex = (red, green, blue) => `#${[red, green, blue].map(v => v.toString(16).padStart(2, '0')).join('')}`;
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
rgbToHex(0, 255, 255); // '#00ffff'
|
||||
```
|
||||
@@ -0,0 +1,32 @@
|
||||
---
|
||||
title: How to convert url parameters to object in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to convert url parameters to object only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const getUrlParams = query => Array.from(new URLSearchParams(query)).reduce((p, [k, v]) => Object.assign({}, p, { [k]: p[k] ? (Array.isArray(p[k]) ? p[k] : [p[k]]).concat(v) : v}), {});
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
getUrlParams(location.search); // Get the parameters of the current URL
|
||||
|
||||
getUrlParams('foo=Foo&bar=Bar'); // { foo: "Foo", bar: "Bar" }
|
||||
|
||||
// Duplicate key
|
||||
getUrlParams('foo=Foo&foo=Fuzz&bar=Bar'); // { foo: ["Foo", "Fuzz"], bar: "Bar" }
|
||||
```
|
||||
@@ -0,0 +1,33 @@
|
||||
---
|
||||
title: How to decode a jwt token in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to decode a jwt token only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const decode = token => decodeURIComponent(atob(token.split('.')[1].replace('-', '+').replace('_', '/')).split('').map(c => `%${('00' + c.charCodeAt(0).toString(16)).slice(-2)}`).join(''));
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
decode(`
|
||||
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.
|
||||
eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0I
|
||||
joxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
|
||||
`);
|
||||
|
||||
// { "sub": "1234567890", "name": "John Doe", "iat": 1516239022 }
|
||||
```
|
||||
@@ -0,0 +1,19 @@
|
||||
---
|
||||
title: How to detect dark mode in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- one-line
|
||||
- js
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to detect dark mode only using single line of code in JavaScript.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const isDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
|
||||
```
|
||||
@@ -0,0 +1,48 @@
|
||||
---
|
||||
title: How to easing functions in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to easing functions only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
// Some easing functions
|
||||
// See https://gist.github.com/gre/1650294 and https://easings.net
|
||||
|
||||
const linear = t => t;
|
||||
|
||||
const easeInQuad = t => t * t;
|
||||
const easeOutQuad = t => t * (2-t);
|
||||
const easeInOutQuad = t => t < .5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
|
||||
|
||||
const easeInCubic = t => t * t * t;
|
||||
const easeOutCubic = t => (--t) * t * t + 1;
|
||||
const easeInOutCubic = t => t < .5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
|
||||
|
||||
const easeInQuart = t => t * t * t * t;
|
||||
const easeOutQuart = t => 1 - (--t) * t * t * t;
|
||||
const easeInOutQuart = t => t < .5 ? 8 * t * t * t * t : 1 - 8 * (--t) * t * t * t;
|
||||
|
||||
const easeInQuint = t => t * t * t * t * t;
|
||||
const easeOutQuint = t => 1 + (--t) * t * t * t * t;
|
||||
const easeInOutQuint = t => t < .5 ? 16 * t * t * t * t * t : 1 + 16 * (--t) * t * t * t * t;
|
||||
|
||||
const easeInSine = t => 1 + Math.sin(Math.PI / 2 * t - Math.PI / 2);
|
||||
const easeOutSine = t => Math.sin(Math.PI / 2 * t);
|
||||
const easeInOutSine = t => (1 + Math.sin(Math.PI * t - Math.PI / 2)) / 2;
|
||||
|
||||
const easeInElastic = t => (.04 - .04 / t) * Math.sin(25 * t) + 1;
|
||||
const easeOutElastic = t => .04 * t / (--t) * Math.sin(25 * t);
|
||||
const easeInOutElastic = t => (t -= .5) < 0 ? (.02 + .01 / t) * Math.sin(50 * t) : (.02 - .01 / t) * Math.sin(50 * t) + 1;
|
||||
```
|
||||
@@ -0,0 +1,29 @@
|
||||
---
|
||||
title: How to emulate a dice throw in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to emulate a dice throw only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const throwdice = () => ~~(Math.random() * 6) + 1;
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
throwdice(); // 4
|
||||
throwdice(); // 1
|
||||
throwdice(); // 6
|
||||
```
|
||||
@@ -0,0 +1,24 @@
|
||||
---
|
||||
title: How to encode a url in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to encode a url only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const encode = url => encodeURIComponent(url).replace(/!/g, '%21').replace(/~/g, '%7E').replace(/\*/g, '%2A').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/%20/g, '+');
|
||||
```
|
||||
Parameters `encodeURIComponent` doesn't encode -_.!~*'().
|
||||
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
---
|
||||
title: How to generate an unique and increment id in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to generate an unique and increment id only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const uid = (() => (id = 0, () => id++))();
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
uid(); // 0
|
||||
uid(); // 1
|
||||
uid(); // 2
|
||||
uid(); // 3
|
||||
```
|
||||
@@ -0,0 +1,33 @@
|
||||
---
|
||||
title: How to get the first defined and non null argument in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get the first defined and non null argument only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const coalesce = (...args) => args.find(item => item !== undefined && item !== null);
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const coalesce = (...args) => args.find(item => ![undefined, null].includes(item));
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
coalesce(undefined, null, 'helloworld', NaN); // 'helloworld'
|
||||
```
|
||||
@@ -0,0 +1,27 @@
|
||||
---
|
||||
title: How to get the value of a cookie in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get the value of a cookie only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const cookie = name => `; ${document.cookie}`.split(`; ${name}=`).pop().split(';').shift();
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
cookie('_ga'); // GA1.2.825309271.1581874719
|
||||
```
|
||||
@@ -0,0 +1,27 @@
|
||||
---
|
||||
title: How to get the value of a param from a url in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get the value of a param from a url only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const getParam = (url, param) => new URLSearchParams(new URL(url).search).get(param);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
getParam('http://domain.com?message=hello', 'message'); // 'hello'
|
||||
```
|
||||
@@ -0,0 +1,42 @@
|
||||
---
|
||||
title: How to get type of a variable in string in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get type of a variable in string only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const getTypeOf = obj => Object.prototype.toString.call(obj).match(/\[object (.*)\]/)[1];
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
getTypeOf('hello world'); // String
|
||||
getTypeOf(1000); // Number
|
||||
getTypeOf(Infinity); // Number
|
||||
getTypeOf(true); // Boolean
|
||||
getTypeOf(Symbol()); // Symbol
|
||||
getTypeOf(null); // Null
|
||||
getTypeOf(undefined); // Undefined
|
||||
getTypeOf({}); // Object
|
||||
getTypeOf([]); // Array
|
||||
getTypeOf(/[a-z]/g); // RegExp
|
||||
getTypeOf(new Date(2021)); // Date
|
||||
getTypeOf(new Error()); // Error
|
||||
getTypeOf(function() {}); // Function
|
||||
getTypeOf((a, b) => a + b); // Function
|
||||
getTypeOf(async () => {}); // AsyncFunction
|
||||
getTypeOf(document); // HTMLDocument
|
||||
```
|
||||
@@ -0,0 +1,27 @@
|
||||
---
|
||||
title: How to redirect the page to https if it is in http in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to redirect the page to https if it is in http only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const redirectHttps = () => (location.protocol === 'https:') ? {} : location.replace(`https://${location.href.split('//')[1]}`);
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const redirectHttps = () => (location.protocol === 'https:') ? {} : (location.protocol = 'https:');
|
||||
```
|
||||
@@ -0,0 +1,33 @@
|
||||
---
|
||||
title: How to run promises in sequence in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to run promises in sequence only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const run = promises => promises.reduce((p, c) => p.then(rp => c.then(rc => [...rp, rc])), Promise.resolve([]));
|
||||
```
|
||||
Parameters `promises` is an array of `Promise`.
|
||||
|
||||
|
||||
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
run(promises).then((results) => {
|
||||
// `results` is an array of promise results in the same order
|
||||
});
|
||||
```
|
||||
@@ -0,0 +1,42 @@
|
||||
---
|
||||
title: How to swap two variables in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to swap two variables only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
[a, b] = [b, a];
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
a = [b, b = a][0];
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
a = (x => x)(b, b = a);
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
// (only works with numbers)
|
||||
a = b + (b = a, 0);
|
||||
|
||||
a = b * (b = a, 1);
|
||||
```
|
||||
@@ -0,0 +1,21 @@
|
||||
---
|
||||
title: How to wait for an amount of time in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to wait for an amount of time only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const wait = async (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds));
|
||||
```
|
||||
Reference in new issue
Block a user