Why does my forloop + if/else console log an undefined value [duplicate]










1
















This question already has an answer here:



  • Chrome/Firefox console.log always appends a line saying undefined

    6 answers



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









share|improve this question













marked as duplicate by Bergi javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 19:32


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • 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.

    – bluejack
    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.

    – ODYN-Kon
    Nov 13 '18 at 19:22















1
















This question already has an answer here:



  • Chrome/Firefox console.log always appends a line saying undefined

    6 answers



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









share|improve this question













marked as duplicate by Bergi javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 19:32


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • 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.

    – bluejack
    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.

    – ODYN-Kon
    Nov 13 '18 at 19:22













1












1








1









This question already has an answer here:



  • Chrome/Firefox console.log always appends a line saying undefined

    6 answers



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









share|improve this question















This question already has an answer here:



  • Chrome/Firefox console.log always appends a line saying undefined

    6 answers



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




This question already has an answer here:



  • Chrome/Firefox console.log always appends a line saying undefined

    6 answers







javascript for-loop undefined






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 19:18









acd37acd37

14029




14029




marked as duplicate by Bergi javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 19:32


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Bergi javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 13 '18 at 19:32


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 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.

    – bluejack
    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.

    – ODYN-Kon
    Nov 13 '18 at 19:22

















  • 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.

    – bluejack
    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.

    – ODYN-Kon
    Nov 13 '18 at 19:22
















are you calling console.log somewhere else , ie directly after queryCars() ?

– Dacre Denny
Nov 13 '18 at 19:19





are you calling console.log somewhere else , ie directly after queryCars() ?

– Dacre Denny
Nov 13 '18 at 19:19




2




2





because either of cars[i].make Or cars[i].model is undefined

– Piyush Khanna
Nov 13 '18 at 19:20





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





@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.

– bluejack
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.

– bluejack
Nov 13 '18 at 19:22




1




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.

– ODYN-Kon
Nov 13 '18 at 19:22





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.

– ODYN-Kon
Nov 13 '18 at 19:22












1 Answer
1






active

oldest

votes


















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.






share|improve this answer























  • 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 "".

    – ODYN-Kon
    Nov 13 '18 at 19:38


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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.






share|improve this answer























  • 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 "".

    – ODYN-Kon
    Nov 13 '18 at 19:38
















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.






share|improve this answer























  • 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 "".

    – ODYN-Kon
    Nov 13 '18 at 19:38














1












1








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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 19:25









ODYN-KonODYN-Kon

2,1671825




2,1671825












  • 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 "".

    – ODYN-Kon
    Nov 13 '18 at 19: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 "".

    – ODYN-Kon
    Nov 13 '18 at 19: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





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




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 "".

– ODYN-Kon
Nov 13 '18 at 19:38






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 "".

– ODYN-Kon
Nov 13 '18 at 19:38




這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3