How to know when dealing with code meant for JavaScript or NodeJS?

Hello, I am learning JavaScript/NodeJS. When reading JS code, I am struggling to identify if the code is meant strictly for JavaScript environment (Web browser Dev Tool console) or NodeJS use or for both even. Are there ways of knowing this?

For example reading this page, how to execute shell commands in js, In that context, I am not quite sure what the difference between JS and Node is.

I am trying to program a simple functionality on using a index.html local page loaded into my web browser. The program purpose to run CMD/PowerShell upon clicking the Firefox image.

My script files (C:\temp\sampl-site\scripts\main.js) content is:

// Image switcher code

let myImage = document.querySelector('img');

myImage.onclick = function() {
	//alert('hello world1')
	//code for CMD here

I am using this MDN JavaScript basics index page as my local page, and the source code I am using can be found HERE.

Any help would be greatly appreciated!

After building and uploading, pushing an API to the web I can tell you that back end JS is really called server.js and its not the same although it is a bit similar. Here is an article they may help you with your question. Good luck

What is Node.js? Server-Side JavaScript Development Basics.

1 Like

Web browsers have a scripting API which allows you to write small programs (scripts) to manipulate certain aspects of browser functionality. The programming language provided for this API is JavaScript. Browsers understand the JavaScript language, allowing these scripts to be executed.

The scripting API consists of a load of functions related to things you would do in a browser, eg find an HTML element or listen for a button click or add some CSS styling.

Yes: are you doing something that only makes sense inside a browser?

So there are two things there that are built in to a browser:

  • the program that reads and executes JavaScript
  • the JavaScript API the browser provides; this is a library

Node is the part of the Chromium browser that reads and executes JavaScript extracted from the browser into part of a different, standalone program

Node doesn’t have anything to do with browsers (anything related to browsers makes no sense if you want to write programs that have nothing to do with browsers).

What it does instead is provide an API for operating system functionality. So stuff like dealing with files or handling network connections.

Browsers do not have access to your local filesystem, for extremely sensible security reasons. So you can’t really do that.

1 Like

This is amazing, I have come to understand much after reading this and a few days later. Thanks allot for this.

1 Like