How to check undefined in Typescript

前端 未结 9 954
面向向阳花
面向向阳花 2021-02-04 23:05

I am using this code to check undefined variable but it\'s not working.

相关标签:
9条回答
  • 2021-02-04 23:33

    Use 'this' keyword to access variable. This worked for me

    var  uemail = localStorage.getItem("useremail");
    
    if (typeof this.uemail === "undefined")
    {
        alert('undefined');
    }
    else
    {
        alert('defined');
    }
    
    0 讨论(0)
  • 2021-02-04 23:34

    In Typescript 2 you can use Undefined type to check for undefined values. So if you declare a variable as:

    let uemail : string | undefined;
    

    Then you can check if the variable z is undefined as:

    if(uemail === undefined)
    {
    
    }
    
    0 讨论(0)
  • 2021-02-04 23:37

    Late to the story but I think some details are overlooked?

    if you use

    if (uemail !== undefined) {
      //some function
    }
    

    You are, technically, comparing variable uemail with variable undefined and, as the latter is not instantiated, it will give both type and value of 'undefined' purely by default, hence the comparison returns true. But it overlooks the potential that a variable by the name of undefined may actually exist -however unlikely- and would therefore then not be of type undefined. In that case, the comparison will return false.

    To be correct one would have to declare a constant of type undefined for example:

    const _undefined: undefined
    

    and then test by:

    if (uemail === _undefined) {
      //some function
    }
    

    This test will return true as uemail now equals both value & type of _undefined as _undefined is now properly declared to be of type undefined.

    Another way would be

    if (typeof(uemail) === 'undefined') {
      //some function
    }
    

    In which case the boolean return is based on comparing the two strings on either end of the comparison. This is, from a technical point of view, NOT testing for undefined, although it achieves the same result.

    0 讨论(0)
  • 2021-02-04 23:45

    It's because it's already null or undefined. Null or undefined does not have any type. You can check if it's is undefined first. In typescript (null == undefined) is true.

      if (uemail == undefined) {
          alert('undefined');
      } else {
          alert('defined');
      }
    

    or

      if (uemail == null) {
          alert('undefined');
      } else {
          alert('defined');
      }
    
    0 讨论(0)
  • 2021-02-04 23:48

    You can just check for truthy on this:

    if(uemail) {
        console.log("I have something");
    } else {
        console.log("Nothing here...");
    }
    

    Go and check out the answer from here: Is there a standard function to check for null, undefined, or blank variables in JavaScript?

    Hope this helps!

    0 讨论(0)
  • 2021-02-04 23:49

    NOT STRICTLY RELATED TO TYPESCRIPT

    Just to add to all the above answers, we can also use the shorthand syntax

    var result = uemail || '';
    

    This will give you the email if uemail variable has some value and it will simply return an empty string if uemail variable is undefined.

    This gives a nice syntax for handling undefined variables and also provide a way to use a default value in case the variable is undefined.

    0 讨论(0)
提交回复
热议问题