I am not some one who has done alot in javascript. My background is in Java/C++. I am currently working on a project that is using alot of javascript however and I came across something that doesn't make sense to me but hopefully some javascript guru out there can give me a nice logical explanation.
var noDefinition = undefined;
var emptyString = "";
var noDefinitionAndEmptyString = noDefinition + emptyString;
console.log("NoDefinition");
console.log(noDefinition);
console.log("EmptyString");
console.log(emptyString);
console.log("noDefinition+emptyString");
console.log(noDefinitionAndEmptyString);
console.log("************************");
if(noDefinition == undefined)
{
console.log("No Definition is undefined");
}
if(emptyString == undefined)
{
console.log("emptyString is undefined");
}
if(noDefinitionAndEmptyString == undefined)
{
console.log("noDefiniton and emptyString is undefined");
}
The code above produces the following results in my console:
[INFO] NoDefinition
[INFO]
[INFO] EmptyString
[INFO]
[INFO] noDefinition+emptyString
[INFO] undefined
[INFO] ************
[INFO] No Definition is undefined
So as you can see when I output the variables noDefinition and emptyString to the console, it produces blank output. When I concatenate them the console will produce undefined. However if I then proceed to use an if statement and compare each of them to undefined. The only if that executes is the first one.
This occurs even though when put to a console the value just shows up blank. Also the concatenation which shows up as undefined in the console fails its compare against undefined and never executes. I am confused by this and I am hoping out there some one can give me an explanation about what is going on.