Execute JavaScript at the Command Prompt



To execute JavaScript at the command prompt, can be achieved through various methods depending on the environment. We will be discussing three different approach which can be used to execute JavaScript at the command prompt with stepwise explanation of steps.

In this article, our task is to execute JavaScript at the command prompt.

Different Methods to Run JavaScript at Terminal

Here is a list of methods that can be used to execute JavaScript at the command prompt which we will be discussing in this article with stepwise explaination and complete example codes.

Runnning JavaScript File on Terminal

In this method, we will be executing a javascript file(.js) in our terminal.

  • First, we need to install node.js in our system. You can visit official site to download it for free.
  • Open the terminal or windows powershell in your system and go to the folder where your file is located. You can either use cd command to traverse or you can simply visit the target folder, and while pressing shift press right click and you will get option of open powershell or open terminal.
  • Once you are in your target folder write the command: node filename.js in your terminal.

Example

Here is an example implementing above mentioned steps to execute javascript file exp.js in command prompt.

<script>
    function runCodeFromTerminal() {
        let string1 = "Hello World!!! ";
        let string2 = "Running js code through terminal...";
        document.write(string1, string2);
    }
    runCodeFromTerminal();
</script>

Output

Compiling Code Directly on Terminal

In this method to execute JavaScript at the command prompt, we will be compiling our code directly in terminal.

  • You must have node.js installed in your system beforehand. Now, open the terminal or windows powershell.
  • Type node in the terminal, then type .editor, this will allow you to enter the editor mode.
  • Now, write your code in the terminal. After completing the code press ctrl+D to finish. It will execute your code.

Example

Here is the example of code written and executed in terminal.

<script>
    let a = 10;
    let b = 20;
    //console.log(a+b);
    document.write(a+b);
</script>

Output

Using Browser's Console

In this method, to execute JavaScript code at the command prompt, we will be compiling our code using browser's console.

  • Open any browser and go to the console panel, you can press F12 or ctrl+shift+i button.
  • Now you can write your JavaScript code in the console and hit enter to run your code.

Example

Remember you have to write down your code line by line to make it work in the browser console.

<script>
    //console.log("Hello World");
    //console.log("Welcome to Tutorialspoint");
    document.write("Hello World");
    document.write("Welcome to Tutorialspoint");
</script>

Output

Conclusion

In this article, we discussed three different approaches to execute JavaScript at the command prompt. If you are executing the javascript code in your terminal then you need to install node.js first. We have discussed three methods, any of the methods can be used to execute at the terminal.

Updated on: 2024-11-27T10:22:08+05:30

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements