I'm truly a newbie at any of this. Anyway, I'm trying to put the results of this simple JavaScript into a txt file
Example script:
<script>
var numOne=25, numTwo=14,res;
res = numOne - numTwo;
document.write(" " + res + "");
</script>
I want to create a txt file and put the results in it, instead of showing the results
I tried to change the document.write but i have no idea what i was doing
In general Javascript deliberately does not make it easy to create a file on someone's computer because that's a security risk. Javascript generally doesn't have access to your computer's file system. (Note that this only applies to Javascript running in a browser. If you run it on someone's computer using something like Node.js, that is a very different thing and creating files is just as easy as with any other programming language).
However, you can create a file object and make the browser download that file.
const result = 3 + 4
const link = document.createElement("a");
const file = new Blob([result], {
type: 'text/plain'
});
link.href = URL.createObjectURL(file);
link.download = "result.txt";
link.click();
URL.revokeObjectURL(link.href);
(And your browser will probably prevent the download from happening if you click the "Run code snippet" button, again for security reasons)
So another way to make it run is to press ctrl-shift-i, which reveals the developer tools console. You can copy and paste this code there, and that should trigger a download of the result file. However, also remember to never copy-paste code you don't understand in your developer console. Again: for security reasons.