How to center text in JavaScript?

To center text in JavaScript, do this: element.style.textAlign = 'center'.

For example, if this is your div with some text in it:

<div id="my-div">Hello World!</div>

Then you can center the text with this JavaScript:

document.getElementById('my-div').style.textAlign = 'center';