Writing Your First Node.js Script

Writing Your First Node.js Script

·

3 min read

Table of contents

No heading

No headings in the article.

Hi, I'm Subham Maity, a software engineer. I also enjoy teaching others how to code through my tutorials. I'm always eager to learn new things and share my knowledge with the community.

⚡ I recently wrote an article on Writing Your First Node.js Script and wanted to share it with you all. You can find the article on my website https://codexam.vercel.app/docs/node/node1 [Better View]

⚡ I also have a repository on GitHub where you can find all the code and projects related to this topic. You can find the repository at https://github.com/Subham-Maity/node-js-full-stack-tutorial

❗ For a more in-depth look at this topic, including a detailed table of contents, check out the complete tutorial on my GitHub Repo

If you want to stay updated with my latest projects and articles, you can follow me on:

  1. If NodeJS is installed, you can check the version by running the following command in your terminal:
node -v
  1. Type node in your terminal to start the NodeJS REPL (Read-Eval-Print-Loop) environment.

  2. Type console.log("Hello World") in the REPL and press enter to print "Hello World" in the terminal.

  3. Let's do some operation check it out.

var a = 10; //enter
console.log(a); //output: 10
  1. You can notice undefined in the output.

This is because the expression does not return any value. It just assigns the value to the variable when you do console.log(a), it prints the value of the variable a which is 10.

  1. You can also do some mathematical operation in the REPL.
var a = 10; //enter
var b = 20; //enter
console.log(a+b); //output: 30
  1. Create a folder named nodeJs and open cmd in that folder and type code . to open the folder in VS Code.

  2. Make a file named index.js and type the following code in it.

NodeJs is not a language, it is a runtime environment. It is used to run JavaScript code outside the browser. It is built on top of the V8 JavaScript engine. It is used to build server-side applications.

console.log("Hello World");
  1. Now open the terminal in VS Code and type node index.js to run the code.

In vs code terminal if you compile by node index.js then you will the output of console.log() in the terminal and in browser you will get the output of console.log() in the browser console. Now the question is here the console.log is same in two place ?

  • Node.js has a built-in console module that provides a simple debugging console similar to the JavaScript console mechanism provided by web browsers. The console module exports two specific components: a Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream; and a global console instance configured to write to process.stdout and process.stderr.

The implementation of the console object is similar in both Node.js and web browsers, which is why you can use methods like console.log() in the same way in both environments.