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
| import { useCallback, useState } from "react"; | |
| function Child({ calc }) { | |
| return <p>{`Total: ${calc()}`}</p>; | |
| } | |
| export default function Parent() { | |
| const [a, setA] = useState(0); | |
| const [b, setB] = useState(0); | |
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 calc(x, y) { | |
| return x + y; | |
| } | |
| const memoizedCalc = useCallback(() => { | |
| return calc(a, b); | |
| }, [a, b]); |
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 initialState; | |
| class User { | |
| #isActive = false; | |
| get getState(){ | |
| if(!this.#isActive){ | |
| throw new Error('User is not active'); | |
| } | |
| return this.#isActive; |
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
| class User { | |
| isActive = false; | |
| get getStatus(){ | |
| if(!this.#isActive){ | |
| throw new Error('User is not active'); | |
| } | |
| return this.#isActive; | |
| } | |
| } |
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 users; | |
| if (userType === 'ADMIN') { | |
| users = await import('https://example.com/users/1'); | |
| } else { | |
| users = await import('https://example.com/users/2'); | |
| } |
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
| class User {} | |
| User.username = "Sean" |
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 date = Temporal.Now.plainDateISO(); // Gets the current date | |
| date.toString(); // returns the date in ISO 8601 date format | |
| // If you additionally want the time: | |
| Temporal.Now.plainDateTimeISO().toString(); // date and time in ISO 8601 format |
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
| try { | |
| const result = 2 / 0; | |
| console.log(result); | |
| } catch (error) { | |
| console.err(error); | |
| throw new Error('Something went wrong', { cause: error }); | |
| } |
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 fruits ="Fruits:apples, oranges, pears"; | |
| const regex = /(apples)/gd; | |
| const matches = [...fruits.matchAll(regex)]; | |
| console.log(matches[0]); |
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 person = { | |
| hasOwnProperty: function() { | |
| return false; | |
| }, | |
| age: 21 | |
| }; | |
| if (Object.hasOwn(person, 'age')) { | |
| console.log(person.age); // true - the remplementation of hasOwnProperty() did not affect the Object | |
| } |