Logical operators in Javascript
There are three logical operators in JavaScript: || (OR), && (AND), ! (NOT)
"OR" operator is denoted by two pipes ( || ) in javascript. If any one of the operands are true then the value is true. Take a look at the below results.
alert( true || true ); // true
alert( false || true ); //true
alert( true || false ); // true
alert( false || false ); // false
Like you would have noticed the he result is always true except for the case when both operands are false. Now let's say that operands are not necessarily boolean ,then they are converted into boolean. Any non-empty value is true in JS."AND" is denoted by &&. AND returns true if both operands are truthy and false otherwise. Take a look at below examples.
alert( true && true ); // true
alert( false && true ); // false
alert( true && false ); // false
alert( false && false ); // false
Like you would have noticed the result is only true if both the operands are true, else false."NOT" is denoted by ! (exclamation mark). The operator accepts a single argument and does the following: Converts the operand to boolean type: true/false. Returns the inverse value.
alert( !true ); // false
alert( !0 ); // true