Skip to content

Instantly share code, notes, and snippets.

View gilles-gardet's full-sized avatar
🏠
Working from home

Gilles gilles-gardet

🏠
Working from home
View GitHub Profile
// Using !! operator easily convert value in boolean
console. log ('This is not Empty: ', !!'') // This is not Empty: false
console. log ('This is not Empty: ', !!'Data') // This is not Empty: true
let data = undefined ?? 'noData'
console.log(data) // noData
data = null ?? 'noData'
console.log(data) // noData
data = '' ?? 'noData'
console.log(data) // ''
data = 0 ?? null ?? 'noData'
// we can convert number to string using '+' operator
const personNo = '238932'
console.log('personNo: ', +personNo, 'typeOf: ', typeof +personNo)
// we can concert string to number using + operator followed by empty string ''
const personId = 324743
const fruitList = ['apple', 'mango']
console.log(typeof fruitList) // object
console.log(Array.isArray(fruitList)) // true
// we can remove duplicate values using 'Set'
const fruitList = ['apple', 'mango', 'apple', 'grapes']
const uniqFruitList = [...new Set(fruitList)]
console.log(fruitList) // ['apple', 'mango', 'grapes']
// wen can use 'Boolean' methodto test truthy values
const fruitList = ['apple', null 'mango', undefined, '']
// filter falsy values
const filterFruitList = fruitList.filter(Boolean)
console.log(filterFruitList) // ['apple', 'mango']
// check if any truthy value is in array
// we access both key and value usign object entries method
const data = {id: 1, name: 'Laptop', isSale: true}
Object.entries(data).forEach(([key, value]) => {
if (['id', 'name']).includes(key)) {
console.log('key: ', key, 'value: ', value)
}
})
// key: id value: 1
// we can also give alias name of variable when destructuring
const productData = {id: 23, name: 'Laptop'}
const {name: deviceName} = productData
console.log('deviceName', deviceName) // deviceName Laptop
// here destructuring value with dynamic key
const extractKey = 'name'
const { [extractKey]: data } = productData
// we can use 'in' keyword to check property exists in object
const person = {
id: 'ab32',
name : 'Krina'
}
console.log('name' in person) // true
console.log('isActive' in person) // false