Giter VIP home page Giter VIP logo

node-repl's Introduction

Node REPL

Overview

Node REPL is the easiest way to get started with Node. You'll soon see that executing Node is straightforward and not intimidating at all. ;)

Objectives

  1. Define the REPL
  2. Launch the REPL
  3. Run Node in the REPL
  4. Exit the REPL

REPL

What do you do when you want to run some small code like a one-liner function or test out something, or see how a string function changes a substring? Or maybe you're working with dates and want to convert from one format to another. Saving code to a file each time you experiment, and running that file can be slow. There's a better way... meet REPL!

REPL stands for read-evaluate-print loop and does exactly what you would expect. It takes your command, executes it and prints the result if any. It looks similar to a command prompt or a terminal, but with an angle bracket > instead of the dollar $ or some other command prompt sign.

The commands in REPL are Node code. They are written in JavaScript or to be specific in one of the implementations of the ECMAScript standard, because browser JavaScript is just an implementation of this standard.

Another way to think about REPL is as an environment in which we can run Node code. Many other languages and platforms also have a similar REPL environment. Browser JavaScript can be run in the console of Google Chrome DevTools. You should be familiar with a REPL from using the Ruby-REPL irb.

Launching REPL

To launch the Node REPL you need to have these things ready:

  1. Node.js installed
  2. Terminal app a.k.a. command prompt on Windows
  3. node as the global command by exporting the path. This is done automatically if you used Node one-click installer from the website.

Open your terminal app. Most likely, you will see a dollar sign on Mac and Unix/Linux. This means you're in bash (Born Again Shell) or a similar shell environment. Now type node:

node

And hit enter. If you have Node installed properly, you'll see a pointy brace >.

Note: We've seen a few beginners to Node trying to execute shell command in the REPL. For example, creating a folder which is mkdir NAME. Normal shell commands would not and should not work in REPL, because REPL is a Node environment and it uses Node language, not shell. The giveaway sign of REPL is that you have the angle bracket >. Remember that when you run into SyntaxError: Unexpected identifier errors. All you need to do is open a new terminal window or exit REPL.

Running Node in REPL

We can run pretty much any code in REPL. Let's do some math:

> 1+1
2
> 13-3
10
> 10 % 3
1

Now let's define an expression (function which returns a value) which take two parameters and returns the sum of them:

> var sum = function(a, b) { return a+b}
undefined

REPL printed undefined, because assignment is not an expression. Don't freak out. We created our function and stored the reference in the sum variable. We can check it by typing sum and pressing enter. The sum is a function so the previous statement worked just fine:

> sum
[Function]

Let's compare how our new expression sum works by using comparison operand ===. The results are printed on the next line and they are booleans:

> 1+1 === sum(1,1)
true
> 2+2 === sum(2,2)
true
> 2+2 === sum(2,5)
false

So we can create function, variable, invoke them and pass around argument just like in DevTools. You saw that you can type some code and see results immediately. REPL is good for quickly testing your code. REPL is the easiest way among others to run your Node code.

Exiting REPL

Exiting REPL is straightforward. All you need to do is close the window. This will terminate the process. If you want to keep the window, you type:

> .exit

Or you can press control + c twice.

Resources

  1. REPL official docs
  2. Node.js Getting Started - Using the REPL and Running Files
  3. Using the Chrome DevTools Console
  4. How do I use node's REPL?

View Node REPL on Learn.co and start learning to code for free.

View Node REPL on Learn.co and start learning to code for free.

node-repl's People

Contributors

annjohn avatar azat-co avatar franknowinski avatar victhevenot avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-repl's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.