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.
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.
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`);
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.
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.
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
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.
@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.