Home > Back-end >  Trying to run hello world JavaScript program on visual studio but keep getting 'document'
Trying to run hello world JavaScript program on visual studio but keep getting 'document'

Time:01-31

Error that I am getting and my simple code

I've watched videos and when people run this it works just fine. I also tried console.log and I get-- 'console' undefined. I have node.js installed

CodePudding user response:

document is not a built-in part of JavaScript. It is part of the Web API provided by browsers which you get when you load your JS by using a <script> element in an HTML document in a web browser. You aren't doing that, and document doesn't have a log method anyway.

You're running your JS using Windows Script Host, which isn't that. WSH does not have a document.

You probably want either:

  • the WriteLine method
  • to not use WSH (Browsers and Node.js being more common ways to run JS)

CodePudding user response:

You aren't running in a browser, there is no document (plus, it'd be console.log there too). You are running in Windows Script Host, you can use WScript.Echo instead. But I'd rather recommend you to run your code in a browser or node.js anyway, WSH isn't a very useful environment for learning modern JavaScript.

You wrote you have node.js installed, so the issue could be that you simply used the wrong command: it should be node and not start!

  •  Tags:  
  • Related