screenm0nkey
screenm0nkey

Reputation: 18795

Still confused about JavaScript Expressions and Statements

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

Upvotes: 2

Views: 193

Answers (2)

Paolo
Paolo

Reputation: 22638

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).

Upvotes: 1

Justin Niessner
Justin Niessner

Reputation: 245429

Because a statement can contain expressions.

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

Upvotes: 1

Related Questions