How to check undefined variable in a ternary operator?

后端 未结 3 1369
别那么骄傲
别那么骄傲 2021-02-14 17:02

I have an issue with ternary operation:

let a = undefined ? \"Defined!\" : \"Definitely Undefined\",
    b = abc ? \"Defined!\" : \"Definitely Undefined\", // Re         


        
3条回答
  •  忘掉有多难
    2021-02-14 17:08

    What is the best and short way to check if abc is undefined before accessing its properties as well as assign blank object {} if undefined?

    And you said that

    I am currently using (typeof abc !== "undefined")

    Looks like you have abc defined and the value is undefined.

    You can try doing

    var a = abc || {};
    console.log(a);
    

提交回复
热议问题