![]() To see what happens if you give the program an argument that is not a valid environment variable, run the following: Now you have a way to retrieve the variables the user asks for, but we still need to handle the case where the user enters bad data. The forEach function ensures that every command line argument in the args array is printed. Now re-run the program with two arguments: You use forEach on the args array, providing it a callback function that prints the current argument’s value in the environment. It accepts a callback function that is used as it iterates over every element of the array. The forEach method is a standard JavaScript method on all array objects. Open the environment.js file for editing:Įcho.js const args = process. Like all objects in JavaScript, you access an individual property by referencing its name in square brackets. The process.env object is a simple mapping between environment variable names and their values stored as strings. In this step you’ll view environment variables and their values using the global process.env object and print their values to the console. Step 5 - Accessing a Specified Environment Variable Rather than viewing a long list of environment variables, you might want to retrieve a specific one. Keep in mind that many of the environment variables you see are dependent on the configuration and settings of your system, and your output may look substantially different than what you see here. '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin',ĭBUS_SESSION_BUS_ADDRESS: 'unix:path=/run/user/1000/bus', In this step, you will make your code accept user input via command line arguments.Ĭreate a new file arguments.js with nano: For example, running node with the -version argument prints the installed version instead of running the interpreter. In order to make the program more dynamic, let’s get input from the user and display it on the screen.Ĭommand line tools often accept various arguments that modify their behavior. Step 3 - Receiving User Input via Command Line ArgumentsĮvery time you run the Node.js “Hello, World!” program, it produces the same output. Having confirmed that the program works, let’s make it more interactive. The string "Hello World" was passed as an argument to the log function.Īlthough quotation marks are necessary in the code to indicate that the text is a string, they are not printed to the screen. The Node.js interpreter read the file and executed console.log("Hello World") by calling the log method of the global console object. With the text editor opened, enter the following code: To write a “Hello, World!” program, open up a command line text editor such as nano and create a new file: ![]() Click the Launch an Interactive Terminal! button below to get started. If you would like to experiment with Node.js without using a server or your local computer, you can use an interactive terminal in your browser.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |