In programming, what is an expression? [closed]
Asked Answered
P

1

24

I've Googled this question, and searched on SO, however I can't seem to get a straight answer.

Is this question so basic no-one has thought to ask it yet?

Can someone please explain what exactly an "expression" is in programming.

Also I program primarily in Javascript, if the definition varies in JS could you please also highlight the difference?

Preceptor answered 26/8, 2013 at 12:13 Comment(2)
I don't see how this is "too broad", there is a very clear way to answer this question.Ayn
This question shouldn't have been closed, sometimes gatekeepers of SO becomes too focused on the content rather than seeing the big picture, I had been programming for quite a while, and realized that I am still not very clear with the term.Aube
C
28

In Javascript:

An expression is any valid unit of code that resolves to a value.

Conceptually, there are two types of expressions: those that assign a value to a variable and those that simply have a value.

The expression x = 7 is an example of the first type.
This expression uses the = operator to assign the value seven to the variable x. The expression itself evaluates to seven.

The code 3 + 4 is an example of the second expression type.
This expression uses the + operator to add three and four together without assigning the result, seven, to a variable.

JavaScript has the following expression categories:

  • Arithmetic: evaluates to a number, for example 3.14159. (Generally uses arithmetic operators.)
  • String: evaluates to a character string, for example, "Fred" or "234". (Generally uses string operators.)
  • Logical: evaluates to true or false. (Often involves logical operators.)
  • Object: evaluates to an object. (See special operators for various ones that evaluate to objects.)"

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

here is Microsoft's explanation of expressions in .NET

Cannot answered 26/8, 2013 at 12:17 Comment(2)
This is largely, but not completely accurate. In some languages, e.g. Python, assignments are not expressions and do not have a value in the way that they do in C++, for instance. Other statements may have no value and yet assign no value to a variable, e.g. calling a function that returns no value, defining a class, declaring a function, and so on.Pola
Is a combination of one or more constants, variables, operators, and functions that the programming language interprets and computes to produce another value. , Literal expression or Literals: An expression whose values actually written in the code. Integer literal: A whole number whose values appears right in the code, such as 123. String literal: Is a string whose value right there in the code, such as "ABC", Arithmetic expression: Expression created using arithmetic operatorsMoneywort

© 2022 - 2024 — McMap. All rights reserved.