Skip to content

ui: Simplify code #4956

@davidjumani

Description

@davidjumani

Optional chaining

The optional chaining ?. is a safe way to access nested object properties, even if an intermediate property doesn’t exist.
(error.response && error.response.headers && error.response.headers['x-description']) || error.message
using optional chaining it reduces to
error.response?.headers?.['x-description'] || error.message

Ref : https://javascript.info/optional-chaining

This can also be used to invoke methods on objects, etc
This improves readability as well as simplifies the existing code

Nullish coalescing

Nullish coalescing is an improvement on the || operator where it only checks if an object is null or undefined.
Using the || operator returns false if the object is zero or an empty string which can cause issues in the code when the value is zero but incorrectly treated as false

let height = 0;

alert(height || 100); // 100
alert(height ?? 100); // 0

Ref: https://javascript.info/nullish-coalescing-operator

Object shorthand

Provides a simpler way when creating an object who's keys have the same name as the variables passed-in as properties

let cat = 'Miaow';
let dog = 'Woof';
let bird = 'Peet peet';

let someObject = {
  cat,
  dog,
  bird
}

Ref https://alligator.io/js/object-property-shorthand-es6/

ISSUE TYPE
  • Enhancement Request

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions