JavaScript Question

Digital Pro

Junior Member
Joined
Dec 1, 2019
Messages
140
Reaction score
76
I have been studying up on JS and I have a question regarding String Concatenation.

My question is this:
Has anyone found any practical or applicable situations where String Concatenation was beneficial for any logical reason? If so, what would be the application? I can see many other ways to write the code without using it. I was just wondering if I am missing something that could make this operation relevant?

I can see it being used in Long Stream, but wouldn't a (/) serve the same purpose and be a lot cleaner.

Any input would be greatly appreciated.
 
Last edited:
Has anyone found any practical or applicable situations where String Concatenation was beneficial for any logical reason? If so, what would be the application?


Code:
var1 = new String( "I'm not sure I understand your question properly. ");
var2 = new String( "This code joins/concatenates two different strings into one and there are so many ways of accomplishing the same thing, everyone has differing opinions on how to do it appropriately. Choose whatever you find easier to merge two or multiple strings into one.");
var3 = var1.concat(var2);
document.write("Result: " + var3);

Paste this code in your browser's console and press enter.
 
So it's basically just a preference of style?
 
Let's say you want to take input from an user make a request to a server and the format is:
Code:
name={NAME}&id={ID}&age={AGE}
You first ask the user for the details:
Code:
name = input("Name = ")
ID = input("ID = ")
age = input("Age = ")
And then you need to send it using the format but doing that will mean you'd have to do string concatenation:
Code:
payload = "name=" + name + "&id=" + ID + "&age=" + age

** I know this is not JS code but I hope it gets the point across **
 
I don't get the point here. What other alternative are you suggesting?

Let's say..

Code:
var a = "hello ";
var b = prompt("what's your name?");

alert( a + b);

What other style do you suggest?

Also yeah, generally it's about preference more than anything else.
 
So it's basically just a preference of style?

Absolutely and when you start working with multiple and different types of data, you'll start to realize why there are several other ways to output the same data.
 
Code:
var1 = new String( "I'm not sure I understand your question properly. ");
var2 = new String( "This code joins/concatenates two different strings into one and there are so many ways of accomplishing the same thing, everyone has differing opinions on how to do it appropriately. Choose whatever you find easier to merge two or multiple strings into one.");
var3 = var1.concat(var2);
document.write("Result: " + var3);

Paste this code in your browser's console and press enter.
lol, this!

OP - these things are basics, every task can be done by more than 1 ways... it's just preference at the end. Just KISS :D
 
For rest communication with API :
code said:
var mainUrl = "http://blackhatworld.com";
requestPost(mainUrl + "/post/" + post.category + post.id) // post.category == /seo/ post.id == /javascript-question.1198267/
 
Back
Top