Different output between + operator and comma

Take a gander at this:

function takeOrder(topping, crustType) {
console.log('Order: pizza topped with '+ topping '& '+ crustType);
}

This threw back an error. Seeing as the declared parameters are split with a comma, and the called inputs are split with one too. Easy peasy it prints how it should.

However, previous lessons show that you can wrap a parameter in + operators when called for in console.log
This prints
Order: pizza topped with ‘topping’& 'crustType’
compared to the correctly spaced output of what the comma separation gave.

I just wanna know why javascript interprets the two forms of syntax differently. It feels like quizzical time wasting and I’m not sure this makes much sense. But thanks!

Hello @lukebassett1,
The above code…

function takeOrder(topping, crustType) {
console.log('Order: pizza topped with '+ topping '& '+ crustType);
}

This one have thrown error cus of wrong implementation of string concatenation.
You missed concatenation operator ( + ) after topping.

I just wanna know why javascript interprets the two forms of syntax differently. It feels like quizzical time wasting and I’m not sure this makes much sense.

The method you’re trying to use is having concatenation operation involved.
On the other hand while printing with console.log(), there is no such operation involved and it prints values with a default space.

3 Likes

You just taught me string method concat(). Thanks!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.