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