0

I'm currently learning JavaScript. It's a really simple function and I don't get why the console always send return undefined, even if my test Array is defined! Making me crazy! I'm sure it's easy but I can't get over it!


var test = [150];
function testFuntion() {console.log(test[0]); }

PC MANGIN
  • 21
  • 1
  • 3
    Can you add a complete code, like where have you defined test array and where do you call test function? – Nemanja Todorovic Feb 05 '20 at 09:49
  • 3
    console.log() doesn't return anything, it just logs to console - `return test[0]` and see if that does what you expect – Shiny Feb 05 '20 at 09:49
  • I edited it. It's really those 2 lines. I just use the console and try to get my value. Am I doing something wrong for not calling my function back ? – PC MANGIN Feb 05 '20 at 09:51
  • Are you running this in a browser's console? – adiga Feb 05 '20 at 09:51
  • 1
    What do you mean by "the console always send return undefined"? Do you type in the console `testFuntion()`, and it prints two lines: `150` and `undefined`, perchance? – mbojko Feb 05 '20 at 09:51
  • @mbojko that what I was writing as an answer, but question got closed :( – Pac0 Feb 05 '20 at 09:52
  • Chrome console yes. It send only undefined – PC MANGIN Feb 05 '20 at 09:53
  • But you got a link directing you to the answer. :) – mbojko Feb 05 '20 at 09:53
  • @PCMANGIN all the variable and function declarations are shown as undeinfed in the console because they don't evaluate to anything. Run `testFuntion()` to actually call the function – adiga Feb 05 '20 at 09:53
  • @PC Magnin, you are _defining_ a function, not _calling_ it . Execute it, now, as mbojko suggested. Tjhe `undefined` is the return value of your whole code in the console. It also is the return value of the function, but it should at least print something in the console before returning undefined. Note that you will have to define it again (the code you posted) if you change or reload page in the browser. – Pac0 Feb 05 '20 at 09:53
  • you are forgotting to call testFunction() – gaurav bankoti Feb 05 '20 at 09:57
  • @Pac0 Thanks! Finally go my [150] showing, with undefined too ! I still don't get why I have the "undefined" but I'm sure I'll understand it in the futur! Thanks again! – PC MANGIN Feb 05 '20 at 10:03
  • Thanks for everyone of you guys! Love you all <3 – PC MANGIN Feb 05 '20 at 10:04
  • The answer on why you still have "undefined" is the other question linked and used as a duplicate-close reason on your question ;) . (not exactly, but near. Here is the proper explanation : it's the return value of the function, it has NO return value, try to add `return 42` inside the function, you'll see the 42 after the display of console.log) You're welcome. – Pac0 Feb 05 '20 at 10:05
  • Indeed now I finally calm down and I get what they said on the linked question! 42 is now showing! It's beautiful ! THANK YOU VERY much ! :) – PC MANGIN Feb 05 '20 at 10:07

0 Answers0