What is an expression what is a statement difference between expression and statement?

Start of main content

What is an expression what is a statement difference between expression and statement?

JavaScript distinguishes expressions and statements. An expression is any valid unit of code that resolves to a value. A statement is a unit of code that performs an action. Some examples:

let x = 0;
function add(a, b) { return a + b; }
if (true) { console.log('Hi'); }


x;          
3 + x;      
add(1, 2);  

Anywhere JavaScript expects a statement, you can also write an expression. This kind of statement is called an expression statement. Conversely, you cannot write a statement where JavaScript expects an expression.

  • Prepare for your next JavaScript interview with this collection of interview questions and answers.

  • JavaScript has three different empty states for variables. Learn their differences and how you can check for each one.

  • Learn all you need to know about the differences between JavaScript's double equals and triple equals operators.

  • Clones a regular expression.

As developers, we live in a world that is a kind of abstraction of the real world. The best example of this abstraction is the software language. Compare to human language, software languages consist of different tokens. There isn’t any word, subject, or adverb like human language. However, all languages should consist of different components. In the software world, languages consist of functions, statements, or expressions, etc.

Almost all developers are able to understand which part of the code is a function. Because functions are explicitly declared in some software languages. For instance, in Python, the function should be declared with def keyword. In addition to that, in Javascript, functions can be defined with function keyword.

Well, how can a developer understand the statement or expression in the bunch of program code?

In this article, I am going to explain what is the difference between expression and statement in software languages. To illustrate the difference, Go and C languages will be used as an example.

Before diving the difference between C and Go languages, let me explain the definition of statement and expression.

“A statement performs some action such as printing values, looping, or if statement. On the other hand, expressions also produce values and we can assign these values to new variables.”

“Go is a compiled, concurrent, garbage-collected, statically typed language developed at Google. For detail, you may consider reading Rob Pike’s keynote talk.”

“Rob Pike is one of the designer of Go.”

There are many differences between Go and C. However, I am going to talk about two differences to give an example of our topic.

Now, we can begin with the first difference,

In Go,

++ and -- are statements not expressions

Unlike Go, in C language, increment and decrement operators are expressions. Therefore, we able to assign produced values to new variables like below.

When this code executed, there will be no error. Because the increment operator is an expression in C language. The printed value will be 10 because the postfix increment operator is used. Because of this, at first, the initial value of a is assigned to b.After that, a will be increased to 11. Therefore b is initialized with the first value of a.

If we use the prefix increment operator, the value of b will be the same with the value of a. Because the assignment operator will be executed after the increment operator.

In this case, the printed value will be 11.

On the other hand, in Go, increment, and decrement operators are not expressions. That's why, if we try to assign this value, our code will not be compiled. We will get a syntax error. Because there is not a returned value from this operation.

Let’s see the incorrect Go code. ⚠️

How can we use increment operator in Go? 🤔

As mentioned, in Go, the increment operator is not expression. The operator affects the operand directly. It will not produce any value. Therefore, we have only a postfix increment operator.

The printed value will be 2.

One more difference to compare to C,

The assignment is not an expression

As mentioned, expressions generate values. In C, when we assign a value to a variable, this assignment produces value.

In this code block, the value of a is assigned to p as below.

p = a

In C, as mentioned, the assignment operator produces a value that is assigned value. In this case, the value of t will be 10. Therefore, the printed value is 10.

However, in Go, the assignment operator is not an expression. ⚠️

If we try to compile this code block, we will get a syntax error. Because the assignment operator is not expression.

To sum up, expression and statement are an important part of software language. To understand the difference between these two terms helps us to fathom the intention of the language.

References:

What is a statement in Python How is a statement different from expression?

An expression can be defined as any element in our utility that evaluates some values. An expression evaluates a value. A declaration does something. Statements represent a motion or command e.g print statements, mission statements.

What is the difference between a statement and an expression in C?

An expression is something that returns a value, whereas a statement does not. The Big Deal between the two is that you can chain expressions together, whereas statements cannot be chained. Sure statements can be chained.

What's the difference between an expression and a statement Scala?

If you know programming in any language, you already know about statements and expressions. A statement is the smallest standalone element that expresses some action to be carried out. Whereas an expression in a programming language is something that produces or returns a value.

What is difference between statement and expression in Java?

A statement forms a complete unit of execution. The following types of expressions can be made into a statement by terminating the expression with a semicolon ( ; ). Such statements are called expression statements. Here are some examples of expression statements.