Property lookup on booleans

October 29, 2017

Performs a lot of dynamic lookups on the Boolean.prototype, as the ToBoolean operation let’s true pass for obj (which might itself be concerning that this can be a boolean). Instead of the coercion, the code should properly check for valid objects via typeof and strict equality with null comparison.

Example:

Use below

if (typeof obj === 'object' && obj !== null) {
    return obj[key] || 0
}

return 0

Instead of

return (obj && obj[key]) || 0

Profile picture

Written by Anh Thang who lives and works in Hanoi. You can find me on Twitter or Github