0

I have a function and created a global variable.

The alert inside the function is alerting the result as expected but the variable is showing nothing.

How can I fix this?

Here's the code:

var connectionResult = '';

function checkConnection() {
    var networkState = navigator.connection.type;

    var states = {};
    states[Connection.UNKNOWN]  = 'Unknown connection';
    states[Connection.ETHERNET] = 'Ethernet connection';
    states[Connection.WIFI]     = 'WiFi connection';
    states[Connection.CELL_2G]  = 'Cell 2G connection';
    states[Connection.CELL_3G]  = 'Cell 3G connection';
    states[Connection.CELL_4G]  = 'Cell 4G connection';
    states[Connection.CELL]     = 'Cell generic connection';
    states[Connection.NONE]     = 'No network connection';

    alert('Connection type: ' + states[networkState]);

    var connectionResult = states[networkState];
};

checkConnection();

alert(connectionResult); // Returns Nothing
0

3 Answers 3

2

The problem is that you are creating a local variable named connectionResult in the checkConnection rather than assigning to the global connectionResult.

Replace

var connectionResult = states[networkState];

with

connectionResult = states[networkState];

And it will work.

To expand on T.J. Crowder's comment below, you can make this function a little more efficient, since you are declaring what is essentially a constant over and over again. You can change the code as follows:

var NetworkStates = {}; // this never changed in the old function, so refactored it out as a "constant"
NetworkStates[Connection.UNKNOWN]  = 'Unknown connection';
NetworkStates[Connection.ETHERNET] = 'Ethernet connection';
NetworkStates[Connection.WIFI]     = 'WiFi connection';
NetworkStates[Connection.CELL_2G]  = 'Cell 2G connection';
NetworkStates[Connection.CELL_3G]  = 'Cell 3G connection';
NetworkStates[Connection.CELL_4G]  = 'Cell 4G connection';
NetworkStates[Connection.CELL]     = 'Cell generic connection';
NetworkStates[Connection.NONE]     = 'No network connection';

function getConnectionState() {
    return NetworkStates[navigator.connection.type];
}

Now wherever you need the connection state you can just call getConnectionState rather than having a global variable floating around.

Sign up to request clarification or add additional context in comments.

2 Comments

Indeed. Or better yet, have checkConnection return it, and assign the result to the global (ideally making it not a global anymore).
Good call @T.J.Crowder, I updated my answer with your suggestion.
1
var connectionResult = states[networkState];

creates a new variable connectionResult within the scope of the function which is completly unrelated to the global variable connectionResult

Just use

connectionResult = states[networkState];

in order to assign the network state to the global variable

Comments

1

var connectionResult inside checkConnection creates a new variable called connectionResult.

This "inner" variable is only in scope inside checkConnection. It hides, or "shadows" the one that you intend to use: any reference to connectionResult inside checkConnection uses it instead of the "outer" variable that you expect.

Just remove var and you'll use the existing connectResult:

connectionResult = states[networkState];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.