loading...
Cover image for Javascript ES6: Destructuring with default values
PROTOTYP

Javascript ES6: Destructuring with default values

adrianbdesigns profile image Adrian Bece Originally published at blog.prototyp.digital ・1 min read

This is a short, but very useful code snippet for destructuring variables with default values (if the value is undefined). Very useful snippet for avoiding errors caused by undefined variables.

In the following example, the options object is being destructured.

const {
    valFirst = 1,
    valSecond = "hello",
    valThird = false
  } = options;

If a destructured value doesn't exist within the object, it will be assigned a default value. If it does exist within the object, it will be assigned the value from the object.

Thank you for taking the time to read this post. If you've found this useful, please give it a ❤️ or 🦄, share and comment.

Posted on by:

adrianbdesigns profile

Adrian Bece

@adrianbdesigns

React, Frontend, Magento 2 certified developer. Magento PWA Studio contributor. Rock and metal music fan. Reads Dune, sci-fi novels and Calvin & Hobbes. Creates amazing interfaces @ prototyp.digital

PROTOTYP

A software company with a design first approach. We design, build, and ship app interfaces, native and web apps and interactive products for startups and enterprises.

Discussion

markdown guide