Surely there’s a better way of doing this? It’s not very elegant. I’m simply wanting to make the first word bolder.
let title_array = title.split(" ");
title_array[0] = "<strong>"+title_array[0]+"</strong>";
title = title_array.join(" ");
>Solution :
Your approach is good, below is another approach using Regex
var title = "Hello to new StackOverflow";
title = title.replace(/^(\w+)/, '<strong>$1</strong>');
console.log(title);
var title = "Hello to new StackOverflow";
title = title.replace(/^(\w+)/, '<strong>$1</strong>');
console.log(title);
Since you are using React, you may have to use dangerouslySetInnerHTML to render the html.
So another approach would be something like this in React.
<div>
{
title.split(" ").map((word, index) => {
return index === 0 ? <strong>{word}</strong> : ` ${word}`;
});
}
</div>