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);
}
1
  • if (!this.activeAppKey || this.activeAppKey.trim().length === 0) { this.activeAppKey = sessionStorage.getItem(AppConstants.APP_KEY); } Sep 18, 2020 at 9:03

8 Answers 8

7

import it from 'is-what'

so,

import { isUndefined, isNullOrUndefined } from 'util';

will become,

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

0
4

try using ! operator.

if (!this.activeAppKey) {
  this.activeAppKey = sessionStorage.getItem(AppConstants.APP_KEY);
}
6
  • 3
    Using ! operator can cause some troubles, when variable is number 0, empty string etc. See this answer: stackoverflow.com/questions/28975896/… Feb 11, 2019 at 9:47
  • it produces errors for codes like ` if (!isNullOrUndefined(user.customRoles[this.activeAppKey])) { ui.role = user.customRoles[this.activeAppKey].roleKey; }`
    – user9520310
    Feb 12, 2019 at 5:57
  • Have you checked user itself is null?
    – Ramesh
    Feb 13, 2019 at 6:28
  • 1
    Might be a good idea to explain Javascript truthy. Sep 18, 2020 at 9:02
  • you can't use ! operator. because this will not allow you to enter in if condition body for this.activeAppKey value is false instead if null or undefined. Sep 8, 2021 at 12:49
4

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

1

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.

1

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
0

try this:

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

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;
  }
}

0

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy