Concatenation question in js Web practice | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Concatenation question in js Web practice

Is there any reason to concatenate like ' console.log("Thank you for" + variable);' and not simply ' console.log("Thank you for ", variable);' It seems to work alike and I'm kinda reluctant to concatenate strings and integers.

12th Apr 2024, 5:27 PM
Roland
4 Answers
+ 4
It depends on how much control you need. Compare the blank spaces in my example: https://sololearn.com/compiler-playground/cEQrq5sR3SHD/?ref=app
12th Apr 2024, 5:35 PM
Lisa
Lisa - avatar
+ 3
No real reason to use either of those examples anymore besides for learning purposes.The second example is said to be cleaner, but that is also outdated. Template literals are the modern way to do that. Lisa provided an example on the last line in the code she shared.
13th Apr 2024, 12:28 AM
Chris Coder
Chris Coder - avatar
+ 2
("Thank you," + " Lisa"); I was aware of this single extra space, but not sure if there are other implications (like unintended line breaks in some conditions). Thank you for your quick response.
12th Apr 2024, 5:42 PM
Roland
+ 1
Thank you for mentioning it Chris. I've seen it before, but never used it in js. I was not aware that it's an apostrophe [`] and not a single quotation mark ['].
13th Apr 2024, 9:39 AM
Roland