2017-04-18 81 views
-1

在w3school上,我讀了關於js窗口並遇到這樣一段代碼。什麼意思是「||」在初始化變量

var h = window.innerHeight 
|| document.documentElement.clientHeight 
|| document.body.clientHeight; 

這是什麼意思?

+1

https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Expressions_and_Operators#Logical_operators – zerkms

+0

強制性閱讀:[爲什麼不w3schools.com?](https://meta.stackoverflow.com/q/280478) – Siguza

回答

1

這意味着如果window.innerHeight是falsy(例如nullundefined0''NaNfalse),然後使用document.documentElement.clientHeight,並且如果這是falsy,然後使用document.body.clientHeight

+3

falsy values include null,false,undefined,0,「」(空字符串),NaN – TurtleTread

+0

當我還年輕的時候,我會用if(!someVar)當someVar是數字並且可以是0 。很高興知道會發生什麼事情可能會導致麻煩! –

+0

謝謝,更新的答案包括完整的虛假列表。 – RJM