Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Put hashtags in a string in javascript

my problem is quite simple, I would like to put hashtags in a text from a list of them.
I have tried every way but I can’t find the solution.
Thanks in advance.

let arr = ['aa', 'bb']

let caption = `Hello | World `;

arr.forEach(el => {
  console.log(`#${el}`)
  caption.concat(' ', `#${el}`)
});

console.log(caption) // I want : "Hello | World #aa #bb" ; But it puts me "Hello | World"

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

concat does not modify the original string. You have to assign the returned value of concat back to caption.

let arr = ['aa', 'bb']

let caption = `Hello | World `;

arr.forEach(el => {
  console.log(`#${el}`)
  caption = caption.concat(' ', `#${el}`)
});

console.log(caption) // prints : "Hello | World #aa #bb"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading