admin管理员组

文章数量:1416082

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

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

Share Improve this question edited Jun 30, 2015 at 19:48 Adrian Gherasim asked Jun 30, 2015 at 19:31 Adrian GherasimAdrian Gherasim 1351 gold badge4 silver badges10 bronze badges 4
  • 1 Seems to me that you don't have to initialize a variable in order to pare its type. Perhaps an uninitialized variable's type is already 'undefined'? – mah Commented Jun 30, 2015 at 19:34
  • Is this a jslint or jshint warning? Please tag your question appropriately. – Bergi Commented Jun 30, 2015 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 Commented Jun 30, 2015 at 19:47
  • @AdrianGherasim: Maybe because the shadow file is unlikely to contain "some text"? – Bergi Commented Jun 30, 2015 at 19:53
Add a ment  | 

1 Answer 1

Reset to default 4

The warning means just what it says - the variable might not have been initialised with a value at the time of the parison. 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 pare the value to undefined.

本文标签: javascriptvariable may not have been initialized when testStack Overflow