2

In chrome Developer tools, when I type
a = 3 logs 3 but var a = 3 logs undefined.

Why does the first statement return 3 but not the second one?

2 Answers2

5

Because that's the way it is.

A statement beginning var is a declaration. Declarations don't, in and of themselves, have a value. They tell the computer to do something (to create a variable, optionally with some initial value).

But assignment expressions are different. a = b evaluates to (or "has") the new value of a, in order to allow chaining, such as a = b = c = d.

Could they have made it so that var a = b was an expression and evaluated to something? Sure, probably. But it would hold absolutely no useful value to butcher the language grammar in such a manner.

Read up about statements and expressions in programming languages.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
3

Because undefined is the result of the var statement.

Statements don't actually have a "result" that can be used in your code, but a program has a final result, and your single line of code in the console is evaluated as a program.

  • Don't confuse evaluation and execution. – Lightness Races in Orbit Aug 04 '18 at 14:50
  • @LightnessRacesinOrbit: Well, it's "evaluated" in the sense that `eval` "evaluates" a snippet of code and provides its result. The code is evaluated as a full program, not necessarily a single expression. –  Aug 04 '18 at 14:52
  • While a C or C++ program returns a value to the surrounding environment, that is not true of JavaScript in a browser. What is the value that is the result of the program? There isn't one. Similarly, referring to your comment on Rishikesh's answer, you said `var a = 3` "is definitely evaluated" but given that it has no value that is obviously not the case. – Lightness Races in Orbit Aug 04 '18 at 14:53
  • @LightnessRacesinOrbit: It does have a value, just not one that can be accessed as a value within the program, unless `eval` was used to execute it within the program. For example: `var x = eval("for (var i = 0; i < 10; i++) {i}"); console.log(x); // 9` –  Aug 04 '18 at 14:55
  • 1
    That's a [completion value](http://www.mattzeunert.com/2017/01/10/whats-a-statement-completion-value-in-javascript.html). It's not "the result of the program"; it's the value of the last evaluated expression within said program. Cherry-picking a program with only one expression is cheating! – Lightness Races in Orbit Aug 04 '18 at 14:56
  • @LightnessRacesinOrbit: Hence the distinctions I've made. In my other comment, the code is evaluated just as `eval` would and provides the resulting value to the console. To say it isn't evaluated would be incorrect, and would imply that there is now no `a` variable. –  Aug 04 '18 at 14:58
  • Not sure what I can add over what I've already said. – Lightness Races in Orbit Aug 04 '18 at 14:58
  • ...oh goodness, "result of the program" is an informal way of referring to the completion value. A program spits out a final result. That result is a potentially usable value to whatever executed the program. –  Aug 04 '18 at 14:59
  • So now we're changing the meaning of words to fit our desired conclusions.. noted! Have a nice day Zach – Lightness Races in Orbit Aug 04 '18 at 14:59
  • @LightnessRacesinOrbit: No, I'm only using informal terminology. We do it every day. Not all communication needs to be perfectly formal. –  Aug 04 '18 at 15:01
  • The program I showed above ends with a `for` statement, yet it provides a result value. It's not cheating. That's silly. The value of the result is that which is provided by the last statement, so anything before it becomes irrelevant. It's not cherry-picking; it's providing a simple example that demonstrates the point. –  Aug 04 '18 at 15:13