Open In App

How do you run JavaScript script through the Terminal?

Last Updated : 26 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

To run a JavaScript script through the Terminal, you’ll need to have Node.js installed on your machine. Here’s a simple step-by-step guide.

Steps to run JavaScript script through the Terminal

Step 1: If you haven’t installed Node.js yet, you can download it from the official website: Node.js Downloads.

Step 2: Create a file of your interest with .js extension.

Example: Create a JavaScript file. Name this file as New.js

javascript




// Function to add two variables.
 
const add = (a, b) => {
    return a + b
}
console.log(add(4, 6))


Output

10

Step 3: Open your terminal or command prompt.

Step 4: Use the cd command to navigate to the directory where your JavaScript file is located.

Step 5: Once you’re in the correct directory, run the script using the node command followed by your script’s filename.

node <FileName>.js

JavaScript is best known for web page development but is also used in various non-browser environments. You can learn about JavaScript from the ground up by following JavaScript Tutorial and JavaScript Examples.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads