2

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.

Bltucker
  • 447
  • 3
  • 9
  • 17

6 Answers6

2
  1. console.log doesn't print anything when it receives a undefined value (implementation-specific).
  2. When you concatenate undefined with an empty string, the resulting value is "undefined". That's printed.

The previous bullet points explain your observations.

Rob W
  • 341,306
  • 83
  • 791
  • 678
2

When you concatenate undefined with a string it becomes a string

var foo = undefined;
var bar = "";
console.log(foo+bar, typeof(foo+bar));
* output *
undefined string

ECMAScript Docs - http://www.ecmascript.org/docs.php Not sure but reading the doc on Page 141 you can find this

15.5.1 The String Constructor Called as a Function

When String is called as a function rather than as a constructor, it performs a type conversion.

Which lets me to believe why concatenation of anything with a string outputs a string

rroche
  • 1,262
  • 1
  • 13
  • 29
1

Rewriting your example might shed more light on what actually happens when concatenating a string and an undefined value:

var noDefinition = undefined;
var emptyString = "";
var noDefinitionAndEmptyString = noDefinition + emptyString;
console.log("************************");
console.log("NoDefinition:" + " " + noDefinition + " " + typeof(noDefinition));

console.log("EmptyString:"+ " " + emptyString + " " + typeof(emptyString));

console.log("noDefinition+emptyString: " + noDefinitionAndEmptyString + " " + typeof(noDefinitionAndEmptyString));

console.log("************************");

Results in:

************************
NoDefinition: undefined undefined
EmptyString: string
noDefinition+emptyString: undefined string
************************
jdi
  • 90,542
  • 19
  • 167
  • 203
Juan Carlos Moreno
  • 2,754
  • 3
  • 22
  • 21
0

Use this instead:

if (typeof noDefinition == "undefined") ...
BPS
  • 1,606
  • 20
  • 37
0

noDefinitionAndEmptyString is actually a string (you can use console.info(typeof noDefinitionAndEmptyString)) and its value is 'undefined'. This happens because of the concatenation - an implicit cast to string occurs, and undefined variable, casted to string, is the string 'undefined'

Maxim Krizhanovsky
  • 26,265
  • 5
  • 59
  • 89
0

if(noDefinition == null) covers typeOf 'undefined' and typeOf 'null', but you may want to add an and statement to make sure it's just not an empty string... like so:

if(noDefinitionAndEmptyString == null || noDefinitionAndEmptyString == "")

Eric Hodonsky
  • 5,617
  • 4
  • 26
  • 36