Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › Issue with Java Script return function
New Posts  All Forums:Forum Nav:

Issue with Java Script return function

post #1 of 7
Thread Starter 
Hi All,

The below function is getting the info I need as I can see it if I console.log(); but how do I then access this data outside of the function?
Code:

function onSuccess(data) {
console.log(data);
        return data
    }



Seems simple but I can not figure it out.
Mid range build
(12 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 3570k P8Z77-V ASUS ROG RX480 Corsair 8GB 2x 2GB + kingston hyperx 16gb 
Hard DriveOptical DriveCoolingOS
Samsung Evo SSD 500gb none corsair H80 windows 7 64 
MonitorPowerCaseMouse
Samsung s22b360 corsair tx 750 m NZXT phantom 410 (GM) logitech G400 
  hide details  
Reply
Mid range build
(12 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 3570k P8Z77-V ASUS ROG RX480 Corsair 8GB 2x 2GB + kingston hyperx 16gb 
Hard DriveOptical DriveCoolingOS
Samsung Evo SSD 500gb none corsair H80 windows 7 64 
MonitorPowerCaseMouse
Samsung s22b360 corsair tx 750 m NZXT phantom 410 (GM) logitech G400 
  hide details  
Reply
post #2 of 7
Not sure what you mean. You are already returning the data, so all you have to do is capture it when you call the function. Then again, you're already passing in data when you call the function, but I'm assuming the real function does something with data.
post #3 of 7
The function you provided just returns the same data it was passed, but like MakubeX said, I'll assume the actual function does something with the data and returns the results. You just need to set a variable = to the return value of a call to the function, ie:
Code:
var x = onSuccess(data) 

function onSuccess(data) {
     console.log(data);
     return data
}

Then x = the returned value/data from the function.
Edited by agawthrop - 9/28/15 at 9:50am
First Build
(12 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 4690K Asus Z97-A Gigabyte GeForce GTX 970 mini Crucial 8GB 
Hard DriveHard DriveOptical DriveOS
Samsung SSD 840 EVO 250GB Western Digital Blue 1TB LG DVD Burner Windows 8.1 Pro 
KeyboardPowerCaseMouse
Logitech K800 Seasonic 550W Corsair Carbide SPEC-02 Logitech G700s 
  hide details  
Reply
First Build
(12 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 4690K Asus Z97-A Gigabyte GeForce GTX 970 mini Crucial 8GB 
Hard DriveHard DriveOptical DriveOS
Samsung SSD 840 EVO 250GB Western Digital Blue 1TB LG DVD Burner Windows 8.1 Pro 
KeyboardPowerCaseMouse
Logitech K800 Seasonic 550W Corsair Carbide SPEC-02 Logitech G700s 
  hide details  
Reply
post #4 of 7
Quote:
Originally Posted by agawthrop View Post

You just need to set a variable = to the function...

You mean set a variable equal to the return value of a call to the function (as in your snippet). That's very different smile.gif
    
CPUMotherboardGraphicsRAM
i7 920 D0 MSI X58 Pro-E Gigabyte GTX 970 (GV-N970IX-4GD) 3x2GB G.Skill DDR3-1333 9-9-9-24 
Hard DriveHard DriveOptical DriveOS
840 Pro Caviar Black LG BD-ROM Windows 8.1 Pro x64 
MonitorMonitorKeyboardPower
Dell U2713HM Dell U2311H Turbo-Trak (Google it :D) Corsair HX-520 
CaseMouseMouse PadAudio
CM690 Mionix Avior 7000 Everglide Titan AKG K 242 HD 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
i7 920 D0 MSI X58 Pro-E Gigabyte GTX 970 (GV-N970IX-4GD) 3x2GB G.Skill DDR3-1333 9-9-9-24 
Hard DriveHard DriveOptical DriveOS
840 Pro Caviar Black LG BD-ROM Windows 8.1 Pro x64 
MonitorMonitorKeyboardPower
Dell U2713HM Dell U2311H Turbo-Trak (Google it :D) Corsair HX-520 
CaseMouseMouse PadAudio
CM690 Mionix Avior 7000 Everglide Titan AKG K 242 HD 
  hide details  
Reply
post #5 of 7
Quote:
Originally Posted by randomizer View Post

You mean set a variable equal to the return value of a call to the function (as in your snippet). That's very different smile.gif

That would be correct! My bad! Fixed the post.
First Build
(12 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 4690K Asus Z97-A Gigabyte GeForce GTX 970 mini Crucial 8GB 
Hard DriveHard DriveOptical DriveOS
Samsung SSD 840 EVO 250GB Western Digital Blue 1TB LG DVD Burner Windows 8.1 Pro 
KeyboardPowerCaseMouse
Logitech K800 Seasonic 550W Corsair Carbide SPEC-02 Logitech G700s 
  hide details  
Reply
First Build
(12 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 4690K Asus Z97-A Gigabyte GeForce GTX 970 mini Crucial 8GB 
Hard DriveHard DriveOptical DriveOS
Samsung SSD 840 EVO 250GB Western Digital Blue 1TB LG DVD Burner Windows 8.1 Pro 
KeyboardPowerCaseMouse
Logitech K800 Seasonic 550W Corsair Carbide SPEC-02 Logitech G700s 
  hide details  
Reply
post #6 of 7
There are many ways to get a variable out of a function.

Return the variable to the caller, and then have the caller assign it to a variable within the callers scope.

Declare the variable in a scope higher than the function called.

Pass the variable as an argument to another function.

Based on your example it isn't quite clear what you are looking for, but it seems you are confused about scope. Perhaps some examples might help.


Declare variable in a global scope:
Code:
var a = "I'm in a higher level scope than the function, because I am declared outside the function"; //

var b = function(){
console.log(a);
}

b();

Pass variable as an argument
Code:
var c = function(b){
console.log(b);
}

var a = function(){
var b = "I'm a function in a's scope, and I am being passed to c";
c(b);
}
a();

Return a variable from a function
Code:
function foo(){
var a = "I am returned to b";
return a;
}

var b = foo();
console.log(b);
post #7 of 7
Where possible it's a good idea to stick with pure functions (those that always return the same value for a given input) and not ones which depend on or mutate the outer scope, and whose behaviour is therefore less predictable. In fact mutating outer scope is not getting a variable out of a function at all.
    
CPUMotherboardGraphicsRAM
i7 920 D0 MSI X58 Pro-E Gigabyte GTX 970 (GV-N970IX-4GD) 3x2GB G.Skill DDR3-1333 9-9-9-24 
Hard DriveHard DriveOptical DriveOS
840 Pro Caviar Black LG BD-ROM Windows 8.1 Pro x64 
MonitorMonitorKeyboardPower
Dell U2713HM Dell U2311H Turbo-Trak (Google it :D) Corsair HX-520 
CaseMouseMouse PadAudio
CM690 Mionix Avior 7000 Everglide Titan AKG K 242 HD 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
i7 920 D0 MSI X58 Pro-E Gigabyte GTX 970 (GV-N970IX-4GD) 3x2GB G.Skill DDR3-1333 9-9-9-24 
Hard DriveHard DriveOptical DriveOS
840 Pro Caviar Black LG BD-ROM Windows 8.1 Pro x64 
MonitorMonitorKeyboardPower
Dell U2713HM Dell U2311H Turbo-Trak (Google it :D) Corsair HX-520 
CaseMouseMouse PadAudio
CM690 Mionix Avior 7000 Everglide Titan AKG K 242 HD 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Web Coding
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › Issue with Java Script return function