0

There are all kinds of examples of functions returning "undefined" but I'm having trouble finding a reason why my JS conditional (which I have reduced to a simple IF...ELSE statement) is returning "undefined" in the console.

var blue = "color";
var green = "color";


if (blue = green) {
    console.log("success");
}
else {
    console.log("fail");
}

Can someone please explain what is undefined? Thanks in advance.

1
  • the console.log() return is undefined, not the code itself.
    – dandavis
    May 26, 2015 at 23:30

3 Answers 3

2

Your if contains an assignment, not an equality check.

Replace:

blue = green

With:

blue == green

Or preferably:

blue === green

Since both are expected to be strings, you can (and should) use the strict equality check (===), there.

This "triple equals" check if both type and value are equal, compared to only value for the ==:

alert("1 === '1': " + (1 === '1'));
alert("1 == '1': " + (1 == '1'));


Now, if you run that code in a browser's console, the actual code will not return a value, thus, it will output undefined, aside from the console.log() results:

enter image description here

4
  • Never use double equals: stackoverflow.com/questions/359494/… May 26, 2015 at 22:21
  • 1
    Never say "Never". If you know what types you're comparing, and you're familiar with JS's type casting, a == really isn't that bad.
    – Cerbrus
    May 26, 2015 at 22:23
  • Really? 2 downvotes? What's so wrong about this answer?
    – Cerbrus
    May 26, 2015 at 22:27
  • 1
    You and me both @Cerbrus i.imgur.com/otKjoWr.png . I wish there were more explanations given at times.
    – Travis J
    May 26, 2015 at 22:29
1

The reason that you see undefined in the console is because that is what the executed code finished with. The last statement executed was the if else clause which has no return value, hence undefined.

A second important aspect to note is that you are doing an assignment inside of your if statement, and as a result of that assignment you are always going to end with if( "color" ) which is going to always be true, and as a result the console.log will always occur.

This results in the console looking like

"success"
undefined

Since someone downvoted this and apparently is not familiar with executing console code

enter image description here

0
1

Your current if-statement is conducting an operation of setting blue to equal green. The single equals sign does not actually check for a truthy value.

In order to run a boolean check here, you need to use a triple equals operator.

if (blue === green)

If you run this in the console, here's how it looks:

enter image description here

Since your if statement doesn't actually return anything (instead, it just logs to the console), you will receive undefined as your output.

5
  • You may want to explain why.
    – Cerbrus
    May 26, 2015 at 22:24
  • This still results in undefined.
    – Travis J
    May 26, 2015 at 22:24
  • I just tested this in the console. The if statement, when run with the triple-equals, returns 'success'. Am I missing some vital part of OP's code that wasn't posted? May 26, 2015 at 22:27
  • 1
    The if statement doesn 't return anything. Sure, 'success' will be logged, but nothing's returned.
    – Cerbrus
    May 26, 2015 at 22:29
  • Thanks for the suggestions, seriously appreciate it! May 26, 2015 at 22:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.