Adrian Gherasim
Adrian Gherasim

Reputation: 135

variable may not have been initialized when test

I'm trying to understand why I get "variable may not have been initialized" when I test (variable === "some text") but I dont get when I used (typeof passwordHashOrg !== 'undefined')

The working code:

checkPass('share', function (error, response) {
    "use strict";
    if (error) {
        console.log(error);
    } else {
        console.log(response);
    }
});

function checkPass(username, callback) {
    "use strict";

    // Require
    var fs = require('fs');

    // Static Parameters
    var usernameInput = username;

    // Open shadow file
    fs.readFile('/etc/shadow', function (error, file) {
        if (error) {
            return callback(error); // file does not exit
        }
        // file is a buffer, convert to string and then to array
        var shadowArray = file.toString().split('\n');

        var passwordHashOrg;
        shadowArray.forEach(function (line) {
            var shadowLineArray = line.split(":");
            var usernameOrg = shadowLineArray[0];
            if (usernameOrg === usernameInput) {
                passwordHashOrg = shadowLineArray[1];
            }
        });
        if (typeof passwordHashOrg !== 'undefined') {
            callback(null, 'userExist');
        } else {
            callback(null, 'unknownUser');
        }

    });
}

and the code that I get "variable may not have been initialized" :

checkPass('share', function (error, response) {
    "use strict";
    if (error) {
        console.log(error);
    } else {
        console.log(response);
    }
});

function checkPass(username, callback) {
    "use strict";

    // Require
    var fs = require('fs');

    // Static Parameters
    var usernameInput = username;

    // Open shadow file
    fs.readFile('/etc/shadow', function (error, file) {
        if (error) {
            return callback(error); // file does not exit
        }
        // file is a buffer, convert to string and then to array
        var shadowArray = file.toString().split('\n');

        var passwordHashOrg;
        shadowArray.forEach(function (line) {
            var shadowLineArray = line.split(":");
            var usernameOrg = shadowLineArray[0];
            if (usernameOrg === usernameInput) {
                passwordHashOrg = shadowLineArray[1];
            }
        });
        if (passwordHashOrg === 'some text') {
            callback(null, 'userExist');
        } else {
            callback(null, 'unknownUser');
        }

    });
}

The only diferent from the two code is:

if (typeof passwordHashOrg !== 'undefined') {
vs
if (passwordHashOrg === "some text") {

Upvotes: 0

Views: 7196

Answers (1)

Bergi
Bergi

Reputation: 664484

The warning means just what it says - the variable might not have been initialised with a value at the time of the comparison. Since that's the expected behaviour, and basically just what you want to test for, the warning can be safely ignored. Alternatively, use

var passwordHashOrg = null; // or
var passwordHashOrg = undefined; // or whatever default value you want

So why didn't you get the warning when using typeof? Because that's not necessarily evaluating the value of the variable, and might be seen by jshlint as justified on non-initialised variables (it even works on undeclared variables). You'd probably get the same warning if you did compare the value to undefined.

Upvotes: 4

Related Questions