Tutorial by Examples

var x = true, y = false; AND This operator will return true if both of the expressions evaluate to true. This boolean operator will employ short-circuiting and will not evaluate y if x evaluates to false. x && y; This will return false, because y is false. OR This operator wil...
Operands of the abstract equality operator are compared after being converted to a common type. How this conversion happens is based on the specification of the operator: Specification for the == operator: 7.2.13 Abstract Equality Comparison The comparison x == y, where x and y are values, prod...
When both operands are numeric, they are compared normally: 1 < 2 // true 2 <= 2 // true 3 >= 5 // false true < false // false (implicitly converted to numbers, 1 > 0) When both operands are strings, they are compared lexicographically (according to alphabeti...
Operator != is the inverse of the == operator. 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 ret...
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 |...
The differences between null and undefined null and undefined share abstract equality == but not strict equality ===, null == undefined // true null === undefined // false They represent slightly different things: undefined represents the absence of a value, such as before an identifier/...
NaN ("Not a Number") is a special value defined by the IEEE Standard for Floating-Point Arithmetic, which is used when a non-numeric value is provided but a number is expected (1 * "two"), or when a calculation doesn't have a valid number result (Math.sqrt(-1)). Any equality or ...
The and-operator (&&) and the or-operator (||) employ short-circuiting to prevent unnecessary work if the outcome of the operation does not change with the extra work. In x && y, y will not be evaluated if x evaluates to false, because the whole expression is guaranteed to be false....
The Problem The abstract equality and inequality operators (== and !=) convert their operands if the operand types do not match. This type coercion is a common source of confusion about the results of these operators, in particular, these operators aren't always transitive as one would expect. &...
/* ToNumber(ToPrimitive([])) == ToNumber(false) */ [] == false; // true When [].toString() is executed it calls [].join() if it exists, or Object.prototype.toString() otherwise. This comparison is returning true because [].join() returns '' which, coerced into 0, is equal to false ToNumber. Bew...
JavaScript has four different equality comparison operations. SameValue It returns true if both operands belong to the same Type and are the same value. Note: the value of an object is a reference. You can use this comparison algorithm via Object.is (ECMAScript 6). Examples: Object.is(1, 1); ...
You can group multiple boolean logic statements within parenthesis in order to create a more complex logic evaluation, especially useful in if statements. if ((age >= 18 && height >= 5.11) || (status === 'royalty' && hasInvitation)) { console.log('You can enter our club'); ...
Beware that numbers can accidentally be converted to strings or NaN (Not a Number). JavaScript is loosely typed. A variable can contain different data types, and a variable can change its data type: var x = "Hello"; // typeof x is a string x = 5; // changes typeof x to...
OperatorComparisonExample==Equali == 0===Equal Value and Typei === "5"!=Not Equali != 5!==Not Equal Value or Typei !== 5>Greater thani > 5<Less thani < 5>=Greater than or equali >= 5<=Less than or equali <= 5
A bit field is a variable that holds various boolean states as individual bits. A bit on would represent true, and off would be false. In the past bit fields were routinely used as they saved memory and reduced processing load. Though the need to use bit field is no longer so important they do offer...

Page 1 of 1