Succinct/concise syntax for ‘optional’ object keys in ES6/ES7?

You can use object spread to have an optional property: let flag1 = true; let flag2 = false; // extra cases added by Abdull let optionalKey8 = 8; let optionalKey9 = undefined; let optionalKey10 = false; let optionalKey11 = null; let optionalKey12 = “twelve”; const obj = { requiredKey1: 1, requiredKey2: 2, …(flag1 && { … Read more

How do I destructure all properties into the current scope/closure in ES2015?

I think you’re looking for the with statement, it does exactly what you are asking for: const vegetableColors = {corn: ‘yellow’, peas: ‘green’}; with (vegetableColors) { console.log(corn);// yellow console.log(peas);// green } However, it is deprecated (in strict mode, which includes ES6 modules), for good reason. destructure all properties into the current scope You cannot in … Read more

IE does not support Array includes or String includes methods

Because it’s not supported in IE, it is not supported also in Opera (see the compatibility table), but you can use the suggested polyfill: Polyfill This method has been added to the ECMAScript 2015 specification and may not be available in all JavaScript implementations yet. However, you can easily polyfill this method: if (!String.prototype.includes) { … Read more

Find all matching elements with in an array of objects [duplicate]

Two things: first, Array.find() returns the first matching element, undefined if it finds nothing. Array.filter returns a new array containing all matching elements, [] if it matches nothing. Second thing, if you want to match 4,5, you have to look into the string instead of making a strict comparison. To make that happen we use … Read more

ES2015/2016 way of ‘typeof varName === ‘undefined`?

Just check for varName === undefined. In older browsers it was possible to assign an alternate value to the global undefined variable causing that test to fail, but in ES2015+ that’s now impossible. Note that there’s no way to distinguish explicitly passing undefined as a parameter from leaving the parameter out altogether other than by … Read more

Best way to polyfill ES6 features in React app that uses create-react-app

Update: The create-react-app polyfill approach and docs have changed since this question/answer. You should now include react-app-polyfill (here) if you want to support older browsers like ie11. However, this only includes the “…minimum requirements and commonly used language features“, so you’ll still want to use one of the approaches below for less common ES6/7 features … Read more

Can you use es6 import alias syntax for React Components?

Your syntax is valid. JSX is syntax sugar for React.createElement(type) so as long as type is a valid React type, it can be used in JSX “tags”. If Button is null, your import is not correct. Maybe Button is a default export from component-library. Try: import {default as StyledButton} from “component-library”; The other possibility is … Read more

Why is Math.pow() (sometimes) not equal to ** in JavaScript?

99**99 is evaluated at compile time (“constant folding”), and the compiler’s pow routine is different from the runtime one. When evaluating ** at run time, results are identical with Math.pow — no wonder since ** is actually compiled to a Math.pow call: console.log(99**99); // 3.697296376497268e+197 a = 99, b = 99; console.log(a**b); // 3.697296376497263e+197 console.log(Math.pow(99, … Read more