Is it possible to check for null inline in javascript?

前端 未结 7 2287
孤街浪徒
孤街浪徒 2020-12-15 18:27

I have a function which parses the address components of the Google Maps API JSON and then returns the city / locality / route name.

The getAddre

相关标签:
7条回答
  • 2020-12-15 19:21

    For empty strings you can use !:

    var foo = 'yo';
    console.log(!foo);
    
    var foo = null;
    console.log(!foo);

    And for the ? you asked about, it's the Conditional (ternary) Operator, the syntax is condition ? if true : if false you can use it as follows:

    var foo = 'yo';
    console.log('1 : ' + (!foo ? 'Null' : 'Not Null'));
    console.log('2 : ' + (foo === null ? 'Null' : 'Not Null'));
    console.log('3 : ' + (foo == null ? 'Null' : 'Not Null'));
    
    var foo = null;
    console.log('1 : ' + (!foo ? 'Null' : 'Not Null'));
    console.log('2 : ' + (foo === null ? 'Null' : 'Not Null'));
    console.log('3 : ' + (foo == null ? 'Null' : 'Not Null'));

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