Concatenating Strings with Plus Operator why?

Tell us what’s happening:
myStr should have a value of This is the start. This is the end.…my question is :
which is the value??

Passed

Your code so far


// Example
var ourStr = "I come first. " + "I come second.";

// Only change code below this line

var myStr = myStr +  "This is the start. " + " This is the end.";


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/concatenating-strings-with-plus-operator

myStr has value of "undefinedThis is the start. This is the end."

Because myStr is not defined before that line, in that line myStr is undefined

how can I defined myStr??

This will define it as an empty string:

var myStr = "";

This will define it as a string with some words in it:

var myStr = "I am not empty";

Follow either of these with:

myStr = myStr + "This is the start. " + " This is the end.";

Please note that neither of the above will solve the exercise, though. Review freecodecamp’s example code. Notice the difference between:

var ourStr = "I come first. " + "I come second.";

and

var myStr = myStr + "This is the start. " + " This is the end.";

1 Like

still does not work, I had tried several ways

There are 2 problems with the above. First, as mentioned by @ilenia, myStr is undefined here so it cannot be concatenated. Let’s get rid of that.

var myStr = "This is the start. " + " This is the end.";

Now we’re close, but FCC still won’t accept our answer here. Hmmm… What’s different between our code above and FCC’s example at the start of our exercise? Ah, I see it. We have an extra space at the beginning of our string, and that extra space will make a difference.

The above code generates:

"This is the start. This is the end."

That’s too many spaces. Can you figure it out from here?

idafloo, I’d suggest going back about 5 lessons to String Variable declarations and review there: https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/declare-string-variables

This may help and Great Luck!

// Example
var firstName = "Alan";
var lastName = "Turing";