Beginner: Use of basic console module in NodeJS

Beginner: Use of basic console module in NodeJS

ยท

2 min read

Hello learners and welcome to my first blog of the year. In this blog, we are going to learn how to use the most common console module to display output on the command line in NodeJs.

NodeJs is an open-source runtime environment for Javascript. Node makes it possible for developers to use Javascript for both server-side and mobile applications.

For this tutorial, you would need the following:

  • A code editor (I would be using vscode)

  • Node installed (currently using v19.3.0)

    Install node

The Command line

A command line or preferably a command-line interface (CLI) is a text-based editing environment, used to run programs, interact, and perform operations on the computer.

This environment is used to execute and test Javascript codes

To run a NodeJS code on the command line, simply type the node command (this only runs if you have nodeJs installed) and the name of the file you wish to execute.

For example; if the name of your file is script.js, you can call it by typing

The console

To interact with the command line, Node provides a core module known as the console module. This console is similar to the console provided by the web browser.

syntax:
console.log(" ")

The console module has a lot of methods but the most used method is console.log().

This method is used to print whatever message is passed into it, on the console.

console.log() can take in multiple parameters that can be an object, array or any message and NodeJS will print them all.

When passing an argument in a function:

output when passing a function

When passing multiple variables:

output when passing multiple variables

when passing an array of strings:

Conclusion

In conclusion, we learned the basic use of a command line in NodeJS and how to run a file in its environment.

Additionally, we also talked about the console module and using the.log() method to output data on the command line in NodeJS.

If this is helpful, leave a like ๐Ÿ‘ and drop a comment ๐Ÿ˜Ž

References

ย