# Tutorial by Examples

## Logic Operators with Booleans

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 &amp;&amp; y; This will return false, because y is false. OR This operator wil...

## Abstract Equality (==)

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...

## Relational operators (<, <=, >, >=)

When both operands are numeric, they are compared normally: 1 &lt; 2 // true 2 &lt;= 2 // true 3 &gt;= 5 // false true &lt; false // false (implicitly converted to numbers, 1 &gt; 0) When both operands are strings, they are compared lexicographically (according to alphabeti...

## Inequality

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...

## 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 |...

## Null and Undefined

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 Property of the Global Object

NaN (&quot;Not a Number&quot;) 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 * &quot;two&quot;), or when a calculation doesn't have a valid number result (Math.sqrt(-1)). Any equality or ...

## Short-circuiting in boolean operators

The and-operator (&amp;&amp;) 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 &amp;&amp; y, y will not be evaluated if x evaluates to false, because the whole expression is guaranteed to be false....

## Abstract equality / inequality and type conversion

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. &...

## Empty Array

/* 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...

## Equality comparison operations

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); ...

## Grouping multiple logic statements

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 &gt;= 18 &amp;&amp; height &gt;= 5.11) || (status === 'royalty' &amp;&amp; hasInvitation)) { console.log('You can enter our club'); ...

## Automatic Type Conversions

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 = &quot;Hello&quot;; // typeof x is a string x = 5; // changes typeof x to...

## List of Comparison Operators

OperatorComparisonExample==Equali == 0===Equal Value and Typei === &quot;5&quot;!=Not Equali != 5!==Not Equal Value or Typei !== 5&gt;Greater thani &gt; 5&lt;Less thani &lt; 5&gt;=Greater than or equali &gt;= 5&lt;=Less than or equali &lt;= 5

## Bit fields to optimise comparison of multi state data

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