+= Vs + The difference?

hi everybody
I have some questions to ask
1- what’s the difference between?

var myStr = "This is the first sentence. ";

myStr += “This is the second sentence.”;

and
they’re looking like they’ve similar purpose?

var myStr = "This is the first sentence. ";

myStr + “This is the second sentence.”;

2- what is the role of \f?

This is shorthand for myStr = myStr + "This is the second sentence."

In the second case you are not assigning myStr + “This is the second sentence.” to anything so the value of myStr doesn’t change.


Where is it in your code? I don’t see it, I need context to answer that

1 Like

Thanks a lot for your help :slightly_smiling_face:
So what i understand is += you give to the variable a new value than the older one, is this correct?

But the + is just to perform mathematical calculations or concatenation not to assign a new value to a variable, is this correct again?

Where is it in your code? I don’t see it, I need context to answer that

That question is seperated it’s about Escape Sequences (RegExp) I use it without any change in the output string?
e.g:
Input: “this is a second \f sentence”
Output: this is a second sentence

No change

With += you update the old variable making a mathematical operation on it (you can also use -=, *= and /= for subtraction, multiplication and division respectively), it is a shorthand way to not repeat the name of the variable (like instead of a = a + 1 you can write a += 1, or in this case that you are changing by one even a++). + on its own can’t change the value of a variable.


It seems correct that \f doesn’t have a visual effect in your console. You can read about what \f is in this wiki page

1 Like

I get the point thanks a lot again :grinning: :slightly_smiling_face: