![]() ![]() ![]() Hopefully, after testing our code in REPL, we don’t have unexpected side effects in our applications (such as expecting a result of true but getting a result of false). REPL’s helpfulness is in enabling us to discover these little interesting quirks in JavaScript and our code. Hence, true is not greater than 1, and the result is false. JavaScript provides automatic data type conversion, after which true and 1 are equivalent values. But then the value of true is compared to the numeric 1. What’s happening is that the expression 3 > 2 is evaluated, returning true. However, in JavaScript, relational operators are evaluated left to right, and each expression’s result is returned for the next evaluation.Ī better way of looking at what’s happening with the preceding code snippet is this REPL session: At first glance, we might expect the expression just typed to evaluate to true, since 3 is greater than 2, which is greater than 1. This code snippet is a good example of how REPL can be useful. The latter two command lines do have results, which are printed out by REPL.īenefits of REPL: Getting a Closer Understanding of JavaScript Under the Hood You can use the variable in REPL, just as you would in a Node application: REPL is a read-eval-print loop, with emphasis on the eval. There is no result from the second call to eval hence, the value returned is undefined. Typing the preceding line into a file and running that file using Node returns: The reason is that the result of the expression is undefined since variable assignment doesn’t return a result when evaluated.Ĭonsider the following instead, which is what’s happening, more or less, under the hood in REPL: It doesn’t return the value 2 it returns a value of undefined. For instance, consider the following line in REPL: You can use the var keyword with REPL in order to access an expression or value at a later time, but you might get an unexpected result. You can even access properties or call methods on the underscored expression: In the following, a is set to 2, and the resulting expression is incremented by 1, and then 1 again: To access the last expression, use the underscore/underline special variable (_). In the following, the expression result is an array with three elements: In this session excerpt, the value of the expression is 2. The tool prints out the result of whatever expression you just typed. Just start typing in your JavaScript, like you’d add it to a file: Anything you type from this point on is processed by the underlying V8 JavaScript engine. REPL then provides a command-line prompt-an angle bracket (>)-by default. To begin REPL, simply type node without providing any Node application file, like so: REPL: First Looks and Undefined Expressions We’re using the console in most applications in the book, but there’s more to this helpful object than just logging messages. REPL is an essential Node development tool, and so is the console. And if the built-in REPL doesn’t provide exactly what you need for an interactive environment, there’s also an API to create your own custom REPL. These workarounds include replacing the underlying mechanism that persists commands, as well as using some command-line editing. In this chapter, I’ll cover how to use REPL, as well as some interesting quirks of REPL and how to work with them. You can actually use REPL to code your entire application-literally testing the application on the fly. Whatever you type into REPL is, for the most part, processed no differently than if you had typed the JavaScript into a file and run the file using Node. REPL (pronounced “repple”) supports simplified line editing and a small set of basic commands. Node also comes with an interactive component known as REPL, or read-eval-print loop. While you’re exploring the use of Node and figuring out the code for your custom module or Node application, you don’t have to type JavaScript into a file and run it with Node to test your code. Interactive Node with REPL and More on the Console
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |