Hi new learner here, why doesnt javascript code from sololearn work elsewhere? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Hi new learner here, why doesnt javascript code from sololearn work elsewhere?

im trying out the Javascript code in repl.it and other sites and they not working. THis makes me worry that im learning JS wrong. Why is this happening? what am i missing? all saying thing like document not defined for Example the for loops code on other sites give error: "for (i=1; i<=5; i++) { document.write(i + "<br />"); } ReferenceError: document is not defined at Object.<anonymous> (/home/runner/UtterLoyalAutotote/index.js:2:5) at Module._compile (node:internal/modules/cjs/loader:1105:14) And another example else if statement : var course = 1; if (course == 1) { document.write("<h1>HTML Tutorial</h1>"); } else if (course == 2) { document.write("<h1>CSS Tutorial</h1>"); } else { document.write("<h1>JavaScript Tutorial</h1>"); } ReferenceError: document is not defined at Object.<anonymous> (/home/runner/UtterLoyalAutotote/index.js:3:5) at Module._compile (node:internal/modules/cjs/loader:1105:14)

11th Nov 2022, 6:31 AM
Pravesh Maharaj
Pravesh Maharaj - avatar
2 Answers
+ 5
JavaScript has two distinct uses: - running in the web browser (client side) - running in Node.js (server side) The "document" object refers to the web browser's context of the current webpage that is opened. If you try to modify the DOM (document object model) from Node, it won't work because there is no webpage in this context. In Node.js use console.log() to output data to the console.
11th Nov 2022, 8:02 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Thank you very much tibor
11th Nov 2022, 4:54 PM
Pravesh Maharaj
Pravesh Maharaj - avatar