The Node.js REPL (Read-Eval-Print-Loop) is a simple module which can be accessible for both as a standalone program or can be included in different programing applications. JavaScript code can be executed using this prompt. REPL command prompt can be accessed if a Node.js binary is initialized without passing any argument. Following are few methods of built-in console object for printing the stderr and stdout.
- console.info([data][, …args])
- console.trace(message[, …args])
- console.time(lable)
- console.timeEnd(label)
- console.log([data][, …arg])
- console.error([data][, …arg])
- console.warn([data][, …arg])
- console.assert(expression, [message])
- console.dir(obj[, options])
console.info([data][, ...args]) -
Multiple arguments can be given and the function is similar to the console.log. The image below gives few examples of the function
console.info()
.
console.trace(message[, …args]) -
Using the function
console.trace()
stack trace is printed to stderr for the current node.js position, the image below gives the example for
console.trace()
function.
console.time(label) -
The function
console.time()
is utilized to track the time for the process and each time must and should have different names but the time taken for a particular process can be known by using the same name with the help of the command
console.timeEnd()
.
console.timeEnd(label) -
Using the function
console.timeEnd()
is utilized to end the timer for the process initialized by
console.time()
function. The example below demonstrate the use of
console.timeEnd()
function.
timeEnd.js
[c]
console.time('Process timer');
var sum = 0;
for (var x=0; x<200000; x++)
{
sum += x;
}
console.log('Time taken Sum up numbers from 1 to 200000');
console.timeEnd('Process timer');
[/c]
Compiling the above code will produce the output as shown in the image below.
console.log([data][, …arg]) -
The function
console.log()
is utilized to print with newline to stdout, the image below gives the example for utilizing the
console.log()
function.
The same example can be executed by creating .js file with the code.
log.js
[c]console.log(“SPLessons Online Tutorials”)[/c]
By compiling the code, one can get the output as shown in the image below.
console.error([data][, …arg]) -
The function
console.error()
method is similar to
console.log()
but the output here sent to stderr instead of stdout. This function is utilized for error messages and more use of this method can slow down the process. Multiple arguments can be passed and the first argument is used as the first message and the rest used as additional values. The snippet code below demonstrates the
console.error()
function. Create a .js file with the following code.
err.js
[c]
const code = 10;
console.error('error', code);
console.error('error #%d', code);
[/c]
By compiling the code, one can get the output as shown in the image below.
console.warn([data][, …arg]) -
The function
console.warn()
is similar to
console.error()
, the example below demonstrates the code for warn function which print a message when a specific file opened and print a warning message if the specified file not found.
[c]
var fd = require("fs");
var path = "text1.txt";
fd.open(path, "r", function(err, fd){
if (err){
console.warn(err);
}
else{
console.log("File open successfully" + path);
}
})
[/c]
By compiling the code, one can get the output as shown in the image below.
console.assert(expression, [message]) -
The function
console.assert()
is utilized to check the expression wither true or false, the image below demonstrates an example for
console.assert()
function.
console.dir(obj[, options]) -
The function
console.dir(obj[,option])
utilize the util.inspect on obj and then prints the string to stdout.