0

So I have been working with javascript for a website I am designing, shocker I know, I was trying to find a way to test if a variable did not exist or wasn't defined. After getting through this I think being undefined and not existing are two different things. Also I think its highly unlikely I found a bug but maybe someone with a better understanding of Javascript can explain to me why the following code works the way it does.

<script type="text/javascript">
var t1="";
var t2;
if (t1==undefined) {document.write("t1 is undefined");}
if (t2==undefined) {document.write("t2 is undefined");}
</script> 

The above code returns "t2 is undefined".

<script type="text/javascript">
var t1="";
if (t1==undefined) {document.write("t1 is undefined");}
if (t2==undefined) {document.write("t2 is undefined");}
</script> 

This second code crashes I believe. So in the first code t2 exists but is not defined? and in the second code it needs to exist before it can be undefined? I just figured that if I did not write "var t2;" then tested for it, it would be undefined. Hopefully I have explained this question enough.

Scott
  • 161
  • 1
  • 6
  • NO its not a duplicate you need to read more carefully. The issues are different I am not trying to figure out how to do it I am trying to figure out why the two codes act differently. Yes I have looked at a lot more than just to the right thank you. – Scott Mar 22 '12 at 07:00

3 Answers3

2

It's not a bug. In fact, the typeof operator is the only place where you can use an undeclared variable without getting an error.

See my answer Internet Explorer: "console is not defined" Error for a detailed explanation

edit:

This is how it's defined in the specs:

The production UnaryExpression : typeof UnaryExpression is evaluated as follows: 1. Let val be the result of evaluating UnaryExpression.
2. If Type(val) is Reference, then
a. If IsUnresolvableReference(val) is true, return "undefined".
...

Everywhere else, IsUnresolvableReference==true results in an error.

Community
  • 1
  • 1
user123444555621
  • 130,762
  • 25
  • 104
  • 122
  • Ok you got it, t2 was undeclared not undefined. I see now. I did experiment with the typeof operator but I kept crashing the code (prolly my fault with dumb syntax errors but it was a large code). Thanks for this. – Scott Mar 22 '12 at 07:13
  • Actually, what I said is not 100% right. You can of course use an unresolvableReference on the *left hand side* of an assignment like `foo=1` to set `window.foo` :) – user123444555621 Mar 22 '12 at 07:17
  • Well 99% was good enough for me seriously thanks this had me scratchin my head for a bit. – Scott Mar 22 '12 at 07:20
0

Well in your case if you have:

var t1 = "";

You will declare an empty string so it is normal to be "defined";

Whilst by doing:

var t2;

You are not defining it as any type of javascript object

Bogdan Emil Mariesan
  • 5,099
  • 1
  • 31
  • 54
  • Yes but if I do not type "var t2;" in the code am I also not defining it? – Scott Mar 22 '12 at 06:42
  • that is correct. by saying var t2; you mark that you might construct an object that will be wrapped by t2 bu until that object won't be created t2 will be considered as undefined. – Bogdan Emil Mariesan Mar 22 '12 at 06:47
  • So are you saying it is necessary to type "var t2;", which will create an object that is not yet defined. Does javascript need the object created before the object can read as undefined? Are you aware that the second code I have posted will not work? – Scott Mar 22 '12 at 06:53
  • In both cases t2 won't exist, in the first case in JavaScript you have it named let's say but you don't have it instantiated so that's why it is undefined, whilst in the second case you are attempting to use a variable that doesn't exist so again you will get undefined. – Bogdan Emil Mariesan Mar 22 '12 at 06:56
  • Thanks for your help you had useful posts but I think bmkorkut made me understand what was wrong a little more clearly. – Scott Mar 22 '12 at 07:05
0

It seems that there's a confusing naming convention here; We can say in the first case, you define variable t2 itself but it does not have a value defined. You can think the "undefined" property as the "value-undefined" to be more precise.

In the second case, you have t2 variable itself is NOT defined, thus you will get an error when you try to use it in code: "variable undefined", this error is because of a different reason but it is named similarly, making the situation a little confusing. Think this error as "variable-undefined" to be more precise.

I think you can just use the property "null" instead of the "undefined" in the first case to be clear.

var t1="";
var t2;
if (t1==null) {document.write("t1 is null");} 
if (t2==null) {document.write("t2 is null");} 

and;

var t1="";
if (t1==null) {document.write("t1 is null");} 
if (t2==null) {document.write("t2 is null");} // this line gives error because t2 variable is undefined
bmkorkut
  • 596
  • 4
  • 16