How can I execute JavaScript at the command prompt?


This tutorial teaches us to execute JavaScript at the command prompt.

JavaScript is the most popular language among programming languages that work with the browser. It is helpful to add the behaviors to the webpage and make it attractive. So, everyone can run JavaScript efficiently in the browser, but what if they want to run JavaScript via command prompt or other terminals.

Here, we have different approaches to running JavaScript at the command prompt.

Using the Node.js

One of the most popular ways to run server-side JavaScript via command prompt is the use of Node.js. Node.js is the run time environment for JavaScript. The whole thing we need to do is download the Node.js, and install it.

Steps to Install Node.js

  • Step 1 − Download the NodeJs from here according to your personal computer’s device specification.

  • Step 2 − Open the installed .exe file of Node.js and install the Node.js to your local computer.

  • Step 3 − To check that Node.js is correctly installed, open the terminal and type the below command. It should show you the version of the Node.js.

$node -v

After successfully installing the Node.js to the local computer, you just need to create a new JavaScript file and write some code into that.

Example

The below example demonstrates to run the JavaScript code at the command prompt. The user needs to create a new file named hello.js and need to add the below code to that file.

// basic JavaScript function
function runCodeFromTerminal() {
   let string1 = "welcome ";
   let string2 = "to the";
   let string3 = "tutorialspoint!"
   console.log( string1, string2, string3 );
}

// call the function on run the file.
runCodeFromTerminal();

Now, to run the above code at the terminal, open the terminal and go to the folder where the current file locates. Users can use the cd command to go to the file directory.

After going to the file directory, enter the below command to the terminal.

Command

$node hello.js

Output

Welcome to the tutorialsPoint!

Users can see that program executed successfully and prints the above output.

Write JavaScript Code to the Terminal and run it

In this method, rather than writing the JavaScript code into the file, we will write it into the terminal and directly execute it.

Make sure that you have installed Node.js on your local computer before you use this approach to run JavaScript in the command prompt.

Steps to writing code in terminal

The user must follow the steps below to write and run the JavaScript code into the terminal.

  • Step 1 − Install Node.js and enter the below command to the command prompt. It will show you a welcome message in the terminal.

$node
  • Step 2 − Write the JavaScript code line by line in the terminal. Users need to remember that they can’t write JavaScript code in the command prompt as they write in the file. Users need to write code step by step, and they can press enter after only they complete the whole step. For example, if users define a function, they need to write the entire function in a single line. After that, they can press enter and call the function in a different sequence.

Example

The below example demonstrates to run the JavaScript code at the Command Prompt.

Enter every step in the terminal line by line.

// basic JavaScript function
let a = 10;
let b = 20;
function runCodeToTerminal() {
   console.log( "The value of a + b is" , a+b );
}

// call the function to run the file.
runCodeToTerminal();

The function will call when you add the last line of the function call in your terminal and it prints the below output.

Output

The Value of a + b is 30.

Using the Nashorn Java Engine

The Nashorn is the engine based on the JSR 292 that comes with the Java SE 8. It provides a better run time performance for JavaScript.

To use the Nashorn engine, users must have installed Java on their local computer. You can find the JJS tool inside the bin folder of JDK installation, with the other tools like Jar.

We can run the JavaScript in the terminal using the JJS tool.

Syntax

$jjs filename.js

Example

Users need to follow different syntax when they want to run the JavaScript using the ‘jjs’ tool. For example, they need to use print instead of console.log in JavaScript code.

Add the below code to the file called hello.js.

// basic JavaScript function to run using the JJS tool
function runCodeToTerminal() {
   print( " Tutorial Points is the best site to learn computer science things." );
}

// call the function on run the file.
runCodeToTerminal();

Now, users need to run the below file using the JJS tool in the command prompt. Go to the file directory, and enter the below command to the terminal.

Command

$jjs hello.js

Output

Tutorial Points is the best site to learn computer science things.

Conclusion

We have looked at the three approaches to running the JavaScript code at the command prompt. The best approach is the run JavaScript code using Node.js. If users have fewer lines of code and need to perform some testing on code, they can use the second approach. The third approach is not recommended as it doesn’t follow the standard JavaScript syntax.

Updated on: 22-Jul-2022

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements