3. Let's write some code

Let’s write some code

  • Install Node.js, it includes ‘npm‘ which allow to manage and install packages for js project.

  • After installation verify that node js and npm are installed correctly by running the following command on your terminal :-

    “node -v“

    “npm -v“

  • To write code in terminal we use “Node REPL(read, evaluate, print, loop)“ how to start just type node in terminal it will start REPL in terminal

    REPL is an interactive environment where we can write and execute js code directly in terminal. It allow to quickly test and experiment with js without a need to type code in file format. This is all running in node js runtime environment.

  • When we use the Node REPL, we are interacting with the Node.js runtime environment which is responsible for running js code outside of any browser.

  • Node js is a js runtime environment and behind the scene, it used js engine.

  • V8 engine developed by google is the same engine that powers the chrome browser. It takes js code and compiles into machine code which our computer can execute. Node js leverage this powerful engine to run js on the server side.

  • Just like the console in our browser the “Node REPL“ allow us to write and test js code interactively. However instead of running in the browser, it runs on our system terminal, allowing us to interact with the underlying operating system and perform task like file handling, server management and more.

  • However writing code like this for long period of time can be frustrating, while “REPL“ is great for quick experiments but it is not ideal for larger projects or complex code. The lack of features like syntax highlighting, debugging tools and project organization makes REPL cumbersome for extended use”.

  • Now lets write some basic js code inside “app.js“ file

      let name = "faraz alam";
      let a = 5;
      console.log(name);
      console.log(a);
      // how to run, just type "node file_name" in teminal to run
    

Global object

These are the objects that are available in all scopes. In Node js these objects provide core functionalities without needing to require them explicitly in our code. The way “window“ global object is for browser in the same way for node it is “global“. “global“ object in node js provide access to globally available functionalities such as setTimeout(), setInterval(), console() etc.

  • how to see these global object is that in node REPL simply write “global“ as soon as you press enter you will able to see different objects

    or in app.js “console.log(global)“.

Node Js “global“ object Vs Browser “window” object

In web browser the global object is called window, this object is automatically created by the browser environment and provide access to various browser specific feature like DOM as well as method like “alert()“, “setTimeout()“, “setInterval()“. It is important to note that “window” is not part of the V8 engine itself but is provided by the browser environment.

“global“ object are not the part of V8 engine itself they are provided by node js, which is built on the top of V8 engine by adding these global object, allowing it to perform task like file handling, interacting with the OS and handling asynchronous events.

globalThis

It is always the global object regardless of where it is accessed. It was introduced in ECMAScript 2020 to provide a standardized way to refer to the global object in any environment (browser, Node.js etc)

  • In browser “globalThis“ is equivalent to “window“.

  • In Node js “globalThis“ is equivalent to “global“

  • It provide a consistent way to access the global object without worrying about the environment.