Skip to content

Instantly share code, notes, and snippets.

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',
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]
const promise = (duration = 0) => {
return new Promise((resolve, reject) => {
setTimeout(resolve, duration)
})
}
promise(1000)
.then(() => {
console.log('1 sec')
return promise(500)
// 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
// 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
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
}
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 }
}
// 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)
// 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')
class Animal {
constructor(name) {
this.name = name
}
get getName() {
return this.name
}
set setName(newName) {
this.name = newName
}