Wednesday, September 28, 2016

Javascript: Operators

Logical

Convert any variable to a boolean

var x = "abc";
var aBool = !!x;

Arithematic

+-*/%

In numeric context, null resolves to zero.

9/0 equals infinity (no exception is thrown)

undefined in numeric context leads to NaN.

Unary

+a
-a
!a

Boolean Operators

Numeric value also evaluates to true unless Numeric value is zero

String with anything in it (even a space) will resolve to true.

undefined -> false
null -> false
NaN -> false
A newed object -> true

!!x -> converts x to boolean

var obj = {};
// when first operand is object, 2nd operand will always be returned
var ans= obj && 100;  // 2nd is numeric
// ans will be 100

var obj1 = {};
var obj2 = {};
var ans = obj1 && obj2;
// ans will obj2

var obj = {};
var ans  = true && obj;
// ans will be obj

var ans = null && 99;
// ans will be null; if either operand is null, null is returned

var ans = "Z" && undefined;
// ans will be undefined; if either operand is undefined, undefined is returned

var ans = "Z" && NaN;
// ans will be NaN; if either operand is undefined, NaN is returned

var ans = false && someUndefinedVar;
// ans will be false; the compiler never gets to someUndefinedVar

var obj = {};
var ans = obj || 1;
// ans is obj (first truthy value is returned)

var ans = false || 1;
// ans is 1

var obj1 = {};
var obj2 = {};
var ans = obj1 | obj2;
// ans == obj1

var obj1 = null;
var obj2 = {}'
var ans = obj1 | obj2;
// ans == obj2

Equality Operators

== compares values
true == 1 evaluates to true
true == 2 evaluates to false (because true converts to number 1)
false == 0 evaluates to true
42 == '42' evaluates to true ('42' is converted to number)

var obj = {
  valueOf: function () { return 1;}
}
1 == obj evaluates to true

var obj = {
  toString: function () { return "1";}
}
1 == obj evaluates to true


var obj1 = {};
var obj2 = {};
obj1 == obj2 evaluates to false
obj1 == obj1 evaluates to false

null == undefined evaluates to true

undefined == 0 evaluates to false

null == 0 evaluates to false

NaN == NaN evaluates to false

Triple-equals sign: type and value must be identical
55 === '55' is false
false === 0 is false
null === undefined is false

55!==55 is false (because they are identically equal).

Relational Operators

"abc" < "ab" is false
"B" < "b" is true

"B".toLowerCase() < "b".toLowerCase() is false

var v = NaN;
Both v >1 is false and v<1 is false

Bitwise Operators

// js converts a number into 32-bit integer before
//   performing bitwise operations
var x = parseInt('1010', 2);  // 2 indicates it is binary number
console.log(x.toString(2));  // print as a binary number

& bitwise AND
| bitwise OR
^  bitwise XOR, 1 XOR 1 == 0, 1 XOR 0 == 1

<< -left shift
>> right-shift

For signed numbers:
>>> right-shift with sign-bit carry
<<<

Compound Assignment

var v = 1;
v +=2;
v -=2;
v /=2;
v %=2;
v <<= 2;  // multiple by 2 * 2
v >>= 1;  // divide by 2

Comma Operator

var a = 1, b = 2;  // variable declaration

var x = (1, 2, 3);
x equals 3 (last value is taken).