0

I am testing the following JS conditional in my console however it is returning "undefined" when it should be executing a logging to console. Can someone please explain why the following doesn't work? I can individually test jQuery("li.category652 a").text() and it returns "Signage" but I just can't get it to work in an if statement in console.

if (jQuery("li.category652 a").text() === "Signage") {
    console.log("success");
}
else {
    console.log("fail");
}

I can copy and paste that code into the console of any webpage that isn't the one I'm working on locally and you will get "fail" because the DOM element jQuery is looking for doesn't exist. If I do it on the webpage with that DOM element then all I get is "undefined" and no success or fail message.

That specific jQuery reference is unique to a local site I am working on but the above conditional can be treated as if it said this:

if ("Signage" === "Signage") {
    console.log("success");
}
else {
    console.log("fail");
}
sparecycle
  • 2,038
  • 5
  • 31
  • 58
  • 2
    Can you provide a full example that demonstrates the problem? [This](http://jsfiddle.net/wa7ha4fg/) works just fine. – Matt Burland May 27 '15 at 14:33
  • The `undefined` you see is the result of the expression (which won't have a value for a non-variable expression), not a message being logged. Are you sure this is the only thing you're putting in your console? – Blixt May 27 '15 at 14:34
  • Do you mean it is not printing anything to the console? It should print 'undefined' because the if-statement itself does not return anything. But before that, it should print either 'success' or 'fail'. – Brandon Horst May 27 '15 at 14:34
  • 1
    Are you sure your console is not set to filter out `.log` statements? What do you expect it to return, if there's no `return`? – DCoder May 27 '15 at 14:34
  • That's just the thing. I can copy and paste that code into the console of any webpage that isn't the one I'm working on locally and you will get "fail" because the DOM element jQuery is looking for doesn't exist. If I do it on the webpage with that DOM element then all I get is "undefined" and no success or fail message. – sparecycle May 27 '15 at 14:41

1 Answers1

3

however it is returning "undefined"

If you look just above the undefined, you should see success or fail (unless jQuery isn't loaded at all, in which case you'll see an error):

enter image description here

Here's a successful example from this page:

enter image description here

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • @maioman: JavaScript statements, somewhat surprisingly, have values. That's the value of the block statement attached to the `else`. Block statements take the value of the last expression in them. `console.log` returns `undefined`. – T.J. Crowder May 27 '15 at 14:40
  • That's just the thing. I can copy and paste that code into the console of any webpage that isn't the one I'm working on locally and you will get "fail" because the DOM element jQuery is looking for doesn't exist. If I do it on the webpage with that DOM element then all I get is "undefined" and no success or fail message. – sparecycle May 27 '15 at 14:40
  • @deeperDATA: I can't come up with any scenario where you wouldn't see success, fail, or an error -- unless console.log is compromised, which seems *really* unlikely. – T.J. Crowder May 27 '15 at 14:46
  • @T.J.Crowder Thank you, I will try again. – sparecycle May 27 '15 at 14:46
  • @deeperDATA: If you figure it out, and it *isn't* the above, please be sure to post an answer saying what it was (it's ***totally fine*** to answer your own question; SO will make you wait 48 hours to accept it, though). (Obviously, if it' *is* the above and it was just a simple testing mistake, then this would be the answer.) – T.J. Crowder May 27 '15 at 14:50
  • Ok, here is the craziness. I swap console.log out with alert() and it works perfectly. For whatever reason it refuses to run console.log but will do any other function I put inside the conditional. – sparecycle May 27 '15 at 14:57