Q:

For loop does not work appropriate for the function

Here is a for loop contains a function which specify whether the port is open or closed by using checkPort function. 
var IPAdress = '192.168'; //Local area network to scan (this is rough)
var Portadd = 80; 
var Newip;
var i=0;
var j=0;
//scan over a range of IP addresses and execute a function each time the port is shown to be open.
for(i=0; i <= 1; i++){
for(j=0; j <= 3; j++){
Newip = IPAdress+'.'+i+'.'+j;

checkPort(Portadd, Newip, function(error, status, host, port) {
// Status should be 'open' since the HTTP server is listening on that port
if(status == "open"){
        console.log("IP" , Newip, "on port" , Portadd, "is open");
    }
else if(status == "closed"){
        console.log("IP" , Newip, "on port" , Portadd, "is closed");
    }
});

console.log(Newip);
}
}

and this is the result:
192.168.0.0
192.168.0.1
192.168.0.2
192.168.0.3
192.168.1.0
192.168.1.1
192.168.1.2
192.168.1.3
IP 192.168.1.3 on port 80 is closed
IP 192.168.1.3 on port 80 is closed
IP 192.168.1.3 on port 80 is closed
IP 192.168.1.3 on port 80 is closed
IP 192.168.1.3 on port 80 is closed
IP 192.168.1.3 on port 80 is closed
IP 192.168.1.3 on port 80 is closed
IP 192.168.1.3 on port 80 is closed

since the NewIp as it is printed out works properly, I expected the result to be for example something like this: 
IP 192.168.0.0 on port 80 is closed
IP 192.168.0.1 on port 80 is closed
IP 192.168.0.2 on port 80 is closed
IP 192.168.0.3 on port 80 is closed
IP 192.168.1.0 on port 80 is closed
IP 192.168.1.1 on port 80 is closed
IP 192.168.1.2 on port 80 is closed
IP 192.168.1.3 on port 80 is closed

Does anybody have an idea that why it shows the IPs like that in the actual result part? 

A:

checkPort is using a callback function that is executed later, and by the time it is executed all the IP addresses have been changed in the variable NewIP.
So the result is the callback function prints the last value of NewIP, because it's referencing that value.
You have two alternatives:

You can use the parameter host that is passed to the callback function instead.
Define var NewIP inside the inner for loop by changing var Newip = IPAdress+'.'+i+'.'+j;. This will create a variable that is scoped to the callback function.

