I'm newbie and trying to study javascript myself.
There is an example:
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop
I just wonder what does the symbol " ||" do? Thank you! Appreciate your help.
I'm newbie and trying to study javascript myself.
There is an example:
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop
I just wonder what does the symbol " ||" do? Thank you! Appreciate your help.
It means that you are trying to get the document.documentElement.scrollTop
function but if it returns undefined
(because the function is not supported in the given browser) it will use the document.body.scrollTop
function instead.
If document.documentElement.scrollTop
is undefined
or null
,scrollTop=document.body.scrollTop
Here ||
Logical OR operator.
Logical OR operator returns the first value of first operand if that is truthy
otherwise it returns the second operand.
The above statement is same as
if(document.documentElement.scrollTop){
var scrollTop = document.documentElement.scrollTop
}
else{
var scrollTop = document.body.scrollTop
}