or-operator

What does the construct x = x || y mean?

依然范特西╮ 提交于 2019-11-25 22:18:33
问题 I am debugging some JavaScript, and can\'t explain what this || does? function (title, msg) { var title = title || \'Error\'; var msg = msg || \'Error on Request\'; } Can someone give me an hint, why this guy is using var title = title || \'ERROR\' ? I sometimes see it without a var declaration as well. 回答1: It means the title argument is optional. So if you call the method with no arguments it will use a default value of "Error" . It's shorthand for writing: if (!title) { title = "Error"; }