0

I have seen a bunch of articles talking about "what is an expression in Js?" but some of them said that "an expression in Js is any piece of code that produces a value" like this one 4 + 4, so this one is of course an expression the value that this will produce is 8, but what about if we only a single value of type:x, like this: 4, they also said that this is an expression but this really doesn’t match or isn’t too accurate with the previous definition, and the other case is that if we do this x = 2, that is considered also an expression an if we move forward x is also an expression, so, what really is an expression? is there different types of expression in Js? which ones are this? what are they differences? what is the really accurate definition for this concept?, please someone with this knowledge I really appreciate if you can clarify this to me!

  • This is a pretty complicated subject that's not really appropriate for an SO answer. – Barmar Feb 01 '23 at 20:07
  • 3
    There is one kind of expression, and it is defined by the language grammar. The JavaScript grammar is very complicated so that's not the best place to start. When you're learning the language, using a simple example like `4 + 4` is a fine way to think about what some documentation means when it says "expression". – Pointy Feb 01 '23 at 20:07
  • 3
    An expression is a value, **OR** anything that *executes and ends up being* a value. – Zak Feb 01 '23 at 20:07
  • 1
    Look at a syntax diagram for js -- then it will be clear when, how, and why what things are expressions – Hogan Feb 01 '23 at 20:08
  • 2
    *"but this really doesn’t match or isn’t too accurate with the previous definition"* - Why not? `4` is syntactically valid and produces a value. – David Feb 01 '23 at 20:10
  • When you have a group of terms and you’re not quite sure of the difference between them, it can be handy to Google ‘term1 vs term2’ to seek articles that specify the difference between them, eg [Statement vs Expression – What's the Difference in Programming?](https://www.freecodecamp.org/news/statement-vs-expression-whats-the-difference-in-programming/amp/) – user1063287 Feb 01 '23 at 20:13
  • @Zak An expression is not a value. At best, an expression *has* a value or *evaluates to* a value. – Bergi Feb 01 '23 at 20:26
  • 1
    "*what is the really accurate definition for this concept?*" exactly what you stated - anything that produces a value after evaluation. If you can take some unbroken line of code (not terminated by `;` even if implicit) and can assign it to a variable, or pass it to a function, it's an expression. `const x = 4+4` and `fn(4+4)` are both valid. As is `const x = 4` and `fn(4)`. Both `4+4` and `4` are both expressions. Something that *doesn't* is an `if` as an example. `const x = if (true)` is invalid code. As is `fn(if(true))`. `if` *statements* are not expressions. – VLAZ Feb 01 '23 at 20:27
  • 2
    Yes, `4+4`, `4`, `x = 2`, `x` all are expressions in JS code. You might want to take a look at some things that are *not* expressions to understand the difference. – Bergi Feb 01 '23 at 20:27
  • Also see [Statement vs. expression](https://exploringjs.com/impatient-js/ch_syntax.html#statement-vs-expression) in the incredibly detailed but very readable [JavaScript for impatient programmers](https://exploringjs.com/impatient-js/toc.html) – user1063287 Feb 01 '23 at 20:43

0 Answers0