In Javascript I know that, for instance, 1 + 2 is an expression ... an expression that returns 3. Since an expression basically means a thing that returns a value, and 3 returns the value 3, it would seem like 3 itself would also be an expression.
However, I've never seen any examples of expressions that were simply primitive values, so I'm curious: is 3, or for that matter 'foo', technically considered to be an expression?
According to the Antlr ECMAScript grammar, an expressionStatement is composed of expressionSequence, which is composed of singleExpression, which can be a literal. Here are the relevant parts:
expressionStatement
: expressionSequence
;
expressionSequence
: singleExpression ( ',' singleExpression )*
;
singleExpression
: Function Identifier? '(' formalParameterList? ')' '{' functionBody '}' # FunctionExpression
... # Cut for brevity
| literal # LiteralExpression
| arrayLiteral # ArrayLiteralExpression
| objectLiteral # ObjectLiteralExpression
| '(' expressionSequence ')' # ParenthesizedExpression
;
literal
: ( NullLiteral
| BooleanLiteral
| StringLiteral
| RegularExpressionLiteral
)
| numericLiteral
;
However, I've never seen any examples of expressions that were simply primitive values
That depends on what you think an expression is.
Yes, they are expressions by themselves, operators and assignments are both optional.
An expression is any valid unit of code that resolves to a value.
Conceptually we can consider two types of expressions:
With Assignment (side effects) : Assign value to a variable. Eg: x=23
With Evaluation : Those that evaluate and therefore resolve to a value. Eg: 6*6
JavaScript has the following expression categories:
5.2534. (Generally arithmetic operators are used)"Foo" or "123". (Generally string operators are used)true or false. ( Generally logical operators are used)new or superthisBased on Mozilla Developer Documentation.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With