Skip to content Skip to sidebar Skip to footer

Logical And (&&) And Or (||) Operators

Logical AND (&&) and OR (||) operators --- who knew they could trick us like this :) Their definition, for JS (according to this explanation), is the following: expr1 &

Solution 1:

It's just how they work. It's not a bug:

Returns expr1 if it can be converted to false; otherwise, returns expr2

This means you can use "default values", like this:

functionsomeFunc(passedParameter){
    var newValue = passedParameter || 1337
}

Or run functions when conditions are met:

var myBool = true;
myBool && someFunc(); // someFunc will only be evaluated if `myBool` is truthy

More info on truthy / falsy

Post a Comment for "Logical And (&&) And Or (||) Operators"