2

I found this PDF here and in it the author describes an expression as any valid set of literals, variables, operators, function calls and expressions that evaluate to a single value i.e.

3 + 7
3 + 7 + 10 + ""
"Dr." + " " + "Pepper"

That all seems fine to me. An a statement is any set of declarations, method and function calls and expressions that performs some action i.e.

var num = 1
document.write("hello")

But later on they refer to the last line of the examples below as statements

var salutation = "Greetings, "
var recipient = "Earthlings"
salutation + recipient //statement

var greeting = "Greetings, "
greeting += "Earthlings" //statement

Why isn't salutation + recipient and greeting += "Earthlings" considered an expression when they are adding two strings like in their expression example "Dr." + " " + "Pepper"

Many thanks

2 Answers 2

1

Because a statement can contain expressions.

A statement is any set of declarations, method and function calls and expressions that performs some action

Sign up to request clarification or add additional context in comments.

2 Comments

@ Justin - Thanks for the response. I understand that a statement can contain expressions by why is salutation + recipient a statement and "Dr." + "Pepper" an expression? They are both adding strings together.
@Nick Lowman: An Expression IS a Statement. An Expression is one kind of Statement. They aren't exclusive categories.
1

I think the first is a mistake, I suspect the author mean to type += instead of just +. The second is a statement because it's shorthand for

greeting = greeting + "Earthlings"

and you are assigning the result of the string concatenation (an expression) back to the original variable (which makes it a statement).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.