Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6794253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:11:58+00:00 2026-05-26T18:11:58+00:00

I am currently linking a javascript file to an html page, and upon using

  • 0

I am currently linking a javascript file to an html page, and upon using a function in that javascript file the return value essentially gets erased and shows up as undefined, even though in the function itself the value is defined (that was probably very confusing, i’ll just show the code and it should make sense):

functions.js

function addActivity(contactNameSelected, username) {   
var returnArray = [];
//post to .php
        if(data.added)
        {
            var newEvent = [];
            newEvent['id'] = data.id;
            newEvent['date'] = formattedDate;
            returnArray.push(true);
            returnArray.push(newEvent);
            return returnArray; //when i debug, this has a value and is a valid array at this point
        }
        else
        {
            returnArray.push(false);
            returnArray.push(data.message); //when i debug, this has a value and is a valid array at this point
            return returnArray;
       }
    }

home.html

var response = [];
response = addActivity(contactNameSelected, username); //although valid above, undefined here
if(response[0]) //error b/c response is undefined
{
    //do stuff if successful 
}
else{
    //do other stuff if unsuccessful
}

If i just return a string it works fine, but for some reason if i attempt to return an array it is simply undefined. Why is this?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:11:59+00:00Added an answer on May 26, 2026 at 6:11 pm

    I’m guessing that the omitted ‘//post to .php‘ looks something like

    $.post('...php', { ... }, function(data) {
       if (data.added) ...
    

    The AJAX response is handled by a callback function, which executes asynchronously. In other words, returnArray is populated well after addActivity has returned.

    The return returnArray; statements are useless because you are returning a value from the callback, not from addActivity. The callback is invoked not by your code, but by XHR (in a different execution context) and its return value is discarded.

    To properly pass your data back in asynchronous style, we need to tweak your code.

    function addActivity(contactNameSelected, username, callback) {                           
        $.post('...', { ... }, function(data) {
            var returnArray=[];
            if(data.added)                        
            {                        
                ...
            }                        
            else                        
            {                        
                ...                      
            }
            callback(returnArray);
        });
    }
    

     

    addActivity(contactNameSelected, username, function(response) {
        if(response[0])
        {                                
            ...
        }                                
        else
        {                                
            ...
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently, I am developing a product that does fairly intensive calculations using MS SQL
I'm going to get a book that uses Xerces C++. I'm currently using VS
Currently, I don't really have a good method of debugging JavaScript in Internet Explorer and
I have a couple of sites that are currently under development and I've put
I'm working at a development shop that is using outdated bug tracking software. They're
I've got a problem with linking unobtrusive js file with view. I've got view
I am currently building an app and getting error in linking, here is a
So, I have page where a PHP script echoes out all of my HTML
While developing a website, firebug threw me a javascript error linking to the execution
I have a userscript (read: my Javascript on someone else's site) that allows users

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.