简体   繁体   中英

How to insert an intro in console.log() in Javascript

I would like to print some text using console.log(). I have several lines, a for each one I have used a console.log(). I would like to create 2 paragraphs. How could I do it? Thanks

  console.log("1 : Lister les contacts"); //paragraph 1 console.log("2 : Ajouter un contact"); //paragraph 1 console.log("0 : Quitter"); //paragraph 1 console.log("Choisissez une option :")); //paragraph 2 

 console.log("1 : Lister les contacts"); //paragraph 1 console.log("2 : Ajouter un contact"); //paragraph 1 console.log("0 : Quitter"); //paragraph 1 console.log("\\n\\nChoisissez une option :"); //paragraph 2​ 

 console.log("1 : Lister les contacts"); //paragraph 1 console.log("2 : Ajouter un contact"); //paragraph 1 console.log("0 : Quitter"); //paragraph 1 console.log(""); //paragraph 1 console.log("Choisissez une option :"); //paragraph 2 

Or you can use \\n to break the line and use two time \\n\\n to break two line:

 console.log(" p1 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. \\n\\n p2 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. "); 

\\n is a new line, and \\t is a tab (indent). Also, you can "concatenate" strings (basically adding them together.

var paragraph1 = "1 : Lister les contacts\n";
paragraph1 = paragraph1 + "2 : Ajouter un contact\n";
paragraph1 = paragraph1 + "0 : Quitter\n");
var paragraph2 = "Choisissez une option :";

console.log(paragraph1 + "\n" + paragraph2);

This will result in what you showed with no indents, and a line between the paragraphs. Note: separate console.log() calls are always on different lines, so keeping them on separate console logs you wouldn't need that \\n at the end.

Also, you could add a \\t to the beginnnig of each line in the list to help separate it a bit.

You can use \\r\\n in between your lines to add a new line so that one call of console.log prints the first 3 lines.

 //paragraph 1 console.log("1 : Lister les contacts\\r\\n2 : Ajouter un contact\\r\\n0 : Quitter"); //paragraph 2 console.log("Choisissez une option :"); //paragraph 2 

You also had an extra ) on the last console.log() .

You can use template literals for multiline String. A template literal is delimited by backticks:

 console.log(`1 : Lister les contacts 2 : Ajouter un contact 0 : Quitter `); //paragraph 1 console.log('Choisissez une option :'); //paragraph 2 

I am a French speaker and think you want something like this. You just don't know how to explain it.

 <form role="form" class="form-horizontal"> <fieldset> <div class="form-group"> <label class="label">Choisissez une option :</label> <div class="select"> <select class="myoptions"> <option value="1">1 : Lister les contacts</option> <option value="2">2 : Ajouter un contact</option> <option value="3">0 : Quitter</option> </select> </div> </div> </fieldset> </form> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM