Luca Brozzi
Luca Brozzi

Reputation: 335

if variable ain't null print it: null i'm confused

if (authorID) {
    console.log(authorID);
    $.ajax({
    -ajax stuff-
    )}
} else window.location.href="notLoggedIn.html";

ok this is my problem: in console authorID (a String) does get printed and it's printed as null. My question is: why does this happen? Since authorID is null shouldn't be the else block the one executed?

I'm really new to Javascript/jQuery so I think i may just have missed some obvious thing, but wasn't able to figure it myself.

edit: here's how authorID is created:

if (session.getAttribute("authorID")!= null) {
    authorID = session.getAttribute("authorID").toString();
} 

since i'm testing without logging in i'm expecting session.getAttribute() to return null

Upvotes: 0

Views: 136

Answers (4)

Derek Henderson
Derek Henderson

Reputation: 9706

Ok, the problem is that a string always evaluates as true. You have said authorID is a string; therefore if (authorID) will always evaluate true.

What you can do is test if your 'authorID' string does not equal 'null':

if (authorID !== 'null')

I imagine you want the page redirection to happen only when 'authorId' equals the string 'null, so your code would look like this:

if (authorID !== 'null') {
    console.log(authorID);
    $.ajax({
    // -ajax stuff-
    )}
} else {
    window.location.href = "notLoggedIn.html";
}

Alternatively, now that you've edited the question to show how 'authorID' is defined:

Change that block to:

if (session.getAttribute("authorID")) {
    authorID = session.getAttribute("authorID").toString();
} else {
    authorID = null;
}

Then you can use your original conditional:

if (authorID) {
    console.log(authorID);
    $.ajax({
    // -ajax stuff-
    )}
} else {
    window.location.href = "notLoggedIn.html";
}

Upvotes: 1

Adrian Salazar
Adrian Salazar

Reputation: 5319

Well, JavaScript is a very flexible language. It is very easy to get confused by "truthy" values.

null and false belong to those truthy common mistakes.

Somewhere before your method execution, a null value was converted into string. Maybe if you get the value from the DOM.

That's why on debug, you see "null" being written to the output. But in reality you have "null" as a string not the raw value of null.

for JavaScript this is evaluates to true:

if("null"){

}

The only way to eliminate this is to make a check of the datatype using typeof and check against the correct value.

if(a !== null && a !== "null"){

}

Upvotes: 2

Tyanna
Tyanna

Reputation: 719

I would suggest having a read of: http://saladwithsteve.com/2008/02/javascript-undefined-vs-null.html

In your case, the end of that article shows the mistake you've made:

if (foo == null) {
       foo = "Joe";
    }

Is equal to:

if (!foo) {
       foo = "Joe";
    }

I did a quick jsfiddle that I hope will spell it out better for you: http://jsfiddle.net/vp9Fa/1/

Upvotes: 0

chamara
chamara

Reputation: 12709

to pass the if condition authorID should be initialized as a boolean variable.

var authorID=true;

if (authorID)
{
  //code if true
}
else
{
 //code if false
}

in your code you can do like below

if (authorID!=null)
{
 //code if not null
}
else
{
 //code if null
}

Upvotes: -1

Related Questions