1.10 Input and Output

We often have a need to interact with users, either to get data or to provide some sort of result. Most of the time we use an HTML form as a way of asking the user to provide some type of input. In our case, we will avoid this and use much simpler functions.

1.10.1 Client side

JavaScript provides us with a function that allows us to ask a user to enter some data and returns a reference to the data in the form of a string. The function is called prompt().

A prompt dialog contains a single-line textbox, a Cancel button, and an OK button, and returns the (possibly empty) text the user entered into that textbox.

var fruit = prompt("What's your favorite fruit?");

if (fruit.toLowerCase() == "apple") {
    alert("Wow! I love apples too!");
}

1.10.2 Server side (Node.js)

On the server, we need to use the readline module. It provides an interface for reading data from a Readable stream (such as process.stdin) one line at a time.

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.question("What's your favorite fruit? ", (answer) => {

  if (answer.toLowerCase() == "apple") {
    console.log("Wow! I love apples too!");
  }

  rl.close();
});

Note Once this code is invoked, the Node.js application will not terminate until the readline interface is closed because the interface waits for data to be received on the input stream.

1.10.3 String Formatting

As you may know, console.log() function provides a very simple way to output values from a JavaScript program.

This feature is non-standard so don't use it in production.

console.log takes zero or more parameters and displays them using a single blank as separator. In addition, each print ends with a newline character by default.

console.log('Hello'); // Hello
console.log('Hello','World'); // Hello World

It is often useful to have more control over the look of your output. Fortunately, Python provides us with an alternative called formatted strings. A formatted string is a template in which words or spaces that will remain constant are combined with placeholders for variables that will be inserted into the string. For example, the statement

var name = 'John';
var age = 42;
console.log(name, 'is', age, 'years old.'); // John is 42 years old.

contains the words is and years old, but the name and the age will change depending on the variable values at the time of execution. Using a formatted string, we write the previous statement as

var name = 'John';
var age = 42;
console.log('%s is %d years old.', name, age);
// John is 42 years old.

This simple example illustrates a new string expression. The % operator is a string operator called the format operator.

Character Output Format
%s String
%d, %i Integer (numeric formatting is not yet supported)
%f, %.xf Floating point number; x denotes the number of decimal places the number should be rounded to (if omitted, the number won't be rounded)
%o Object hyperlink
%c Style formatting

results matching ""

    No results matching ""