This article is recorded by a student studying JS instead of a memo. Don't expect the content.
This time it will be done on Ubuntu, so I will start the virtual environment with iTerm2.
Where it started
Move to the directory where Ubuntu is installed. vagrant up is a command to start Ubuntu installed on a virtual PC, and vagrant ssh connects to SSH with the Vagrant virtual machine set.
On the console
If you enter, the console will be ready to accept js. This is a state called REPL. Describe the node.js file saved in the directory to be implemented this time below.
const n = process.argv[2] || 0;
let sum = 0;
for (let i = 1; i <= n; i++) {
s = s + i;
}
console.log(s);
The first line is a description for using JS in strict mode.,The second line is a variable called n```process.argv[2] || 0
The above values are assigned. process.argv is Node.Get command line arguments from the js program. argv[0]Node executable file(node.exe)Full path, argv[1]Script file(.js)Because the full path of,Can store argv[2]Because.||Returns the left side if the JS logical operator OR can see the left side as true. Otherwise it returns the right side. When used with a boolean value If either of the operation targets is true,||Returns true, and if both are false, returns false.
The third line specifies a variable called s, and the fourth and subsequent lines write a loop using a for statement. The loop continues until i is the value of the command line argument.
Finally, display the value of s on the console and finish.
If you write the following in the console of the virtual environment and the value is displayed, it is successful. Thank you for your hard work!!!
```node file name command line argument
When the user enters one or more expressions in the REPL, the interpreter evaluates them and displays the results on the console. The statically typed language REPL allows you to enjoy the convenience of a dynamically typed language while maintaining type safety, but some REPLs installed in the integrated development environment run on the code editor. Some have full-fledged code completion similar to what they do.
The command line is a screen operated only with the keyboard, and the arguments are the values passed to the program or function. In other words, when you say a command line argument, interpret it as a value to be passed to the program.
Recommended Posts