Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I didn't know what to answer to my students

Tags:

javascript

My students are very beginners.

When I taught them JavaScript, I showed them this function declaration:

function test() {
  /* code */
}

Then I showed them that you could do some input directly into the console of the Webbrowser. I then show them arithmetic operations: 1+2, 6*3 and so on.

One of my students asked me to try this and I was pretty sure it would not work but it did:

function test() {
  1+3;
}

What could I say to explain this? And what could I say when you enter some assignation in the command line, like var i=5; the return result is undefined?

like image 704
Olivier Pons Avatar asked Dec 01 '25 05:12

Olivier Pons


2 Answers

Unlike some other languages, JavaScript allows expressions as statements, see §12.4 ("Expression Statement") of the specification. So that code is valid, it just doesn't have any enduring effect.

It's the same reason that this works:

foo && foo();

That's a freestanding expression just like your 1+3;. In this case, of course, it calls foo only if foo is not falsey.

Philipp points out in the comments that ECMAScript5's "use strict"; is another example of this. Technically in ECMAscript5 it's a directive, but it works with pre-ECMAScript5 engines because to them it looks like an expression statement.

And what could I say when you enter some assignation in the command line, like var i=5; the return result is undefined?

Because statements don't have a result, only expressions. var is a statement. If you did them separately, you'd get this:

> var i;
undefined
> i=5;
5

...because the result of an assignment expression is the value being assigned.

like image 56
T.J. Crowder Avatar answered Dec 03 '25 17:12

T.J. Crowder


When you type something in a browser console you're creating a javascript Program (even if it's as small as 3+2, it's still a program). A program is a sequence of Statements and FunctionDeclarations, separated by (possibly implied) semicolon. JS evaluates all statements and declarations in order and the result of the whole Program is the last non-empty result. While FunctionDeclaration always yields an empty value (undefined), some Statements can return non-empty results. These are listed in the "Statements" section in the standard (http://ecma-international.org/ecma-262/5.1/#sec-12), look for the stuff like

Return (normal, V, empty) or Return (normal, empty, empty)

where the middle part denotes the return value of the statement.

Examples:

> var a=1; function x() { 3+1 }
undefined

this returns nothing because nether var statement, nor a decl have a value.

> 3 + 2
5

This returns 5, because expression statements have values.

> function x() { 3 + 2 }
undefined

This is undefined because FunctionDeclarations don't have values.

> var a = 10; while(a>3) a--; function x() {}
4

This one returns 4 because while does have a value. 

> var a = 10; if(a>3) a--
10

and so does if.

Note that while most JS statements do have values, there's no way to obtain or use these values in a JS program (apart from eval(), which the console does under the hood). Statements are being evaluated, but their results get lost, and the only real purpose of a statement is to produce side effects. This somewhat strange behavior is a result of Javascript being a functional language (where everything has a value) with procedural syntax. In other words, semantically it would be no problem to allow something like

 return if(a > 10) 5 else 0

or

x = 5 + do { r = nextVal() } while(!used(r))

The only reason why this doesn't work is an arbitrary syntactical limitation.

like image 30
georg Avatar answered Dec 03 '25 17:12

georg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!