JavaScript Mozilla states expressions are also statements. Why?

Looking at these articles from Mozilla's JavaScript guide:

Expressions

Statements

expressions are also considered assignment statements. In fact, in the second article one can read "any expression is also a statement". Being acquainted with other programming languages, I thought that expressions are always values, but they never cause side effects like statements would do. In other words, 7, 7 + 8, "string", etc., are expressions, because they don't change the state, but a = 7 is a statement, since a variable has now been defined (i.e. a state has changed).

Why would Mozilla not differentiate between the two in JS?

Answer:1

I believe you are taking the terms "expression" and "statement" too literally. "Expressions not changing any state" is a very tough requirement for a programming language.

A thought experiment: In 7 + 8 substitute 8 with a function call to

var globalVar = 0;
function my8() { 
  globalVar = globalVar + 1; 
  return 8;
} 

Is 7 + my8() a statement or an expression? There is no obvious state change happing here, but still my8 performs a state change. Using the "no side-effects" definition it would be impossible to decide if 7 + my8() is a statement or an expression without analyzing the code of the my8 function. Of course it would be possible to simply prohibit any state change as part of a function call, but that is not the way of JavaScript.

In my experience most languages define "everything which returns a value" as an expression and a statement, everything else as just a statement.

To answer your question "Why would Mozilla not differentiate between the two in JS?":

I think they do, but not in the manner you expected. To consider "everything which returns a value" an expression seems to be the most practical approach.

Also there is no contradiction between a chunk of code being a statement and an expression at the same time. That is simply how Javascript and many other languages work. Of course it is always possible to draw a more strict line between those two.

Examples:

Assignments return values, so this is possible:

 a = b = c = 1;

It can be written in the more obvious form:

 a = (b = (c = 1));

Because of that an assignment is considered an expression (and also a statement).

On the other hand:

if (true) { };

does not return a value (in Javascript!) and therefore is no expression (but still a statement).

Answer:2

An expression is a code fragment that returns some value, Expression (Computer Science):

3; // 3
{}; // Object
func(); // whatever func returns, or undefined if not specified

You can combine expressions into one compound expression:

3 + 7; // 10
{}, []; // Array. Comma operator always returns result of right-most expression

A statement is the smallest valid code fragment that can be compiled or interpreted, Statement (Computer Science):

5; // valid js

You can also combine statements into compound statements:

check || func(); // valid js
{
    4 + 9;
    "block statement";
}

In the Mozilla documentation, a statement refers to any (compound) statement that is explicitly or implicitly terminated by semi-colon (;).

[,,[],[,[,,],,]]; // Array declaration whose reference is returned (and ignored)
// Multi-dimensional array with empty (undefined) elements

In some programming languages the above example doesn't compile or doesn't get interpreted. Other languages might not allow for the result of an expression not to be catched.

Javascript is very expressive, which is why every expression counts as a valid statement. Some statements are not expressions, like break, return, while, etc. They don't return any value, but they control the program execution flow.

Answer:3

We are developing a PhoneGap app. We have 3 cases like this: $("#dvData .quantity input").on('keyup', function() { var totalamount = 0; var discountAmount = 0; var LineAmount1 = 0; ...

We are developing a PhoneGap app. We have 3 cases like this: $("#dvData .quantity input").on('keyup', function() { var totalamount = 0; var discountAmount = 0; var LineAmount1 = 0; ...

I want to know if it is possible to use ng-repeat in a model that look like this: Example Model: $scope.items = { item1:{[1,2,3,4,5]}, item2:{[a,b,c,d,e]}, item3:{[a1,b2,c3,d4,e5]} }; The ...

I want to know if it is possible to use ng-repeat in a model that look like this: Example Model: $scope.items = { item1:{[1,2,3,4,5]}, item2:{[a,b,c,d,e]}, item3:{[a1,b2,c3,d4,e5]} }; The ...

I am trying to create textboxes inside <div id="screens"> </div>. I have a drop down menu for which I select the number of textboxes to be created. The JavaScript code I've written here ...

I am trying to create textboxes inside <div id="screens"> </div>. I have a drop down menu for which I select the number of textboxes to be created. The JavaScript code I've written here ...

  1. create text field dynamically javascript
  2. create text file dynamically in c#
  3. create text box dynamically in asp.net
  4. unity create text dynamically
  5. create text file dynamically in ssis
  6. create text file dynamically in java
  7. create static text dynamically mfc
  8. create text field dynamically
  9. create textbox dynamically in c#
  10. dynamically create text file in php
  11. jquery create input text dynamically
  12. javascript create input text dynamically
  13. dynamically create text boxes asp.net c#
  14. dynamically create text file javascript
  15. create kendo textbox dynamically
  16. dynamically create textbox in jquery

I'm using gridster to make a grid of links. The link should work normal when click on it. Problem is it's also get clicked after dragged. How can I stop it from being clicked after dragged? Please ...

I'm using gridster to make a grid of links. The link should work normal when click on it. Problem is it's also get clicked after dragged. How can I stop it from being clicked after dragged? Please ...

  1. prevent click while dragging
  2. jquery prevent click while loading
  3. jquery prevent click while animating