Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Primitives in Javascript (eg. 3) Technically Considered Expressions?

Tags:

javascript

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?

like image 270
machineghost Avatar asked Dec 13 '25 17:12

machineghost


2 Answers

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.

like image 77
Alex Taylor Avatar answered Dec 15 '25 06:12

Alex Taylor


Yes, they are expressions by themselves, operators and assignments are both optional.

Expressions

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:

  • Arithmetic: Evaluate to a number. eg. 5.2534. (Generally arithmetic operators are used)
  • String: Evaluates to a character string, for example, "Foo" or "123". (Generally string operators are used)
  • Logical: Evaluates to true or false. ( Generally logical operators are used)
  • Left-hand-side expressions: Left values are the destination of an assignment. eg. new or super
  • Primary expressions: Basic keywords and general expressions in JS. eg. this

Based on Mozilla Developer Documentation.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators

like image 21
colxi Avatar answered Dec 15 '25 07:12

colxi



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!