Contechtions
Contechtions

Reputation: 85

How does the or operator work in this case?

var v; v = (v || 0) + 3;

While learning javascript I ran into this. Does anyone know how it works, what it's called, etc.

Upvotes: 0

Views: 56

Answers (1)

PSL
PSL

Reputation: 123739

If both this statement comes right after the other then it doesn't make much sense.. It just means this

var v; //which is undefined here

if(!v) //which is true in this case..
{
  v=0; //initialize v=0;
}
v +=3; //Now increment v here as if it doesn't know what v was.

and guess what this set of statement will just output 3 always.

Just replace this with var v=3 :)

Upvotes: 4

Related Questions