Search code examples
javascriptbooleanswitch-statementgate

Or gate (||) sorts false as undefined


I have a code like this:

var IFS =
    document.isFullScreen || 
    document.webkitIsFullScreen || 
    document.mozIsFullScreen || 
    document.msIsFullScreen;

The exact problem is that the || gates sorts false as undefined, since isFullScreen is a boolean.

How do I fix this?


Solution

  • If every element is undefined, then IFS will be undefined. Since undefined is a falsy value, you can still make conditional statements as:

    var IFS =
        document.isFullScreen || 
        document.webkitIsFullScreen || 
        document.mozIsFullScreen || 
        document.msIsFullScreen;
    
    if(!IFS){
        console.log('not full screen');
    }
    

    If you still want to hold the false value in case every other variable is undefined, then you can go with something like:

    var IFS =
        document.isFullScreen || 
        document.webkitIsFullScreen || 
        document.mozIsFullScreen || 
        document.msIsFullScreen || 
        false;