'what is the alternative for the util.isNullOrUndefined(object) in the Util module ? Angular 7

util.isNullOrUndefined(object) has been depreciated and I cannot find any alternative for it. Can someone point out a feasible alternative for it?

if (isNullOrUndefined(this.activeAppKey) || this.activeAppKey.trim().length === 0) {
  this.activeAppKey = sessionStorage.getItem(AppConstants.APP_KEY);
}


Solution 1:[1]

try using ! operator.

if (!this.activeAppKey) {
  this.activeAppKey = sessionStorage.getItem(AppConstants.APP_KEY);
}

Solution 2:[2]

Why not simply create you own:

export function isNullOrUndefined(value: any) {
    return value === null || value === undefined;
}

Lets say you create a tools.ts file and write the above code:

You can use it in your component as follows :

import { isNullOrUndefined } from 'tools';

The above code will work no changes there. Plus it will reusable

Solution 3:[3]

import it from 'is-what'

so,

import { isUndefined, isNullOrUndefined } from 'util';

will become,

import { isUndefined, isNullOrUndefined } from 'is-what';

Solution 4:[4]

try this:

if (this.activeAppKey === null || this.activeAppKey === undefined || this.activeAppKey.trim().length === 0) {
  this.activeAppKey = sessionStorage.getItem(AppConstants.APP_KEY);
}

Solution 5:[5]

I would go for this approach. Create some util class and add this as a static method.

export class Util {
  static isNullOrUndefined<T>(obj: T | null | undefined): obj is null | undefined {
    return typeof obj === 'undefined' || obj === null;
  }
}

Solution 6:[6]

You can use the isNullOrUndefined function from the core-util-is npm package.

It was deprecated from nodejs core because behaviour or api changes on these functions could break a lot of existing code without notifying the users. In a npm package, using semver, only a major version bump can break api/behaviour. A lot safer this way.

Solution 7:[7]

From NodeJs reference [https://nodejs.org/api/util.html#utilisnullorundefinedobject] :

Deprecated: Use value === undefined || value === null instead.

This makes the answer given by super IT guy above correct (https://stackoverflow.com/a/56138823/6757814)

Solution 8:[8]

Using a double equals on a null checks for null and undefined exclusively

if (this.activeAppKey == null || this.activeAppKey.trim().length === 0)

null == undefined // true
null == null      // true
null == 0         // false
null == ""        // false
null == false     // false

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ramesh
Solution 2 Jatin Rane
Solution 3 Manoj TM
Solution 4 super IT guy
Solution 5
Solution 6 Romain Durand
Solution 7 Rodjf
Solution 8 BrennenRocks