Like SystemiConnectFri 15 Oct 2021, 04:50
Auto Save MessagesLGforumFri 26 Feb 2021, 13:31
New tutorial questionTheCrowMon 15 Feb 2021, 08:12
Support iOS Emojis (and other platforms)LGforumSun 14 Feb 2021, 01:25
LGforum (2806)
Why did logging a result of a function returns undefined? Vote_lcapWhy did logging a result of a function returns undefined? Voting_barWhy did logging a result of a function returns undefined? Vote_rcap 
Mr.Easybb (1587)
Why did logging a result of a function returns undefined? Vote_lcapWhy did logging a result of a function returns undefined? Voting_barWhy did logging a result of a function returns undefined? Vote_rcap 
Bloodbath (745)
Why did logging a result of a function returns undefined? Vote_lcapWhy did logging a result of a function returns undefined? Voting_barWhy did logging a result of a function returns undefined? Vote_rcap 
Rukiafan (533)
Why did logging a result of a function returns undefined? Vote_lcapWhy did logging a result of a function returns undefined? Voting_barWhy did logging a result of a function returns undefined? Vote_rcap 
Dom (513)
Why did logging a result of a function returns undefined? Vote_lcapWhy did logging a result of a function returns undefined? Voting_barWhy did logging a result of a function returns undefined? Vote_rcap 
puppycheese (446)
Why did logging a result of a function returns undefined? Vote_lcapWhy did logging a result of a function returns undefined? Voting_barWhy did logging a result of a function returns undefined? Vote_rcap 
pedro (330)
Why did logging a result of a function returns undefined? Vote_lcapWhy did logging a result of a function returns undefined? Voting_barWhy did logging a result of a function returns undefined? Vote_rcap 
Neymar (301)
Why did logging a result of a function returns undefined? Vote_lcapWhy did logging a result of a function returns undefined? Voting_barWhy did logging a result of a function returns undefined? Vote_rcap 
Hitsu (281)
Why did logging a result of a function returns undefined? Vote_lcapWhy did logging a result of a function returns undefined? Voting_barWhy did logging a result of a function returns undefined? Vote_rcap 
Flora (275)
Why did logging a result of a function returns undefined? Vote_lcapWhy did logging a result of a function returns undefined? Voting_barWhy did logging a result of a function returns undefined? Vote_rcap 


Why did logging a result of a function returns undefined?

HitsuHitsu
Status : Finished on working fmAPI.

Posts : 281
Join date : 2013-09-09
Age : 25
Location : Indonesia
Sat 25 Jul 2015, 10:24
There's something bugging my mind recently and sorry for the bad title. Assume I create a little snippet.

Code:
[panda=js]function pushArray(arr, str)
 arr.push(str);
}

Let's assume that someone was currently using my snippet. He pushes a certain value into an array and blah.

Code:
[panda=js]var blah = [1];
var push = pushArray(blah, 2);

Now he wants to check the value of the variable [ic]push[/ic]. But sadly it returns [ic]undefined[/ic].

Code:
[panda=js]// pushArray module code here
// his code here
console.log(push); // returns undefined

I think you'd understand what I mean now.
I need an answer; why did it always returns undefined unless there's a [ic]return[/ic] statement behind the code inside the function?
Ange TuteurAnge Tuteur
Status : No status yet...

Posts : 109
Join date : 2014-02-18
Age : 28
Location : North America
Sat 25 Jul 2015, 12:23
What you're attempting would look something like this. ( everything in the same scope )

Code:
[panda=js](function() {

  function pushArray(arr, str) {
    arr.push(str);
    return arr;
  };
 
  var blah = [1],
      push = pushArray(blah, 2);

  console.log(push); // [1, 2]

})();

The reason push is undefined is because you're not assigning a value to it, you're only calling the function. To assign a value with your function you need to return arr, which returns blah + the newly added value.
HitsuHitsu
Status : Finished on working fmAPI.

Posts : 281
Join date : 2013-09-09
Age : 25
Location : Indonesia
Sat 25 Jul 2015, 12:59
Super. Just what I needed. Thank you very much. Very Happy
avatarGuest
Wed 04 May 2016, 20:58
Topic solved and archived.
Sponsored content