1

Given the following code, which works, and prints out all colors that are red, why does it also print out an undefined value at the end of the interations?

var cars is just a large array of car-objects with various properties.

function queryCars(){

  // Your code here
   for (var i = 0; i < cars.length; i++ ){
      if(cars[i].color == "Red") {
        console.log(cars[i].make + " " + cars[i].model)
      }
   }

}

Result:

queryCars();
"Audi A5"
"Audi TT"
"BMW 3"
"BMW X5"
"Hyundai Elantra"
"Acura TLX"
undefined
acd37
  • 532
  • 1
  • 7
  • 14
  • are you calling `console.log` somewhere else , ie directly after `queryCars()` ? – Dacre Denny Nov 13 '18 at 19:19
  • 2
    because either of cars[i].make Or cars[i].model is undefined – Piyush Khanna Nov 13 '18 at 19:20
  • @DacreDenny nope... that is the only time it is called. – acd37 Nov 13 '18 at 19:22
  • You can just do console.log(cars[i]) to debug this yourself. Without looking at your data it's hard to know, but I think @PiyushKhanna has the answer. – Blunt Jackson Nov 13 '18 at 19:22
  • 1
    building on what @DacreDenny said, this undefined doesn't appear to be coming from the console.log in the for loop, because you'd have more than just 1 undefined if it were. – Kon Nov 13 '18 at 19:22
  • @PiyushKhanna all cars in the array have both a valid make and model. – acd37 Nov 13 '18 at 19:22
  • yes @ODYN-Kon that's what I'm seeing also - @acd37 in dev tools, click on the line number next to `undefined` - it should take you to where `undefined` is logging - is it in your loop ? – Dacre Denny Nov 13 '18 at 19:23
  • here is the codepen: https://codepen.io/DocZer0/pen/KrNyVy? – acd37 Nov 13 '18 at 19:24
  • Try entering `queryCars() || 1` in the console, and think about that for a moment. – trincot Nov 13 '18 at 19:28
  • @DacreDenny there is no line number listed next to the undefined – acd37 Nov 13 '18 at 19:28
  • @trincot not super helpful... I've been thinking about this for a while and the reason I came to Stack OverFlow is because I wanted/needed others' input on this – acd37 Nov 13 '18 at 19:29
  • I am asking you to think about a variation. Did you notice it? Share what you think of it? Do you know what `|| 1` does? – trincot Nov 13 '18 at 19:30
  • Not seeing a undefined in the codepen – Piyush Khanna Nov 13 '18 at 19:32
  • @trincot yes I am aware... I'm also aware I can return any other integer or string value here, but that's not the goal, the goal is to console log the list and exit. I've been developing for some time, but not managed to wrap my head around what seems to be a simple issue – acd37 Nov 13 '18 at 19:34
  • I hoped you would see that the last output you see is the *evaluation of the expression* you type in the console. Your question was "Why". The answer has been given. – trincot Nov 13 '18 at 19:36
  • Looks like @Bergi is correct... it's just an intricacy of the Chrome/Firefox browser. – acd37 Nov 13 '18 at 19:36

1 Answers1

1

This undefined is not coming from the console.log. It's the return value of the function, since function is not returning anything.

For example, in repl.it:

let a
let b
console.log(a + " " + b)

prints:

undefined undefined
=> undefined

Not that the console.log would have printed 2 undefines if it were part of that loop.

Kon
  • 4,023
  • 4
  • 24
  • 38
  • Right.. so I understand that it is the return value of the function... nothing returned, thus undefined. But is there a way to fix it? I can't return inside the loop because then it stops the loop. I can return outside of the loop, and return something like "Done.", but I just want it to console.log the list and exit... – acd37 Nov 13 '18 at 19:32
  • 1
    You don't need to fix it. It's fine as is. It's just the behavior of the script executor. If you insist on not having that last define, you can return "". – Kon Nov 13 '18 at 19:38