- How can I execute JavaScript at the command prompt?
- Using the Node.js
- Steps to Install Node.js
- Example
- Command
- Output
- Write JavaScript Code to the Terminal and run it
- Steps to writing code in terminal
- Example
- Output
- Using the Nashorn Java Engine
- Syntax
- Example
- Command
- Output
- Conclusion
- 🚀 How to Run JavaScript Code
- How to Run JavaScript from the Command Line
- How to Run JavaScript from the Browser
- Run a script from an HTML file
- Inspect the Browser Console
- Run JavaScript with a Framework
- Run JavaScript in a Sandbox
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.
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
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.
- 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
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
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
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.
🚀 How to Run JavaScript Code
In order to follow along with this course, you need to know how and where you run your JavaScript code. You have several options to run your first hello world programming:
Open your editor and create a file named index.js .
How to Run JavaScript from the Command Line
Running a JS program from the command line is handled by NodeJS. Start by installing NodeJS on local machine if necessary.
Now simply open the command line in the same directory as the index.js script you created (VS Code will do this automatically with the integrated terminal).
How to Run JavaScript from the Browser
When people think of “JavaScript”, they most often think of a web browser. You can run code in the browser by creating an HTML file that references the script. In our case, we used the defer option, which will execute the JS after the HTML file is finished loading.
Run a script from an HTML file
html> head> script defer src="./index.js">script> head> html>
Now simply open this HTML file on your local machine and open the developer console (next step) to see the output.
Inspect the Browser Console
In Chrome, you can open the developer console with Ctrl+Shift+J (Windows) or Ctrl+Option+J (Mac), or manually from the settings menu by selecting More Tools -> Developer Tools. The console allows you to run code in the browser, similar to how
Run JavaScript with a Framework
It is worth mentioning that frameworks like React, Angular, Svelte, etc will take care of the building & running of your app automatically and provide framework-specific tooling and steps for running code. In the real world, you are more likely to use the tooling provided by the framework to run your code, as opposed to the basic methods shown in this couse.
Run JavaScript in a Sandbox
This course uses StackBlitz to run JS code examples in an isolated sandbox in the browser. This is a great option for sharing quick demos and issue reproductions 💡.