This file contains 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 symbol1 = Symbol() | |
const symbol2 = Symbol('id') | |
const symbol3 = Symbol('id') | |
// Each symbol is unique | |
console.log(symbol2 !== symbol3) | |
// Symbols could be used as object keys | |
const obj = { | |
[symbol1]: 'War', |
This file contains 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 target = {} | |
// Create proxy with no interceptors | |
const proxy = new Proxy(target, {}) | |
proxy.test = 5 | |
console.log(target.test) // Expected output: 5 | |
console.log(proxy.test) // Expected output: 5 | |
const arr = [1, 2, 3] |
This file contains 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 promise = (duration = 0) => { | |
return new Promise((resolve, reject) => { | |
setTimeout(resolve, duration) | |
}) | |
} | |
promise(1000) | |
.then(() => { | |
console.log('1 sec') | |
return promise(500) |
This file contains 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
// String methods | |
const str = 'my string' | |
console.log(str.repeat(2)) // Expected output: my stringmy string, | |
console.log(str.startsWith('my')) // Expected output: true | |
console.log(str.startsWith('st')) // Expected output: false | |
console.log(str.startsWith('my', 1)) // Expected output: false | |
console.log(str.endsWith('ing')) // Expected output: true | |
console.log(str.endsWith('st')) // Expected output: false |
This file contains 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
// lib.js | |
export const PI = Math.PI | |
const sum = (a, b) => a + b | |
export default sum | |
// file.js | |
import sum, { PI } from 'lib.js' | |
console.log(sum(1, PI)) // Expected output: 4.141592654 | |
// lib2.js |
This file contains 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* generateSequence(start, end) { | |
for (let i = start; i <= end; i++) yield i | |
} | |
const sequence1 = generateSequence(1, 10) | |
for (const n of sequence1) { | |
console.log(n) // Expected output: sequence from 1 to 10 | |
} |
This file contains 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 fibonacci = { | |
// Iterated objects must implement [Symbol.iterator] method | |
// which returns object with next method | |
[Symbol.iterator]() { | |
let pre = 0, cur = 1 | |
return { | |
next() { | |
[pre, cur] = [cur, pre + cur] | |
return { done: false, value: cur } | |
} |
This file contains 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
// Set - collection with unique values | |
const set = new Set() | |
const john = 'John' | |
const diana = 'Diana' | |
const kate = 'Kate' | |
set.add(john) | |
set.add(diana) | |
set.add(kate) |
This file contains 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
// Map - data structure with key-value pairs, where keys can be any type | |
const map = new Map() | |
map.set('1', 'string') | |
.set(1, 'number') | |
.set(true, 'boolean') | |
const obj = { name: 'John' } | |
map.set(obj, 'John') |
This file contains 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 Animal { | |
constructor(name) { | |
this.name = name | |
} | |
get getName() { | |
return this.name | |
} | |
set setName(newName) { | |
this.name = newName | |
} |
NewerOlder