Quick Feature Detection in JavaScript
If you want to create an object that you can use to query features you can do so like this :-
const featureDetection = { hasFill: 'fill' in Array, hasFrom: 'from' in Array } console.log(featureDetection.hasFill) // true if Array has fill (ES2015) console.log(featureDetection.hasFrom) // true if Array has from (ES2015) if(!featureDetection.hasFill) { // handle the case where we can't use fill (i.e. polyfill) } if(!featureDetection.hasFrom) { // handle the case where we can't use from (i.e. polyfill) }
in
returns true if the specified property is on the specified object (or it's prototype chain)
Of course, there are more standard ways of doing this like Modernizr or @babel/preset-env if you are feeling particulary fancy but I thought this was neat little use case when you don't want to bring in a heavy dependency.