Why is this function returning "undefined"? [duplicate]

Active3 hr before
Viewed126 times

6 Answers


I also alert the function in order to see what it returns:,Anybody know what is wrong, ie why the code returns undefined in the cases where I test the "response" variable?, Stack Overflow Public questions & answers , GitLab launches Collective on Stack Overflow

This is an asynchronous call:

$.post("username_check.php", {

I'm trying to return a value from one function to another, get_Positions () has to get the lat and lng from parseJson (date), it's just returning undefined.  I want to get these two values from within get_Positions (). , The parseJson function returns nothing. The return [lat,lng]; you added is returning the value for the $.each callback and not for the parseJson function.

The correct one is:

function parseJson(data) {
   var result = [];

   $.each($.parseJSON(data), function(item, value) {
      $.each(value, function(i, k) {
         var location = function() {
            lat = k.lat;
            lng = k.lng;
            return [lat, lng];
         loc = location();
         lat = loc[0];
         lng = loc[1];

         console.log([lat, lng]);

         result = [lat, lng];

   return result;
load more v

The Contents are licensed under creative commons.,Otherwise result.type must be normal. Return undefined.,This is defined in the [[Call]] internal method specification (relevant points in bold):,This has the desired effect. However, it also returns "undefined" as an unexpected bonus.

So I've hijacked the console function:

var log = Function.prototype.bind.call(console.log, console);
console.log = function(a) {
   log.call(console, a);
   submitmsg("Log", a);

So should the correct code be the following?

var log = Function.prototype.bind.call(console.log, console);
console.log = function(a) {
   return log.call(console, a);
   submitmsg("Log", a)
load more v

I do get the results, if it's true - it generates new number, if false - should return. I tried to alert the num in the else part, and it did Alert the number, but on return - undefined. Why is it happening and how to fix it?,Also, as pointed out in the comments, it would actually be far simpler to have the server give you a random number.,For a solution using deferred objects, try this:,And here's the function itself:

I have a form on my page. When the user hits the Send button, in the background it also generates a unique_number by calling for a function that generates this number and also checks in the DB that this number doesn't exist; if it does - generates it again, if doesn't - returns this number. However, for some reason when I'm trying to print out this number to the page, or alert it - I'm getting undefined, though the function returns the number. Here's the call for the function:

var unique_num = create_unique_number();
rest of this

And here's the function itself:

function create_unique_number() {
   var num = Math.floor(Math.random() * (999999 - 100000 + 1)) + 100000;

   $.getJSON("inc/API.php", {
         command: "is_unique_number_exist",
         unique_num: num
      function(result) {
         if (result == true) {
         } else {
            return num;
load more v

And just to be sure everything looks copacetic, let’s print out the value of our new whoAmI variable:,Objects referenced from anywhere in the current call stack (that is, all local variables and parameters in the functions currently being invoked, and all the variables in the closure scope),A traditional, old-browser-compliant solution is to simply save your reference to this in a variable that can then be inherited by the closure; e.g.:,The better choice would be to pass in a function as the initial argument; e.g.:

Consider this example code snippet:

Game.prototype.restart = function() {
   this.timer = setTimeout(function() {
      this.clearBoard(); // what is "this"?
   }, 0);
load more v

Flowing off the end of a value-returning function (except main) without a return statement is undefined behavior. ,without encountering a return statement, return; is executed. ,If control reaches the end of the main function, return 0; is executed. ,If the return type of a function is specified as a placeholder type, it will be deduced from the return value.

#include <iostream>
#include <string>
#include <utility>
void fa(int i)
    if (i == 2)
    std::cout << i << '\n';
} // implied return;
int fb(int i)
    if (i > 4)
        return 4;
    std::cout << i << '\n';
    return 2;
std::pair<std::string, int> fc(const char* p, int x)
    return {p, x};
void fd()
    return fa(10); // fa(10) is a void expression
int main()
    fa(2); // returns, does nothing when i==2
    fa(1); // prints its argument, then returns
    int i = fb(5); // returns 4
    i = fb(i); // prints its argument, returns 2
    std::cout << i << '\n'
              << fc("Hello", 7).second << '\n';
load more v

Other "returning-undefined" queries related to "Why is this function returning "undefined"? [duplicate]"