Home > Enterprise >  Javascript is there a way to get the null or undefined in string format?
Javascript is there a way to get the null or undefined in string format?

Time:09-29

I can test null or undefined with ?? operator :

let test = undefined;
let result = test ?? "null or undefined"

But can I actually get "null" if it is null or "undefined" if it is undefined using ?? somehow ?

CodePudding user response:

Just cast to string?

const test = undefined;
const result = test ?? String(test);
console.log(result);

  • Related