What does `+ ','` mean?

What is the meaning of + ‘,’


Hi there! Can you please explain the role of + ‘,’ after every step in making sandwich?


That is the concatenation operator for joining strings and variables to form a single string. This is the old form of string interpolation.


what is the meaning of +’,’ . Also the repetition of “topping1 and topping2” can not be repeated thus the function can not call duplicated layers; why?

1 Like

This is meant to be an extremely simple example of the usefulness of functions in coding. Without the
+ ',' the return value of sandwich would be breadhamcheesebread instead of bread,ham,cheese,bread. It would be even more readable if we added a space in addition to the comma by changing it to + ', '.
The return value of sandwich would then be bread, ham, cheese, bread.

This function can be customized in any way the programmer wishes. If we wanted to repeat the layers to make a double-decker sandwich (i.e. “bread,ham,cheese,ham,cheese,bread”) we could re-write it like this:

function makeSandwich(topping1, topping2) {
  sandwich = 'bread' + ','
  sandwich += topping1 + ','
  sandwich += topping2 + ','
  sandwich += topping1 + ','
  sandwich += topping2 + ','
  sandwich += 'bread'
  return sandwich

The “Learn How to Code” course is a beginning course. There are many ways this example function could be refactored to be more concise and more useful. As you progress through learning to code you’ll see what I mean. Happy coding!


// Define the ‘makeSandwich()’ function
function makeSandwich(topping1, topping2, topping3) {
sandwich = ‘bread’ + ‘,’
sandwich += topping1 + ‘,’
sandwich += topping2 + ‘,’
sandwich += topping3 + ‘,’
sandwich += ‘bread’
return sandwich

// Call the function and store the returned value in ‘result’
result = makeSandwich(‘jelly’, ‘cheese’, ‘ham’)

I can’t solve the problem, what I did wrong ?

It could be just a matter of missing white space after the comma so the toppings are separated as in a sentence.

I think it’s just a comma, so it doesn’t read it as breadhamcheesebread, for example. Instead it reads it as bread, ham, cheese, bread. I don’t think it really matters because you’re not printing the strings anyways.

I just have one simple question, so why is the first line sandwich= ‘bread’ + ','
but at the end it it’s sandwich += 'bread’

Why use = at the start and use += at the end.

1 Like

The whole process is that of building a single string from all the aggregate components.

We start with the top slice, so it is a direct assignment…

sandwich = 'bread, '

It was/is unnecessary to concatenate with another literal character so above we have it included in the initial string value.

Given that we want to continue this string (as in grow it) we use the augmentation operator to preserve whatever value to we add to.

sandwich += 'ham, '
sandwich += 'cheese, '

The final augmentation will be the other slice of bread.

sandwich += 'bread'

Notice how we only use direct assignment when we are initializing the string? That should answer your question as to why. Assignment replaces whereas augmentation, well, augments. It adds to or continues.

1 Like

function makeSandwich(topping1, topping2,topping3) {
sandwich = ‘bread’ + ‘,’
sandwich += ‘peanut butter’ + ‘,’
sandwich += ‘jelly’ + ‘,’
sandwish += ‘pickles’ + ‘,’
sandwich += ‘bread’ + ‘,’
return sandwich

result = makeSandwich('peanut butter ', ‘jelly’, ‘pickles’)
where is the fault plz help me !!

Hello, @zinoubghbenz78810214.

Welcome to the forums.

Look closely at your spelling of sandwich on every line. :wink:

Also, you should be using topping1, topping2 & topping3 in your code in place of peanut butter, jelly & pickles
For example:

sandwich += topping1 + ','

thank u very much i see now the difference

1 Like

Very helpful answer. Thanks!

1 Like