This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sum() { | |
sum.a ??= 10; | |
sum.b ??= 20; | |
return sum.a + sum.b; | |
} | |
console.log(sum()); // 30 | |
sum.a = 30; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sum() { | |
let result = 0; | |
for (const argument of arguments) { | |
result += argument; | |
} | |
return result; | |
} | |
sum(5, 8); // 13 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log(Math.ceil(0.5)); // 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with (Math) { | |
console.log(ceil(0.5)); // 1 | |
} | |
// Out of 'with (Math) {...}', it returns an error | |
console.log(ceil(0.5)); // Uncaught ReferenceError: ceil is not defined |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let a = null; | |
a ??= 10; | |
console.log(a); // "10" | |
let b = "something"; | |
b ??= 10; | |
console.log(b); // "something" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const x = null; | |
console.log(x ?? "Hey"); // "Hey" | |
const y = 10; | |
console.log(y ?? "Something"); // 10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const crypto = require("crypto"); | |
const secret = "YOUR_SECRET_KEY" // Replace this with the key from static-auth-secret | |
function generateTurnKey() { | |
// The username is a timestamp that represents the expiration date of this credential | |
// In this case, it's valid for 12 hours (change the '12' to how many hours you want) | |
const username = (Date.now() / 1000 + 12 * 3600).toString(); | |
// Now create the corresponding credential based on the secret |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function App() { | |
const [varA, setVarA] = useState(0); | |
useEffect(() => { | |
if (varA > 0) return; | |
const timeout = setTimeout(() => setVarA(varA + 1), 1000); | |
return () => clearTimeout(timeout); | |
}, [varA]); // Great, we have our dependency array correctly set |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function App() { | |
const [varA, setVarA] = useState(0); | |
useEffect(() => { | |
const timeout = setTimeout(() => setVarA(varA + 1), 1000); | |
return () => clearTimeout(timeout); | |
}, []); // Avoid this: varA is not in the dependency array! | |
return <span>Var A: {varA}</span>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function App() { | |
const [varA, setVarA] = useState(0); | |
useEffect(() => { | |
if (varA >= 5) return; | |
const timeout = setTimeout(() => setVarA(varA + 1), 1000); | |
return () => clearTimeout(timeout); | |
}, [varA]); |
NewerOlder