How do you run JavaScript through the Terminal?
If you want to run a JavaScript script through the Terminal, you’ll first need to make sure you have Node.js installed on your computer. Here’s a simple guide to help you get started:
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.
// 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.