jamesson
jamesson

Reputation: 327

jscript if for global variable

Here's my code

var isNew = true

function limb(a,b)
{
    if (isNew=true) 
    {
        post(a,b);
        isNew = false;
        post ("first");
    }
    else
    {
        post(a,b);
        post ("not first"); 
    }
}

The problem I'm having is that else condition is never triggered. I'm assuming value of isNew is never updated, but I have no idea why.

Upvotes: 0

Views: 154

Answers (2)

Jeremy Banks
Jeremy Banks

Reputation: 129756

In some languages = is used both to assign values and to compare them. JavaScript uses different operators for each of these.

x = 10 always means "set x to 10, and give me the value of x".
x == 10 always means "tell me if x is equal to 10".

So your condition could have been if(isNew == true) and it would have worked. You can also just put if(isNew).

Upvotes: 2

Fu Cheng
Fu Cheng

Reputation: 3395

You should use if (isNew===true) to test it.

Upvotes: 4

Related Questions