Really basic javascript question:
Global variables in javascript and node.js, what am I doing wrong? I've seen other posts where the answer was to return the value at the end of the function. I tried this and I still can't get the value to be altered to "5" and stay that way throughout my socket connection. Any help would be greatly appreciated!
Could it be the fact that the variable is changed in an anonymous function?
var x;
io.sockets.on('connection', function(socket){
x = 0;
console.log("x at first is: " + x);
socket.on('key', function(value){//value = 5
x = value;
console.log("x is now + " + value);
return x;
})
console.log("outside, x is " + x);
socket.on('page', function(){
console.log("x is unfortunately still: " + x);
})
})
and my console log looks like this:
x at first is 0
x is now 5
outside, x is 0
x is unfortunately still: 0