When there are several variables and figure out even one of them is undefined, which code is the best one?
variable is like below
const [a, b, c] = [1, '4', undefined]
Array.includes[a, b, c].map(e => typeof e).includes('undefined')
for ... offor (const e of [a, b, c]) {
if (typeof e === 'undefined') {
// 'undefined' found
}
}
It depends on what you mean under "the best code". If you care about readability, you should choose the functional way.
even one of them is undefined
According to the description, the method you are looking for is some.
Also, it's better to check for undefined explicitly, preferring the typeof operator
const array = [1, '4', undefined];
const isOneOfThemIsUndefined = array.some(e => typeof e === 'undefined'))
A simple includes call will do, without the typeof mapping:
[a, b, c].includes(undefined)
const array = [1, '4', undefined];
const isOneOfThemIsUndefined = array.filter(Boolean)
console.log(isOneOfThemIsUndefined)
// => [1, '4',]