Thursday, October 13, 2011

Lazy Evaluation


var boolV = true || false || true || false || true;
 console.log(boolV); //output is true

Question is did all the operations (logical) got executed ?
Ans is NO.

If several logical operations one after the other, but the result becomes clear at some point before the end of entire operation, then the remaining operations are not performed.

Lets consider the above statement.

var boolV = true || false || true || false || true;

It is before the first local operation it is clear that boolV is  suppose to have value true irrespective of any logical or operation. hence the other logical operations are not executed. in the above statement.

To verify this, we will the following code.

var a =10;
console.log(a); //output 10
console.log(!!++a); //output true
console.log(a);        //output 11
var boolV = true || false || true || false || true || !!++a;
console.log(boolV);  //output true
console.log(a);         //output 11 (which imples the !!++a didn't got executed)
boolV =  !!++a || true || false || true || false || true ;
console.log(boolV); //output true
console.log(a);        //output 12 (which imples the !!++a  got executed)


Ain't that interesting !!!

Wednesday, October 12, 2011

Starting reading Object Oriented Javascript by Stoyan Stefanov again

Starting reading Object Oriented Javascript by Stoyan Stefanov again.

I used to read this book managing between my office work and completing house hold choir.
Interestingly this time, I am being asked to complete this book by tomorrow (thats a big task) and that too as an work which I'm liking it.

This book contains conceptual treasures of javascript and everytime I read, I am starting to like it more.

Small interesting codes for all.

var a=10;
var b =20;
var c = "20";

var d,e,f,g;

d= a+b;
e= a+c;
f= b+a;
g= c+a;

console.log(d);
console.log(e);
console.log(f);
console.log(g);

Can you guess the output???
d>>> 30
e>>>  1020
f>>> 30
g>>> 2010

the value of d and f remains same whereas e and f are different. since e and f becomes string variable  from the operation  e= a+c; and g= c+a;

<< number >> + << string >> will result in string concatenation
and result type will also be << string >>.


There was an error in this gadget