I am currently learning node.js and already meet several times the same problem that seems very simple but I can't still understand how to solve it.
Code:
var SRC_PORT = 6025;
var dgram = require('dgram');
var clientUDP = dgram.createSocket("udp4");
var test
clientUDP.bind(SRC_PORT, function () {
multicastNew()
});
function multicastNew() {
var test = 777
console.log(test);
}
Problem
Cant use variable test content outside the function multicastNew()
In the function multicastNew()
I have a variable var test
. In that function multicastNew()
I gave to test = 777
. When I want to console.log(test)
in the same function multicastNew()
everything works,it outputs 777
. The problem is that when I want to console.log(test)
outside function multicastNew()
it outputs undefined
.
Can you please explain me how to solve this issue and why it is. Thank you!