What is readline module clearScreenDown() in Node.js?

In NodeJS, one can use the readline.clearScreenDown() method to clear the screen based on the position of the cursor. This method clears the screen below the current position of the cursor.

A clear screen
A clear screen

Syntax

readline.clearScreenDown(stream[, callback])

Parameters and return value

Parameter

  • stream: It refers to <stream.writable>. Mostly process.stdout is passed as the parameter because we want to clear the output screen.

  • callback: When the operation completes, readline.clearScreenDown invokes a callback function.

Return value

  • This method returns a boolean value true if the screen is cleared and write object becomes clear. Returns false otherwise.

Code

Let’s have a look at the implementation of this method.

You can change the cursor position using readline.movecursor() method.

// require readline
const readline = require("readline");
// create an interface
const rl = readline.createInterface(process.stdin, process.stdout);
// run a question. On Calling clearScreenDown() method
// the terminal clears the screen andremoves this question, and only
// prints the answer.
rl.question("What is your Age? ", function(a) {
readline.moveCursor(process.stdout,0,-2);
readline.clearScreenDown(process.stdout);
console.log("Hey, your Age is", a);
rl.close();
});

Application

One of the applications of clearScreenDown() is to clear the entire screen by moving the cursor to first position (index 0).

const clearScreen = () => {
// values is the number of lines that cursor needs to move. 
 readline.cursorTo(process.stdout, 0, values)
 readline.clearScreenDown(process.stdout)
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved