<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mat Wilmot on Medium]]></title>
        <description><![CDATA[Stories by Mat Wilmot on Medium]]></description>
        <link>https://medium.com/@matwilmot90?source=rss-dbef98fece58------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*N6QBrqTmwFF10vmm1G8HLQ.jpeg</url>
            <title>Stories by Mat Wilmot on Medium</title>
            <link>https://medium.com/@matwilmot90?source=rss-dbef98fece58------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 21:54:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@matwilmot90/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[JavaScript Basics — Objects & Object Manipulation]]></title>
            <link>https://javascript.plainenglish.io/javascript-basics-objects-object-manipulation-82fc9d39db06?source=rss-dbef98fece58------2</link>
            <guid isPermaLink="false">https://medium.com/p/82fc9d39db06</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[object-oriented]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[oop]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Mat Wilmot]]></dc:creator>
            <pubDate>Tue, 08 Sep 2020 04:58:12 GMT</pubDate>
            <atom:updated>2020-09-14T08:53:20.221Z</atom:updated>
            <content:encoded><![CDATA[<h3>JavaScript Basics — Objects &amp; Object Manipulation</h3><p>You’ve probably heard the term ‘Object Oriented Programming’ or ‘OOP’ among developers or on the internet, this article serves as an introduction to the concept of objects and how we can use them in our code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*22wViW13m0Nu2ZmO.jpg" /></figure><h3>What is an object?</h3><p>Put simply, an object is a collection of data sorted into key/value pairs. To keep a running example in this article, let’s consider your kitchen to be an object. Inside your kitchen, you have a stove, an oven, a refrigerator, a sink, and some cupboards — and maybe some kitchens have a toaster oven, and others don’t. Each of these things are the keys in the key/value pairs I was talking about. The values would be whatever data represents those keys. Written in JavaScript, our kitchen object might look like this…</p><figure><img alt="example of an object written in JavaScript" src="https://cdn-images-1.medium.com/max/391/1*uEnGR6uvYd-aB0LH9cHwtw.jpeg" /></figure><p>So we used curly braces to define an object, then separated key/value pairs with commas.</p><h3>Object Datatypes</h3><p>You’re not limited to strings, integers, and booleans for your values. You can also use arrays, more objects, and heck, you could even use an array of objects.</p><p>Consider the fact that a stove has burners, and can use a variety of heating methods — it’s starting to sound like an object itself, and we can definitely put an object inside of an object, this is how it would look…</p><pre>let kitchen = {<br>  stove: {<br>    brand: &quot;Kenmore&quot;,<br>    burners: 4,<br>    heatingMethod: &quot;induction&quot;,<br>  },<br>  oven: &quot;Kenmore&quot;,<br>  refrigerator: &quot;Whirlpool&quot;,<br>  sink: &quot;Sterling&quot;,<br>  cupboards: 6,<br>};</pre><p>Whoa, object-ception. All we had to do was add more curly braces and start writing another object, right there inside our kitchen object. We call this ‘nesting objects’, think like Russian dolls — one object inside another. Let’s add some more information to the oven, like how many racks it has, what its heating method is, and what’s being stored in that drawer underneath…</p><pre>let kitchen = {<br>  stove: {<br>    brand: &quot;Kenmore&quot;,<br>    burners: 4,<br>    heatingMethod: &quot;induction&quot;,<br>  },<br>  oven: {<br>    brand: &quot;Kenmore&quot;,<br>    racks: 2,<br>    heatingMethod: &quot;electric&quot;,<br>    drawer: [&quot;glass baking dish&quot;, &quot;baking tray&quot;, &quot;muffin pan&quot;],<br>  },<br>  refrigerator: &quot;Whirlpool&quot;,<br>  sink: &quot;Sterling&quot;,<br>  cupboards: 6,<br>};</pre><p>We were able to add an array as a value by just adding the square brackets and putting comma-separated strings. I imagine you’re seeing a pattern by now, but let’s think about how a refrigerator might look. I’ve very pointedly not used the term ‘fridge’, because a refrigerator (usually) has a cooling compartment, and a freezing compartment — the fridge and the freezer. The fridge usually has some drawers at the bottom, and a few shelves. Let’s see what a refrigerator broken up into two objects would look like…</p><pre>let kitchen = {<br>  stove: {<br>    brand: &quot;Kenmore&quot;,<br>    burners: 4,<br>    heatingMethod: &quot;induction&quot;,<br>  },<br>  oven: {<br>    brand: &quot;Kenmore&quot;,<br>    racks: 2,<br>    heatingMethod: &quot;electric&quot;,<br>    drawer: [&quot;glass baking dish&quot;, &quot;baking tray&quot;, &quot;muffin pan&quot;],<br>  },<br>  refrigerator: {<br>    brand: &quot;Whirlpool&quot;,<br>    fridge: {<br>      drawers: 2,<br>      shelves: 3,<br>      temp: 40,<br>    },<br>    freezer: {<br>      shelves: 1,<br>      temp: 0,<br>      iceMaker: false,<br>    },<br>  },<br>  sink: &quot;Sterling&quot;,<br>  cupboards: 6,<br>};</pre><p>Whoa, that was a lot! We turned the refrigerator into an object with three keys — fridge, freezer, and brand — and made fridge and freezer into objects, too. Next up is the sink, this should be an easy one to update. A sink has a faucet (the controls for the water flow), a spout, and either one or two sinks. Some also come with a disposal unit in the drain…</p><pre>let kitchen = {<br>  stove: {<br>    brand: &quot;Kenmore&quot;,<br>    burners: 4,<br>    heatingMethod: &quot;induction&quot;,<br>  },<br>  oven: {<br>    brand: &quot;Kenmore&quot;,<br>    racks: 2,<br>    heatingMethod: &quot;electric&quot;,<br>    drawer: [&quot;glass baking dish&quot;, &quot;baking tray&quot;, &quot;muffin pan&quot;],<br>  },<br>  refrigerator: {<br>    brand: &quot;Whirlpool&quot;,<br>    fridge: {<br>      drawers: 2,<br>      shelves: 3,<br>      temp: 40,<br>    },<br>    freezer: {<br>      shelves: 1,<br>      temp: 0,<br>      iceMaker: false,<br>    },<br>  },<br>  sink: {<br>    brand: &quot;Sterling&quot;,<br>    faucetType: &quot;mixer&quot;,<br>    spoutType: &quot;hose&quot;,<br>    sinks: 1,<br>    disposal: false,<br>  },<br>  cupboards: 6,<br>};</pre><p>Nothing particularly new there, but stay with me, because I saved the best for last. For the cupboards, we’re going to use an array of objects. Here’s how it’ll look…</p><pre>let kitchen = {<br>  stove: {<br>    brand: &quot;Kenmore&quot;,<br>    burners: 4,<br>    heatingMethod: &quot;induction&quot;,<br>  },<br>  oven: {<br>    brand: &quot;Kenmore&quot;,<br>    racks: 2,<br>    heatingMethod: &quot;electric&quot;,<br>    drawer: [&quot;glass baking dish&quot;, &quot;baking tray&quot;, &quot;muffin pan&quot;],<br>  },<br>  refrigerator: {<br>    brand: &quot;Whirlpool&quot;,<br>    fridge: {<br>      drawers: 2,<br>      shelves: 3,<br>      temp: 40,<br>    },<br>    freezer: {<br>      shelves: 1,<br>      temp: 0,<br>      iceMaker: false,<br>    },<br>  },<br>  sink: {<br>    brand: &quot;Sterling&quot;,<br>    faucetType: &quot;mixer&quot;,<br>    spoutType: &quot;hose&quot;,<br>    sinks: 1,<br>    disposal: false,<br>  },<br>  cupboards: [<br>    { location: &quot;below sink&quot;, contents: [] },<br>    { location: &quot;above stove&quot;, contents: [] },<br>    { location: &quot;left of stove&quot;, contents: [] },<br>    { location: &quot;right of stove&quot;, contents: [] },<br>    { location: &quot;below counter&quot;, contents: [] },<br>    { location: &quot;above counter&quot;, contents: [] },<br>  ],<br>};</pre><p>Phew, that’s a lot of code! But don’t let it overwhelm you, this is just taking basic principals of objects and adding them together in creative ways. You might have noticed that I left empty arrays in the ‘contents’ field for each of the cupboards. Partially, this was to keep the code shorter, but it also sets us up for some fun with methods later on.</p><p>So, now that we’ve built this object, you must be wondering..</p><h3>How do I access the information?</h3><p>Great question, the easiest way to access information in an object is through ‘dot notation’. The kitchen is the object, and you want to access the information regarding it. For the sake of simplicity, we’ll assume you just want to display this information in the console. Assuming your code contains the object we built above, you can type the following to see the entire object…</p><pre>console.log(kitchen)</pre><p>Not a shocker, huh? You just got everything you typed, but in the console. Let’s say you’re only interested in information about your refrigerator right now, you could return just the fridge by typing…</p><pre>console.log(kitchen.refrigerator)</pre><p>We used a period to access a tier of information within that object. This is dot notation — using a dot to move through your object. The above code should console log the entire object for your kitchen. Let’s take it deeper and see what we can find out about the fridge…</p><pre>console.log(kitchen.refrigerator.fridge)</pre><p>Now we just get the object containing the number of drawers, shelves, and the temperature. And if we only want to know how many drawers are in the fridge in our kitchen? You guessed it, we add another period…</p><pre>console.log(kitchen.refrigerator.fridge.drawers)</pre><p>Now, if you used the object example above, you should just get the number 2 in your console. Pretty simple, right? Just to make sure, let’s see what happens when we try to see what’s in the drawer under the oven…</p><pre>console.log(kitchen.oven.drawer)</pre><p>We get an array of strings, and now that we’re getting an array, we can definitely access this like an array, using square brackets to determine which index of the array we want to see…</p><pre>console.log(kitchen.oven.drawer[1])</pre><p>And now we can access the baking tray in the drawer. Pretty neat huh?</p><p>At this point you could start writing functions to push() to the kitchen.cupboards[2].contents, or to say that kitchen.sink.brand = &quot;Whirlpool&quot;, and that would be absolutely fine, but there is a better way…</p><h3>Object Methods</h3><p>I left one item off the list at the start of this article when I was telling you what data types an object could take — an object can also contain functions, which we call methods. These methods stay specific to the object in which they are created, clearing up your global scope. Let’s add a simple method to add an item to the drawer under the oven. For the sake of brevity, I’ll leave a lot of the object out of this snippet…</p><pre>let kitchen = {<br>  oven: {<br>    brand: &quot;Kenmore&quot;,<br>    racks: 2,<br>    heatingMethod: &quot;electric&quot;,<br>    drawer: [&quot;glass baking dish&quot;, &quot;baking tray&quot;, &quot;muffin pan&quot;],<br>  },<br>  addToDrawer: function (item) {<br>    this.oven.drawer.push(item);<br>  },<br>};</pre><p>So you’ll notice the keyword ‘this’ in the function,<strong> ‘this’ is something that, when used in a method, refers to the object in which it is called.</strong> To get the same functionality from a function declared outside of that object, we’d need to address the object directly using the variable name ‘kitchen’.</p><p>In order to use this method we just created, you only need to access it through ‘kitchen’ and then call it like normal. Here’s an example you can try to prove that it works…</p><pre>console.log(kitchen.oven.drawer);<br>kitchen.addToDrawer(&quot;cake tin&quot;);<br>console.log(kitchen.oven.drawer);</pre><p>Or in other words, look at the array, push to the array, then look at the array again.</p><p>You can make these methods as complex as you like, building on top of the basics I’ve demonstrated here. For example, you could build a method to add items to any given cupboard…</p><pre>let kitchen = {<br>  cupboards: [<br>    { location: &quot;below sink&quot;, contents: [] },<br>    { location: &quot;above stove&quot;, contents: [] },<br>    { location: &quot;left of stove&quot;, contents: [] },<br>    { location: &quot;right of stove&quot;, contents: [] },<br>    { location: &quot;below counter&quot;, contents: [] },<br>    { location: &quot;above counter&quot;, contents: [] },<br>  ],<br>  addToCupboard: (index, item) =&gt; {<br>    this.cupboards[index].contents.push(item)<br>  },<br>};</pre><p>Taking into account the fact that arrays start at index 0, we could use the following command to add something to the cupboard below the sink…</p><pre>kitchen.addToCupboard(0, &quot;cleaning spray&quot;)</pre><h3>Conclusion</h3><p>There is a lot more to learn about objects than I’ve covered here, but hopefully this helps you understand the basic principals of how objects work in JavaScript.</p><h4><strong>JavaScript In Plain English</strong></h4><p>Enjoyed this article? If so, get more similar content by <a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong>subscribing to Decoded, our YouTube channel</strong></a><strong>!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=82fc9d39db06" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/javascript-basics-objects-object-manipulation-82fc9d39db06">JavaScript Basics — Objects &amp; Object Manipulation</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to use Inquirer.js]]></title>
            <link>https://javascript.plainenglish.io/how-to-inquirer-js-c10a4e05ef1f?source=rss-dbef98fece58------2</link>
            <guid isPermaLink="false">https://medium.com/p/c10a4e05ef1f</guid>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[npm]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[cli]]></category>
            <dc:creator><![CDATA[Mat Wilmot]]></dc:creator>
            <pubDate>Sat, 08 Aug 2020 16:13:05 GMT</pubDate>
            <atom:updated>2020-08-10T06:48:18.376Z</atom:updated>
            <content:encoded><![CDATA[<h4>Inquirer is a promise-based npm package used in Node projects to create CLI (Command Line Interface) tools for query-based tasks</h4><p>In this article I want to talk about a CLI tool that I really enjoy using, and hopefully someone learns something along the way.</p><h3>Let’s start at the beginning: What is Inquirer?</h3><p>Inquirer is a promise-based npm package used in Node projects to create CLI (Command Line Interface) tools for query-based tasks. It’s great for asking the user questions, validating user inputs, and doing stuff with the responses given.</p><p>I have used Inquirer, alongside a mysql database, to create a CLI tool called the ‘employee manager’. It asks the user what they would like to do, and gives them a list of options. They can add new staff, assign roles, departments, and offices, set salaries, and so much more — all without having to create a front-end.</p><h3>What’s Node?</h3><p>Node.js is a JavaScript runtime built on <a href="https://v8.dev/">Chrome’s V8 JavaScript engine</a>. For more information on what Node is and how to set it up, <a href="https://nodejs.org/en/about/">click here</a>. You’ll need Node before you can use Inquirer.</p><h3>So how do I use Inquirer?</h3><p>Glad you asked! For the sake of this article, we’re going to build up a simple Inquirer-based project, starting at the very beginning and working all the way to so more in-depth stuff. Their documentation (link at the bottom) has all the info you’ll need to get set up, but let’s cover it here too.</p><p>If you haven’t done so already, initialize npm in your project by typing the following into your terminal at the root directory of your project:</p><pre>npm init -y</pre><p>Now you’re ready to install Inquirer. Type the following into your terminal:</p><pre>npm install inquirer</pre><p>The next step involves bringing the Inquirer library into the file you’re working on. You can do that by adding the following line to the start of your code:</p><pre>const inquirer = require(&quot;inquirer&quot;)</pre><p>So now that we’ve brought inquirer into our project, let’s build our first inquiry and run it. Every inquiry starts with the same code:</p><pre>inquirer.prompt()</pre><p>So we access the inquirer library and use the prompt method to display our question to the user. But it’s more complicated than that — inquirer takes an array of objects for its questions. So we open an array inside the parentheses, and then an object inside of that array, to give us something like this:</p><pre>inquirer.prompt([{}])</pre><p>Each question object should consist of at least three keys:</p><ul><li>name - how you refer to the question (and the answer given) later on</li><li>type - what type of question is this? (input, multiple choice, numerical etc)</li><li>message - the question you’re asking the user</li></ul><p>Using this new knowledge, we can create a basic question that looks something like this:</p><pre>inquirer<br>  .prompt([<br>    {<br>      name: &quot;user_name&quot;,<br>      type: &quot;input&quot;,<br>      message: &quot;What is your name?&quot;,<br>    },<br>  ]);</pre><p><em>It’s important to note that the default option for ‘type’ is ‘input’ and therefore isn’t actually necessary in this scenario, but I like to include it for the sake of clarity.</em></p><p>But we’re not done yet, we’ve asked the question but we’re not doing anything with the answer. The answer is given back to us as an object where the key is the ‘name’ property of the question, and the answer is its value. You can access the answer using .then() with a callback function inside. Let’s say we want to print the answer to this question to the console, we could use the following code:</p><pre>inquirer<br>  .prompt([<br>    {<br>      name: &quot;user_name&quot;,<br>      type: &quot;input&quot;,<br>      message: &quot;What is your name?&quot;,<br>    },<br>  ])<br>  .then((answer) =&gt; {<br>    console.log(&quot;Hello &quot; + answer.user_name);<br>  });</pre><p>Congratulations, you just wrote your first inquiry using Inquirer.js. To run this question, type the following into your console:</p><pre>node &lt;filename&gt;</pre><p>For the purpose of this article, I wrote my own file to demonstrate.</p><figure><img alt="A screenshot of some Inquirer code and it’s output in the terminal" src="https://cdn-images-1.medium.com/max/845/1*lsoqw1zy0IKSfjDxlTJ30A.png" /><figcaption>Demonstrating usage and expected outcome</figcaption></figure><h3>Let’s take it a step further</h3><p>So we’ve asked a question, how do we ask multiple questions? It’s as easy as adding another question object to the array. In the following example, I have added on to the code we wrote earlier, with a few small modifications, to ask the user for their first and last names:</p><pre>inquirer<br>  .prompt([<br>    {<br>      name: &quot;first_name&quot;,<br>      type: &quot;input&quot;,<br>      message: &quot;What is your first name?&quot;,<br>    },<br>    {<br>      name: &quot;last_name&quot;,<br>      type: &quot;input&quot;,<br>      message: &quot;What is your last name?&quot;,<br>    },<br>  ])<br>  .then((answer) =&gt; {<br>    console.log(&quot;Hello&quot;, answer.first_name, answer.last_name);<br>  });</pre><h3>Types of questions</h3><p>So we’ve learned to add input-based questions. What if we want the user to select from a range of possible options? All we need to do is change the ‘type’ field of the question. Inquirer offers many types for your questions, the most common ones I use are:</p><ul><li>Input</li><li>Number</li><li>Confirm</li><li>List</li><li>Checkbox</li><li>Password</li></ul><p>We’ve already seen some input type questions, so let’s see what we can do with the other types.</p><h3>type: number</h3><p>The following is an example of a question that uses a number and prints the result to the console.</p><pre>inquirer<br>  .prompt([<br>    {<br>      name: &quot;pet_count&quot;,<br>      type: &quot;number&quot;,<br>      message: &quot;How many pets do you own?&quot;,<br>    },<br>  ])<br>  .then((answer) =&gt; {<br>    console.log(&quot;You own&quot;, answer.pet_count, &quot;pets&quot;);<br>  });</pre><p>It comes with some built-in validation that returns ‘NaN’ if you try to enter something that isn’t a number (e.g. “eight”). We can use this to our advantage and build in a conditional that asks the question again if you didn’t give a number by making the question into a function like so:</p><pre>const getPetCount = () =&gt; {<br>  inquirer<br>    .prompt([<br>      {<br>        name: &quot;pet_count&quot;,<br>        type: &quot;number&quot;,<br>        message: &quot;How many pets do you own?&quot;,<br>      },<br>    ])<br>    .then((answer) =&gt; {<br>      if (!answer.pet_count) {<br>        console.log(&quot;That wasn&#39;t a number!&quot;);<br>        getPetCount();<br>      } else {<br>        console.log(&quot;You own&quot;, answer.pet_count, &quot;pets&quot;);<br>    });<br>};</pre><pre>getPetCount();</pre><p>What we did was turn the question into a re-usable function, and call the question again if the answer comes back as false. Note that, since the question function is not immediately invoked, we need to actually call the function <strong>below </strong>where we declared it.</p><h3>type: confirm</h3><p>Confirm gives the user a simple yes or no option and returns a boolean value as the answer. The following code is an example of how to use the confirm type:</p><pre>inquirer<br>  .prompt([<br>    {<br>      name: &quot;wants_pizza&quot;,<br>      type: &quot;confirm&quot;,<br>      message: &quot;Do you want a free pizza?&quot;,<br>    },<br>  ])<br>  .then((answer) =&gt; {<br>    console.log(answer.wants_pizza);<br>  });</pre><p>The user is presented with the question:</p><p><strong>Do you want a free pizza? (Y/n)</strong></p><p>A simple Y or N will give you ‘true’ or ‘false’. You could use this to build up a series of questions, all saved as functions, to make a pizza order builder. You do want a pizza? <em>How many pizzas do you want?</em> <em>What address should we deliver the pizzas to?</em> That’s a series of questions using confirm, number, and input question types. But what crust should the pizza have?</p><h3>type: list</h3><p>The list type requires you to add an additional key to your question object: a choices array. This will present the user with a list of options to pick from and save their submission as the answer to the question. This type completely prevents the user from entering their own inputs and restricts their choices to your pre-defined list. Here’s an example:</p><pre>inquirer<br>  .prompt([<br>    {<br>      name: &quot;pizza_crust&quot;,<br>      type: &quot;list&quot;,<br>      message: &quot;Choose your crust:&quot;,<br>      choices: [&quot;Thin Crust&quot;, &quot;Stuffed Crust&quot;, &quot;Pan&quot;],<br>    },<br>  ])<br>  .then((answer) =&gt; {<br>    console.log(answer.pizza_crust);<br>  });</pre><p>The user will be able to pick their crust type from the list and their choice will be printed to the console. If we stick with the pizza order builder idea, let’s say you defined an ‘order’ object at the start of the document, with keys for an address, a crust, number of pizzas, etc.. you could be pushing the values from these answers to that object, then move on to the next question. So what if we want to select multiple options from a list — pizza toppings, for example?</p><h3>type: checkbox</h3><p>Checkbox works similarly to a list, in that you have to provide a choices array, except the user can select multiple options. This would be a great way to select pizza toppings. Here’s an example:</p><pre>const toppingArray = [&quot;Cheese&quot;, &quot;Pepperoni&quot;, &quot;Onions&quot;, &quot;Peppers&quot;, &quot;Jalapeños&quot;, &quot;Chicken&quot;]</pre><pre>inquirer<br>  .prompt([<br>    {<br>      name: &quot;pizza_toppings&quot;,<br>      type: &quot;checkbox&quot;,<br>      message: &quot;Choose your toppings:&quot;,<br>      choices: toppingArray,<br>    },<br>  ])<br>  .then((answer) =&gt; {<br>    console.log(answer.pizza_toppings);<br>  });</pre><p>The user will be given the question “Choose your toppings:” and then be presented with a list of options, which, in this example, I defined previously in a variable. The user can use space to select/deselect options, and press enter to submit their answer. Once answered, the answers are saved as strings in an array. An answer to this question might look like this:</p><pre>[&quot;Cheese&quot;, &quot;Onions&quot;, &quot;Peppers&quot;, &quot;Chicken&quot;]</pre><p>Interesting idea, you can save these answers to a variable and use them again later in another ‘list’ or ‘checkbox’ style question.</p><h3>type: password</h3><p>This example doesn’t really fit in to the pizza order builder example, but it’s an important one to cover. The password type will hide the user input completely. No asterisks or blanked out characters, you will see absolutely nothing as you type. This is incredibly useful, though, as anyone looking over your shoulder can’t even see how long your password is! Here’s an example:</p><pre>inquirer<br>  .prompt([<br>    {<br>      name: &quot;user_password&quot;,<br>      type: &quot;password&quot;,<br>      message: &quot;Enter Password:&quot;,<br>    },<br>  ])<br>  .then((answer) =&gt; {<br>    console.log(answer.user_password);<br>  });</pre><p>Obviously, you don’t want to print the sensitive information you just typed directly into the console, but doing this initially is a great way to test that it worked. A better solution in a finished product may be to save the password to a variable or immediately submit it along with a username to log someone into a service.</p><h3>Want to build a pizza order builder?</h3><p>That’s awesome. <a href="https://github.com/MatWilmot/pizza-order-builder">I’ve created a super simple example and uploaded it to GitHub</a>, feel free to clone the repo and see how everything works!</p><h3>So what else can inquirer do?</h3><p>One of my favorite features in Inquirer, from before I learned I could separate all my questions into functions, is ‘when’. In a more complex scenario than the pizza order builder, you might want to add a follow-up question that only gets asked if the user selected a certain answer from a previous question. Here’s an example of how to use ‘when’:</p><pre>inquirer<br>  .prompt([<br>    {<br>      name: &quot;wants_pizza&quot;,<br>      type: &quot;confirm&quot;,<br>      message: &quot;Do you want a free pizza?&quot;,<br>    },<br>    {<br>      name: &quot;confirm_answer&quot;,<br>      type: &quot;confirm&quot;,<br>      message: &quot;Are you sure?&quot;,<br>      when: (answers) =&gt; answers.wants_pizza === false,<br>    },<br>  ])<br>  .then((answers) =&gt; {<br>    if (answers.wants_pizza) {<br>      console.log(&quot;The user wants free pizza&quot;);<br>    } else if (answers.confirm_answer) {<br>      // the user definitely doesn&#39;t want pizza<br>    } else {<br>      // the user changed their mind<br>      // run the function to ask this question again<br>    } <br>  });</pre><p>So we can see that the ‘when’ property is actually using a callback function with a basic conditional. If the answer was false, then ask if the user is sure. In the .then() block, we use a conditional to see:</p><ul><li>If the first question was answered as ‘true’, do something.</li><li>If the first answer was <strong>not</strong> true, if the second answer was true, the user definitely doesn’t want pizza, run some kind of exit function here</li><li>else the user said they didn’t want pizza, then changed their mind, so they should be redirected to the initial question</li></ul><p>It’s important to note that if the user says they do want a free pizza, they don’t get asked if they are sure — this is the power of ‘when’. <strong>By using ‘when’, we can ask a question <em>only</em> when a given criteria is met, and skip it otherwise.</strong></p><h3>So should I split my inquiries up into functions, or use ‘when’?</h3><p>In my opinion, both have their use cases. Splitting things up into functions is great for making reusable questions, but the answers to those questions are confined to the scope of their .then() blocks unless you save them to a variable outside of that scope. In the case of the pizza order builder, this was fine since each question contributed a value to an object.</p><p><em>The pizza order builder could just as easily have been two functions — one to display the start menu, and one that contains every follow-up question after that, since we always ask every question in that list.</em></p><p>One argument for using the ‘when’ feature is that it can save on the number of lines of code written and make your files a little shorter; you don’t need to write a whole inquirer.prompt() block, and then have a conditional to either send you to that question, or to the next question. Another benefit is that by asking questions in this way, you don’t lose the answer to the question that took you to that point by changing scope.</p><h3>Default Values</h3><p>Inquirer allows you to add default values to your inquiries, to speed up navigation and make suggestions to the user. Here’s an example of how it might be used:</p><pre>const mediaArray = [&quot;Facebook&quot;, &quot;Wikipedia&quot;, &quot;Medium&quot;];</pre><pre>inquirer<br>  .prompt([<br>    {<br>      name: &quot;fav_media&quot;,<br>      type: &quot;list&quot;,<br>      message: &quot;What is your favorite source for info?&quot;,<br>      choices: mediaArray,<br>      default: &quot;Medium&quot;,<br>    },<br>  ])<br>  .then((answer) =&gt; {<br>    console.log(answer.fav_media);<br>  });</pre><p>Now, when the user is asked for their favorite source of info, the selected option will be the third option — “Medium”.</p><figure><img alt="example of what functionality the default option gives us" src="https://cdn-images-1.medium.com/max/532/1*HzTKlXDIwR-mQ-pItc0IQA.png" /><figcaption>How the inquiry begins when you use a ‘default’ option</figcaption></figure><p>This can be very useful if you’re trying to implement CRUD functionality into your project, specifically the Update part. You can pull the existing value into a variable or function argument, and use that as the default value.</p><h3>More Information</h3><ul><li>If you want to learn more in-depth features in Inquirer.js, you can view the official docs on npm <a href="https://www.npmjs.com/package/inquirer">here</a>.</li></ul><h3>Credit where it’s due</h3><ul><li>Thank you to <a href="https://medium.com/u/b616c834b117">Squirrel</a> for their article on how to add code blocks to Medium articles. This thing looked a <strong><em>mess</em></strong> before I found that article.</li><li>Thank you to <a href="https://medium.com/u/9e6725f80bfc">Thomas W. Smith</a> for teaching me all of this (and so much more) in the first place, and for proof-reading this article.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c10a4e05ef1f" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/how-to-inquirer-js-c10a4e05ef1f">How to use Inquirer.js</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>