admin管理员组

文章数量:1393866

I am wondering Can you assign a variable to alert ? what does it really mean and do ? For example,

var a = alert('test');  

I tried it out, and the alert pops up as soon as the page loads where variable a remains 'undefined' when I call it. Aren't we suppose to make it an anonymous function with the alert inside like

var a = function(){ alert('test'); }

If we want to trigger an alert on something? Why does javascript allow you to do that?

I am wondering Can you assign a variable to alert ? what does it really mean and do ? For example,

var a = alert('test');  

I tried it out, and the alert pops up as soon as the page loads where variable a remains 'undefined' when I call it. Aren't we suppose to make it an anonymous function with the alert inside like

var a = function(){ alert('test'); }

If we want to trigger an alert on something? Why does javascript allow you to do that?

Share Improve this question edited Sep 12, 2012 at 2:28 John Kugelman 362k69 gold badges552 silver badges596 bronze badges asked Sep 12, 2012 at 2:17 peterpeter 8,47317 gold badges75 silver badges95 bronze badges 3
  • alert return nothing or other means return undefined, what do you want to achieve? – xdazz Commented Sep 12, 2012 at 2:20
  • I am just wondering why javascript allows we to do that if it doesn't really means anything. – peter Commented Sep 12, 2012 at 2:22
  • @user1389813: Consider a function that, based on its inputs, may return something or may not return something. You want to be able to store the return value when it returns something, but for consistency, JavaScript also allows you to assign its return value when it does not return anything. A function that never returns anything can also have its return value assigned to a variable, again for consistency. – icktoofay Commented Sep 12, 2012 at 2:25
Add a ment  | 

4 Answers 4

Reset to default 5
var a = alert('test');

Think of this statement like any other variable assignment. In order to perform the assignment, first the right-hand side is evaluated. In this case it's a function call, so the function is called and its return value is obtained. In alert's case, the return value is undefined. It doesn't return anything.

Then the return value is assigned to a, so a ends up with the value undefined.

As a side effect of the statement, an alert message is displayed. That happens as a consequence of calling alert() and getting its return value.

function foo(x) {
    return x * 2;
}

var a = foo(3);

This code is structurally similar to the alert call, and would result in a being 6. But then alert() doesn't return a value. It's more like this:

function bar(x) {
    return;
}

var a = bar(3);

Now a is undefined.

var a = alert('test');  

This says to execute alert('test') and assign the return value from that function to a variable named a.

This would be no different than:

var max = Math.max(1,2,3,4);

where max would end up with the value 4 in it as the return value from executing Math.max(1,2,3,4).


var a = function(){ alert('test'); }

This says to declare an anonymous function and assign that function object to the variable a. Since the function is just declared, it is not executed at this time. You can execute it in the future with a().

You mented:

I am just wondering why javascript allows we to do that if it doesn't really means anything

Well, JavaScript (and any other language, including English) allows you to do a lot of stuff that does not mean anything, as long as the syntax is valid. For example, the snippets bellow also mean nothing:

var a;
a = a; // so what?

function something() { /* nothing */ }
var b = something(); // very similar to your example!

Wouldn't it be less consistent if you could assign from some functions, but not from others? If the language were typed, that would produce an error, but since it's not, what's the problem with it? If they don't return a value, their return is undefined, and nothing breaks if you try to assign that to a variable. So you can make functions that sometimes return something, sometimes not. That can be an advantage if used wisely. It's a feature, not a problem with the language.

If you do:

 var a = function(){ alert('test'); }

You can then do:

a();

since you've defined an anonymous function..

alert is a function so you can't assign anything to it, per se.

If you want something to pop up if a variable has a value, you could do something like -

$(document).ready(function() { 
  if (someVar != undefined) {
    alert(someVar);
  }
});

本文标签: Javascript assign variable to alertStack Overflow