Skip to content

Instantly share code, notes, and snippets.

@NovoManu
Last active March 15, 2021 07:54
Show Gist options
  • Save NovoManu/f786909fc41d9ad340898da421d13d43 to your computer and use it in GitHub Desktop.
Save NovoManu/f786909fc41d9ad340898da421d13d43 to your computer and use it in GitHub Desktop.
// 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
console.log(str.endsWith('ing', 10)) // Expected output: true
console.log(str.includes('string')) // Expected output: true
// Number methods
const num1 = 10
const num2 = 9007199254740992
console.log(Number.isNaN(num1)) // Expected output: false
console.log(Number.isNaN(num1/'hello')) // Expected output: true
console.log(Number.isFinite(Infinity)) // Expected output: false
console.log(Number.isFinite(num1)) // Expected output: true
console.log(Number.isSafeInteger(num1)) // Expected output: true
console.log(Number.isSafeInteger(num2)) // Expected output: false
console.log(Math.abs(0.2 - 0.3 + 0.1) < Number.EPSILON) // Expected output: true
console.log(Math.trunc(5.5)) // Expected output: 5
console.log(Math.sign(7)) // Expected output: 1
console.log(Math.sign(-7)) // Expected output: -1
console.log(Math.sign(0)) // Expected output: 0
// Array methods
const arr = [{ name: 'Joe' }, { name: 'Beth' }]
console.log(arr.find(({ name }) => name === 'Beth')) // Expected output: { name: 'Beth' }
console.log(arr.findIndex(({ name }) => name === 'Beth')) // Expected output: 1
const set = new Set()
set.add('Joe')
set.add('Beth')
console.log(Array.from(set)) // Expected output: ["Joe", "Beth"]
console.log(Array.of(1, 2, 3)) // Expected output: [1, 2, 3]
console.log(Array.from(Array(3).fill(null))) // Expected output: [null, null, null]
console.log(Array.of(1, 2, 3).copyWithin(1, 0)) // Expected output: [1, 1, 2]
for (const [key, value] of Array.from(set).entries()) {
console.log({ key, value })
}
for (const key of Array.from(set).keys()) {
console.log(key)
}
for (const key of Array.from(set).values()) {
console.log(key)
}
// Object methods
const beth = { name: 'Beth', age: 25 }
const john = Object.assign(beth, { name: 'John' })
console.log(john) // Expected output: { name: 'John', age: 25 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment