2 Simple Ways to Run Your JavaScript Code
Whether you are a programmer or a technical writer like me, at some point in your career, you will learn some JavaScript. When you start learning, one of the key things to know is how to run your code.
What is JavaScript?
JavaScript or JS (as it is often called) is a programming language used to make a website interactive. It is one of the core technologies for building web applications.
There are different ways to run a JS code. In this tutorial, you’ll learn two simple ways to do this:
Directly in Chrome.
Using the command line with Node.js.
Beyond these two, you can use frameworks like React and Angular to run your JavaScript code.
Prerequisite
This tutorial is for beginner software developers and technical writers familiar with basic programming tools like VS Code and Chrome.
To complete this tutorial, you need:
Chrome or your preferred web browser.
VS Code or your preferred code editor.
Node.js.
Running Your JS Code in Chrome
Here are the steps to do this:
Step 1: Open Chrome’s Developer Console Tool
Open a new tab on Chrome.
Right-click a space in the tab and select Inspect.
- Or press Ctrl + Shift + I on Windows or Cmd + Option + I on Mac.
In the Developer Tool, click Console.
Step 2: Run Your JavaScript Code
- In the Console, enter some JavaScript code. For example:
console.log('hello world!');
alert('JavaScript is working');
- Press Enter to run your code. If you use the above code, you will get two results: a pop-up or alert and a “hello world,” in the Console.
Step 3: Debugging errors
If your code returns an error, this will be displayed in the Console. To debug:
Check your code to ensure you entered the correct JavaScript syntax.
If there is an error in the syntax, correct it and run the code again.
You just learned how to run your JS code from the web browser. The next steps will focus on how to do this from the command line with Node.js.
Running the JS Code from the Command Line with Node.js
Node.js is a runtime environment that allows you to run JavaScript projects outside of a web browser. With Node.js, you can run your JS code directly in your code editor. Follow the steps below to do this:
Step 1: Write some JS in VS Code
Create a folder on your laptop and open it with VS Code.
Create a file in the folder.
Note: Your file should end with .js
so that the code editor will recognize it as a JavaScript file.
- Write some JS code in the file. For example:
console.log('hello world!');
alert('JavaScript is working');
Step 2. Install Node.js
If you already installed node.js, skip this step. If you don’t, follow the steps to install:
Download and install Node.js from the official website: https://nodejs.org/.
Follow the installation instructions for your operating system.
Verify installation. To do this:
Open the file in VS Code from step 1.
In the title area, click the three dots (if Terminal is not listed).
Select Terminal > New Terminal.
On the new terminal, click
+
to choose thecommand line
option you want to use (you can use PowerShell on Windows )
- Enter the code below:
node -v
This will display the installed version of Node.js.
Step 3: Run Your JavaScript File
- Enter
node
followed by your JS code file name. For example:
node (filename)
- You’ll see the output in the terminal:
Note: the first code worked, but the second one showed a ReferenceError
. Why is this?
Step 4: Debugging errors
If there’s an error in your code, Node.js will display the error message and point to the issue in your script. In the above screenshot, the error ReferenceError: alert is not defined
occurs because the alert
function is part of the browser's JavaScript runtime (used for displaying popup messages in web browsers) and is not available in Node.js, which runs JavaScript outside the browser.
Node.js does not provide browser-specific features like alert
. Instead, it focuses on server-side features.
To debug this error:
Replace
alert
with Node.js compatible code likeconsole.log
Enter the node command from Step 2 again.
Both codes work. No errors.
This is how to run your JS code from your code editor using Node.js.
Now that you’ve learned how to run your JS code both on the web browser and with Node.js, select the option that works for you.
Stay Happy Coding!