if (!var) - What does it mean exactly? Let's Explore with TypeScript
Caner Demirci

Caner Demirci @canerdemirci

About: I write code as a hobby for now. * C#, Flutter, Unity, HTML-CSS-JS *

Location:
Bursa, Turkey
Joined:
Sep 15, 2022

if (!var) - What does it mean exactly? Let's Explore with TypeScript

Publish Date: Mar 20 '23
0 0

if (!variable) What does it mean exactly? I think there's no need to explain much about this. This piece of code show us enough.

const nameof = (n: any) : string => Object.keys({n})[0];

const valueOrType = (n: any) : any => {
  if (n === undefined)
    return 'undefined';

  if (n === null)
    return 'null';

  if (n === '')
    return 'empty string';
}

const isFalsy = (n: any) : boolean => !n ? true : false;
const logInputIsFalsy = (inp: any) => console.log(`[${nameof(inp)}] - value : [${valueOrType(inp)}] - is ${isFalsy(inp) ? 'falsy' : 'not falsy'}`);

logInputIsFalsy('');
logInputIsFalsy(null);
logInputIsFalsy(undefined);
logInputIsFalsy(false);
logInputIsFalsy(0);
logInputIsFalsy('word');
logInputIsFalsy(1);
Enter fullscreen mode Exit fullscreen mode

Console outputs:

  • "[n] - value : [empty string] - is falsy"
  • "[n] - value : [null] - is falsy"
  • "[n] - value : [undefined] - is falsy"
  • "[n] - value : [false] - is falsy"
  • "[n] - value : [0] - is falsy"
  • "[n] - value : [word] - is not falsy"
  • "[n] - value : [1] - is not falsy"

Comments 0 total

    Add comment