Contact Us

Essentials of Node.js

Part-2 Node.js Terminal

Invoke the Terminal
Node.js comes with an interactive REPL [ Read-Evaluate-Print-Loop ] shell, It takes user commands as input and prints the output on standard output or console after evaluating the inputs. In this tutorial, we'll be looking at basic node.js commands which we can execute right on this terminal. To invoke the node.js terminal simply type 'node' on your windows command prompt or Linux shell.

$ node
In the following sections we'll see some of the basic javascript or node.js operations which we can perform using the node.js terminal.
Print 'Hello World'
Following the tradition, let's start by learning how we print something to the console when using Node.js. console.log() is the function used for same as shown below.

$ node
> console.log('Hello World');
Hello World

Note that 'undefined' is return value of console.log(). That means on being evaluated, it returns nothing therefore 'undefined' is printed on the screen.

Elementary Mathematics
We can perform simple mathematical operations right on the terminal making it useful as a calculator. While reading this tutorial, it is highly recommended that you try it actually on your node.js terminal.

$ node
> 3 + 4
> 10 - 2
> 9 * 5
> 10 / 2
> 20 % 3
> 30 / 0
> -30 / 0
> 'a' / 2
Note that Infinity , -Infinity and NaN [not a number] are 3 symbolic representations of a number in node.js
Declaring Variables
As Node.js is nothing but "Javascript out of browser world" therefore it is dynamic in nature. It means that we need not define the type of variable. We can simply assign values to variables and their type will be determined automatically. We'll use typeof operator to check type of variables.

$ node
> var x = 10
> x
> typeof x
> var y = 12.12
> typeof y
> var z = 'i_am_not_a_number'
> typeof z
> var ar = [1,2,3,4,5]
> typeof ar
> var ob = {"key1": "value1", "key2": "value2"}
> typeof ob
> ob = null
> typeof ob
> ob
> typeof true

As shown above, node.js data types include number, string, boolean, object. Note that null means that an object is empty however the data type of null is still object.
If we try to check the type of a variable which has not been declared, then it returns 'undefined'

$ node
> typeof anything
Date and Time

$ node
> var date = new Date();
> date
Wed Jul 27 2016 12:28:18 GMT+0530 (India Standard Time)
> typeof date
> var day = date.getDay()
> day
> var hours = date.getHours()
> hours
> date.setHours(2)
> date
Wed Jul 27 2016 02:28:18 GMT+0530 (India Standard Time)
> date.toJSON()
> date.getTime()

We declared a date object of type Date which is built in Node. Once we have the date object we can apply different GET & SET operations on it as shown. Some of these methods prove to be very useful as we'll see in upcoming tutorials.
Know your Process
Last but not of course the least, we can use Node.js terminal to display a lot of useful low level information about the currently running process and the environment in which the process is running. process object is available globally to Node, so we need not initialize it, we use it directly as shown below. Note that the part after "//" is a comment written to help you understand particular input or output, it is not the part of input of output itself.

$ node
> var pid = process.pid    // declare a variable and store process id in it
> pid 
4780 					  // process id of the current process
> process.arch            // underlying architecture 32 or 64 bit
> process.env.USERNAME    // username of the user running the process 
> process.argv[0]           // first element of array of variables passed while invoking the terminal
> process.cwd()           // function to print the current working directory

In this way, we can actually gain a lot of useful insights about process .
In this tutorial, we learned about how we can use Node.js Terminal to perform many useful programming operations. In the next tutorial we'll learn about how to write the source code in a file and execute it rather than using a terminal.

developed & nourished with by rishabh.io