0

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") {

Adrian Gherasim
  • 135
  • 1
  • 4
  • 10
  • 1
    Seems to me that you don't have to initialize a variable in order to compare its type. Perhaps an uninitialized variable's type is already 'undefined'? – mah Jun 30 '15 at 19:34
  • Is this a jslint or jshint warning? Please tag your question appropriately. – Bergi Jun 30 '15 at 19:40
  • The problem is: I'm trying to look for a specific user in a linux shadow file and if that user exist do something. The question is: why do I get 'unknownUser' when I try to test if variable 'passwordHashOrg' have some value but if I test for defined it success. – Adrian Gherasim Jun 30 '15 at 19:47
  • @AdrianGherasim: Maybe because the shadow file is unlikely to contain "`some text`"? – Bergi Jun 30 '15 at 19:53

1 Answers1

4

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375