JavaScript OR (||) variable assignment explanation

后端 未结 12 2399
北恋
北恋 2020-11-21 06:41

Given this snippet of JavaScript...

var a;
var b = null;
var c = undefined;
var d = 4;
var e = \'five\';

var f = a || b || c || d || e;

alert(f); // 4
         


        
12条回答
  •  -上瘾入骨i
    2020-11-21 07:14

    See short-circuit evaluation for the explanation. It's a common way of implementing these operators; it is not unique to JavaScript.

提交回复
热议问题