Word Blanks | = or += operator?

Word Blanks | = or += operator?
0

#1

Tell us what’s happening:
var result = “”;
result+= " *strings + variables here* ";

I understand that the first line initializes the variable result. In the second line, this one is assigned a value (a sentence made out of strings and variables).
My question: why is it used += instead of just = in the solution? I can’t see the advantage of it.

Thanks!

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/word-blanks


#2

I can’t see all the example code in the challenge because I’m on my phone. Did you add += part? Usually when they give you a variable like result you just give it the value. If that makes sense? So you would erase the “” and add your solution to result instead of leaving it a empty string


#3

Hi Cody_Biggs,
What you wrote does make sense to me, but I was told that initialize variables this way is a good practice https://www.w3schools.com/js/js_best_practices.asp.
Also, var result = “”; is not deleted in the solution. And the += operator is used to assign var result the “new” value (which is a sentence made out of strings and variables) instead of the = operator only.
The code works fine with both operators, I’m just curious about why they decided to use one over the other.


#4

The challenge starts with the following code:

// Your code below this line
var result = "";

// Your code above this line

You can either replace the “” after var result = or you can leave that first line and add a second line like:

result += 

They both do the same thing. I would opt for the one liner instead of two, to keep the code more concise.

When you say they, who are you referring to and what did they write for their solution. The only way to know why they wrote it a certain way is to ask them.