Page 191 - JavaScript
P. 191
Will return true if the operands aren't equal.
The javascript engine will try and convert both operands to matching types if they aren't of the
same type. Note: if the two operands have different internal references in memory, then false will
be returned.
Sample:
1 != '1' // false
1 != 2 // true
In the sample above, 1 != '1' is false because, a primitive number type is being compared to a
char value. Therefore, the Javascript engine doesn't care about the datatype of the R.H.S value.
Operator: !== is the inverse of the === operator. Will return true if the operands are not equal or if
their types do not match.
Example:
1 !== '1' // true
1 !== 2 // true
1 !== 1 // false
Logic Operators with Non-boolean values (boolean coercion)
Logical OR (||), reading left to right, will evaluate to the first truthy value. If no truthy value is
found, the last value is returned.
var a = 'hello' || ''; // a = 'hello'
var b = '' || []; // b = []
var c = '' || undefined; // c = undefined
var d = 1 || 5; // d = 1
var e = 0 || {}; // e = {}
var f = 0 || '' || 5; // f = 5
var g = '' || 'yay' || 'boo'; // g = 'yay'
Logical AND (&&), reading left to right, will evaluate to the first falsy value. If no falsey value is
found, the last value is returned.
var a = 'hello' && ''; // a = ''
var b = '' && []; // b = ''
var c = undefined && 0; // c = undefined
var d = 1 && 5; // d = 5
var e = 0 && {}; // e = 0
var f = 'hi' && [] && 'done'; // f = 'done'
var g = 'bye' && undefined && 'adios'; // g = undefined
This trick can be used, for example, to set a default value to a function argument (prior to ES6).
var foo = function(val) {
// if val evaluates to falsey, 'default' will be returned instead.
return val || 'default';
https://riptutorial.com/ 148

