r/JavaScriptTips • • Aug 01 '22

how do I use javascript by itself?

Hi, I know that I can type javascript code inside html using script tag and I can use node.js to run javascript independently. But I wanna know whether there is any other way to code in javascript without needing any other language. Also, is node.js another language? I googled it, but couldn’t quite understand what it does. I am a beginner, so please try to explain in simple words since I don't understand much of the terms

10 Upvotes

3 comments sorted by

7

u/arturo_ui_ Aug 02 '22

Nodejs is not a programming language. It is a JS RUNTIME ENVIRONMENT

Runtime environment basically defines where your javascript code will be executed, and which global objects your program can reach. There are 2 main js runtime environment:

  1. Browser: we use browser environment when we write code inside script tag with html. As we said before: runtime environment defines global objects. In here for e.g window object is defined. You can use window.alert() which is an built-in global function in this environment

  2. Node environment: It is an environment created for executing javascript without browser. This environment also defines its own global objects. For e.g when you try to execute same window.alert() code here: error will be occured. Node doesnt provide this object.

1

u/kongker81 Aug 29 '22 edited Aug 29 '22

One of the biggest conundrums around Javascript for beginners is learning the difference between Node.JS and traditional Javascript. I had a hard time myself before I decided to create a full product in Node. Here is the best way to explain the differences:

Node.JS is equivalent to PHP

If you are a PHP developer, then you know PHP is a specialized language to write serverside web applications. I say serverside, because the code is interpreted on a server, waits for the request, then serves the webpage. Now if you want realtime interaction on the webpage, PHP is done and you hand it off to Javascript which is embedded in your webpage. You can use Ajax to communicate from Javascript to the serverside code in this case.

Node.JS vs Client Side Javascript

Now we get to Node.JS which I said is equivalent to PHP. You can write serverside applications just as you could with PHP. As far as I am concerned, Node.JS Javascript is quite different from client side Javascript. Same syntax, but totally different mindsets. Node.JS cannot manipulate the dom, and cannot interact with anything on the browser just like PHP cannot. And just like with PHP, you STILL need to use client side Javascript if you want browser interaction. And you STILL need to use Ajax, to communicate from the browser to the server. Except, instead of PHP being the serverside code, now its Javascript (on a Node.JS server) running the serverside code.

The above becomes more clear if you ever decide to create a Node.JS application. Why use Node.JS if you have PHP? It all comes down to preference. I believe Node.JS may be a little faster, but in reality, it all boils down to how skilled the developer is.

0

u/[deleted] Aug 01 '22

[deleted]

1

u/[deleted] Aug 01 '22

Thanks a lot