最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - var undefined = true; - Stack Overflow

programmeradmin0浏览0评论

I'm doing some experimenting with this malicious JavaScript line: var undefined = true;

Every uninitialized variable in JavaScript has the value of undefined which is just a variable that holds the special value of 'undefined', so the following should execute the alert:

var undefined = true, 
    x;

if (x) {
    alert('ok');
}

But it doesn't, and my question is why?

On further experimentation, I tried the following:

var undefined = true, 
    x = undefined;

if (x) {
    alert('ok');
}

This time, the alert is executed.

So my question is...since in the first snippet x holds undefined (because it is not initialized), why didn't the alert execute? The strange thing is that when explicitly stating that x is undefined (x = undefined), the alert executed...

I'm doing some experimenting with this malicious JavaScript line: var undefined = true;

Every uninitialized variable in JavaScript has the value of undefined which is just a variable that holds the special value of 'undefined', so the following should execute the alert:

var undefined = true, 
    x;

if (x) {
    alert('ok');
}

But it doesn't, and my question is why?

On further experimentation, I tried the following:

var undefined = true, 
    x = undefined;

if (x) {
    alert('ok');
}

This time, the alert is executed.

So my question is...since in the first snippet x holds undefined (because it is not initialized), why didn't the alert execute? The strange thing is that when explicitly stating that x is undefined (x = undefined), the alert executed...

Share Improve this question edited Nov 24, 2012 at 9:37 Denys Séguret 382k90 gold badges810 silver badges775 bronze badges asked Mar 28, 2010 at 19:47 Andreas GrechAndreas Grech 108k101 gold badges303 silver badges362 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 17

There is a difference between a variable named undefined and the value called undefined.

var undefined = true, 
    x;

In this example, the variable undefined is set to the value true, and x to the value (not the variable!) undefined.

var undefined = true, 
    x = undefined;

In this example, the variable undefined is set to the value true as well, and x is set to the value contained in the variable undefined (which is true).

So, while you can declare a variable named undefined, you cannot change the fact that non-initialized variables are set to the value undefined.

Just declaring a variable called "undefined" does not mean that you're overriding the built-in concept of what the native "undefined" value is.

Imagine if Java would let you use "null" as an identifier. Well, I guess Java doesn't have the same coercion as Javascript. Anyway the Javascript statement

if (x) alert("foo");

involves an implicit coercion of the value of "x" to boolean. The value isn't defined, so its coercion to "boolean" results in false.

Uninitialized variables get the special value undefined. When you assign a variable to another variable you're giving it a string that references a variable you've defined within the current scope. In this case you've defined a variable named undefined so the JavaScript engine will look first through the variables, see that you've named one undefined and then assign it that variable.

发布评论

评论列表(0)

  1. 暂无评论