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!
var response = [];is redundant since the function in the line after should return a new array.