I have a question about loading the contents of a text file into a JavaScript variable. I want to load the contents of the file /robots.txt into a variable.


<script>
var fileContents;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
fileContents = this.responseText;
console.log(fileContents)
}
};
xhttp.open("GET", "/robots.txt", true);
xhttp.send();
</script>

This code seems to work, but I'm not entirely sure about its efficiency and if there's a better way to achieve this. Can someone provide an alternative solution or suggest improvements?