简体   繁体   中英

Which is right way to use below statement performance wise?

Which approach is most recommended. First or Second ?

var x = document.querySelectorAll("span");

// #1:  Console returns false
x.length > 0 && x[0].blur();

// #2:  Console returns undefined
if (x.length > 0) {
  x[0].blur();
}

If you find x.length > 0 then you will definitely get x[0] and do not need to check && x[0].blur(); . So your second approach is much better.

if (x.length > 0) {
  x[0].blur();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM