Why would we use string concatenation?

I appreciate you understood the question askers (and my own wonderment) are asking from a place of only what we know and have learned; when you wrote that “what if” we wanted it to be re-usable, exchangeable… it made more sense and we can easily see that LATER it will make sense. The hardest thing, for me, about coding is when you’ve never seen or done something before, it’s hard to remember when you do not understand it (that’s why the repetition in the courses is GREAT for me-something I normally would not like).

Ha, ha, now that I am thinking about it, you might use this if you were creating a type of MadLibs - changing out different words and showing different added-together strings after including the random words.

Anyway, thank you.

6 Likes

Yes. A madlib is a great example of when you would use string concatenation in the real world!

4 Likes

Why do we use the $ sign and the curley braces,is that an interpolation?

Yes, here is the documentation on template literals and here is an article on string interpolation.

Here is an example where string concatenation would be needed/useful:

let playerName = /*[what player types]*/;
document.Write('Hello, ' + playerName);

String concatenation must be used because the variable of the player’s name can have many different values. Therefore, the string cannot just be written out.

P.S. I have limited JS knowledge, so this code might not really work, but you should get the idea.

2 Likes

String interpolation allows you to easily insert variables and expressions into strings. If you used string concatenation, your code could look messy, like this:

const name = "John Doe";
const age = 32;

console.log("Hello! My name is " + name + " and I am " + age + " years old" );

Not only is it messy, but it’s easier to encounter an error as you might leave out a +
Instead, you can use string interpolation. To actually insert expressions into your string, yo do have to use ${EXPRESSION} and the string itself must be wrapped in backticks

const name = "John Doe";
const age = 32;

console.log(`Hello! My name is ${name} and I am ${age} years old`);
2 Likes

Technically, concatenation IS a form of string interpolation, sans the template literal expressions. This is just a newer, more practical form.

2 Likes

True.

@levi1237 Instead, you can use a newer and more concise form of string interpolation: template literals

I’m very new to java, but this popped into me head as a reason to append two strings that’s simple, though tedious to type out. . If you want to use

console.log(‘Why do “you” append strings if there’s no reason?’);

It will mess up because you have both double and single quotations in what you want to print. But it will work if you use

console.log('Why do ’ + '“you” ’ + “append strings if there’s no reason?”)

A less tedious way to print that is by using back quotes instead of single or double quotes. Notice in the next example that what I want to print is opened and closed with back quotes so they don’t interfere with the single or double quotes. (okay so the back quotes messed up the formatting of the text box on this forum so I put an @ symbol in place of the back quote)

console.log(@Why do “you” append strings if there’s no reason?@);

Other’s have given more useful reasons. I just found this a simple way to understand why anyone would ever need to append strings.

Really good tip. Thanks for the lesson!

1 Like

This reply completely cleared up my similar question but also really reinvigorated me. Thank you for renewing my determination to learn how to code! You’re a truly inspiring tutor.

1 Like

This reply made me search for the upvote button and used it for the first time.
Thank you for that clear, forest instead of individual trees, comment!

2 Likes

I like how you explain, but since I’m just getting started here, I won’t assume I understand it all. Thanks

1 Like

PERFECT! Really good example.
Thanks mate!

1 Like

One example is from bioinformatics, you might want to join DNA sequences together. DNA is represented by the letters A, C, G, and T. The sequences could be thousands or tens of thousands of letters long. One would copy a sequence and paste it into the console.log( ) command and then copy and paste the next sequences, and so on.

I once had to add a file of 14,000 letters to a file of 2.6 billion letters. I used the cat command in Linux, though

What happens if you use console.log( “any word” - “any word”) (instead of using +)?

Did you try it? - is not a recognized string operator. JS will treat, or attempt to treat the two strings as numbers and perform subtraction, giving NaN as a result, I would imagine.

1 Like

Ohh, that makes sense, thank you. Yes, it does give NaN as a result, thank you

1 Like

@mtf Is there any difference in performance between using concatenation or string literals? Curious about if there is some trade off or sring interpolation are just a modernized version for easier use.

The difference may be negligible, generally speaking. What interpolation gives us is the ability to express, not just poll, meaning fewer variables.

Strings are potentially quite expensive to work with, at the low level. Concatenation means an operation to combine two or more objects into a single string representation. While the objects themselves might be expressions, it is far more awkward to concatenate than to interpolate. And, the bonus is we’re only ever working with a single string. The expressions themselves can even contain strings that contain expressions. Nesting of this type may not be overly common, but it is doable with relative ease. Give me backticks over plus signs and a waft of quotes, any day.

2 Likes