optional-chaining-js
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Introduction

Optional Chaining functionality for vanilla javascript Prevent error 'Cannot read property of undefined' or 'Cannot read property of null'.

Handle null or undefined parent property when accessing child properties in an object.

Installation

npm install optional-chaining-js --save

Usage

import { optionalChaining } from 'optional-chaining-js';

// Prevent error 'Cannot read property of null'
// by handling null/undefined parent properties
let customer = { info: null };
optionalChaining(customer, 'info.address.city'); // undefined

let customer = { info: { address: undefined } };
optionalChaining(customer, 'info.address.city'); // undefined


let customer = {
    info: {
        name: 'John Doe',
        address: {
            country: 'Indonesia',
            province: 'DKI Jakarta',
            city: 'Jakarta'
        }
    }
};
optionalChaining(customer, 'info.address.city'); // Jakarta

Package Sidebar

Install

npm i optional-chaining-js

Weekly Downloads

0

Version

1.0.1

License

ISC

Unpacked Size

7.7 kB

Total Files

13

Last publish

Collaborators

  • rizkirachyan