2

I'm sure I'm just overlooking something, but I can't see what it is.

I have a page I'm using to test some new code with using the console. Most of it works.

    if(typeof(thisNode.L1.download) != 'undefined') {
    console.log('a1');
        if (thisNode.L1.download.sku.toString() == lastSku) {
            console.log('a2');
            addSku = thisNode.L1.cd.sku.toString();
        } else { console.log('a3'); }
    } else if(typeof(thisNode.S5.download) != 'undefined') {
        console.log('b1');
        if (thisNode.S5.download.sku.toString() == lastSku) {
            console.log('b2');
            addSku = thisNode.S5.cd.sku.toString();
        } else {
            console.log('b3');
        }
    }
    console.log('foo');

returns

    a1
    a3
    foo
    undefined

given that typeof(thisNode.S5.download) != 'undefined' returns true

and lastSku returns "24536"

and thisNode.S5.download.sku.toString() returns "24536"

This is not expected.

I did some breaking down and it looks like it's the initial if statement that is the problem.

I enter into the console: if (thisNode.L1.download.sku.toString() == lastSku) {} i get "undefined"

So I checked it piece by piece

lastSku returns "24536"

thisNode returns a JSON object. Object {L1: Object, S2: Object, S3: Object, S5: Object}

thisNode.L1 returns Object {box: Object, download: Object, cd: Object}

thisNode.L1.download returns Object {sku: 24354}

thisNode.L1.download.sku returns 24354

thisNode.L1.download.sku.toString() returns "24354"

thisNode.L1.download.sku.toString() == lastSku returns false

    if (thisNode.L1.download.sku.toString() == lastSku) {
        console.log('foo');
    } else {
        console.log('bar');
    }

returns "bar" undefined

    if (thisNode.L1.download.sku.toString() == lastSku) {
       console.log('foo');
    } else {
        console.log('bar');
    }
    console.log('yabba');

returns

    bar
    yabba
    undefined

Note that I can put any JavaScript in the original if statement and i still get undefined, so it's not that there's no code for it to skip.

To recap, the original block doesn't appear to ever get to line 7 but it does look like after running through the first set of if statements it does keep running code after all of them.

The E
  • 697
  • 1
  • 9
  • 23
  • Is that code in a function? If so, it will output 'undefined' if nothing is returned – Cilan Jan 18 '14 at 16:52
  • 1
    The console *always* prints the value of the last statement evaluated. Block statements have no value (they're `undefined`), as do `var` declarations, `function` declarations, and others. – Pointy Jan 18 '14 at 16:54

2 Answers2

4

I enter into the console: if (thisNode.L1.download.sku.toString() == lastSku) {} i get "undefined"

That's entirely expected behaviour.

The undefined you're seeing (and the final undefined you're seeing in your larger code examples) is just Chrome's JS console outputting the value the last statement evaluates to, and in JavaScript, if statements don't evaluate to a value.

Try this on the console:

console.log('foo')

You'll see

foo
undefined

The foo is output by console.log, the undefined is the return value of console.log. If you look to the left of undefined, you'll see a grey <- arrow, indicating this is the return value of the last statement, not output caused by the code.

See below:

enter image description here

This is typical behaviour for REPL environments.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • actually, I do see the undefined. I so focussed on that display that I didn't see the `else if` will never be checked, because, DUH, the first one returned something. I need to restructure this with some `&&` combining the pairs. I'm not entirely sure what I was thinking by not doing it in the first place. – The E Jan 18 '14 at 16:54
  • @ManofSnow Yes he will, unless you're using a browser where `console.log` returns a value or a console that doesn't output return values in REPL mode. – user229044 Jan 18 '14 at 16:56
  • @meagar Okay, but I see your edit replaced **for** with **foo** – Cilan Jan 18 '14 at 16:57
  • @ManofSnow That was corrected 7 minutes ago... Can you explain why you think he "won't see undefined from that"? – user229044 Jan 18 '14 at 16:58
  • @meager I didn't know the return keyword goes in **if** statements without functions – Cilan Jan 18 '14 at 17:00
0

the undefined was a red herring I couldn't stop looking at, which meager helpfully got me to finally see.

The real problem was the external if..else if...else if structure. The reason it never checked thisNode.S5 was because I used

if(typeof(thisNode.L1.download) != 'undefined') {
    if (thisNode.L1.download.sku.toString() == lastSku) {
        //do stuff
    }
else if(typeof(thisNode.S5.download) != 'undefined') {
    if (thisNode.S5.download.sku.toString() == lastSku) {
        //do different stuff
    }
}

putting the second check INSIDE the first if was a dumb movee. I wasn't thinking. I should, instead, have used

if(typeof(thisNode.L1.download) != 'undefined' && thisNode.L1.download.sku.toString() == lastSku) {
    //do stuff
} else if(typeof(thisNode.S3.download) != 'undefined' && thisNode.S5.download.sku.toString() == lastSku) {
    //do different stuff
}
The E
  • 697
  • 1
  • 9
  • 23