JS Getting Started

Welcome to another tutorial. In this tutorial, you will be introduced to different ways to run JavaScript on your computer.

As you may know, JavaScript is a popular programming language that has a broad range of applications.

In the earlier stage of the use of JavaScript, it was mostly used to enhance webpages' interactivity in areas such as animation form validation, etc. However, JavaScript is more than that, it’s now widely used in areas such as mobile application development server-side development, etc. 

The uniqueness of JavaScript makes it possible to be run in several ways, such as;

  • Creating web pages
  • Using the console tab of web browsers, and 
  • Using Node.js

 

1. Using Console Tab of Web Browsers

JavaScript can run on all popular web browsers because most browsers have built-in JavaScript. 

This is how to run JavaScript on your browser

  1.  Open your Google Chrome browser (or use any of your favorite browsers).
  2.  Open the browser developer tools by right-clicking on an empty area and selecting Inspect. [ or Shortcut: F12]

 

3. For other browsers: On the developer tools, go to the console tab. Then, you write the JS code and press enter to run the code.

 

2. Using Node.js

Node is a back-end run-time environment used in executing JavaScript code. To Node.js to run JavaScript, follow the steps below:

  • Install the newest version of Node.js.
  • Then, install an IDE/Text Editor such as Visual Studio Code. After installing Visual Studio code, do this;

create a file → write JS code →save it with a .js extension.

  • Open up the command prompt/terminal on your computer  navigate to the file location → type node hello.js → hit enter.
  • You will get output on the command prompt.

Note:  you can run JavaScript on the command prompt/terminal directly.in other to do that, type node and press enter. Thereafter, you can start writing JS code.

 

3. Creating Web Pages

From the above, we understood that JavaScript was created to make web pages interactive, as such JavaScript and HTML are interconnected and therefore go hand in hand. 

  • Open VS Code → Go to File →New File → Save it with the .html extension. Let’s take this example; main.html.
  • Copy this doctype as it is the least valid HTML code and save it in the file.

Just like this:

  • Similarly, to create a JavaScript file; you will start by writing down the following JS code and save it with ‘.js’ just like main.js. This is shown below.

 

One more thing, in the HTML file, we need to link the main.js file in other to use it. To do this, add the following code in main.html. 

 

  • Then open the main.html file using a browser.
  • To confirm if your JS code ran or not; 
  • Right-click on the web page → Inspect → Choose the console tab.

 

I believe you can now run JavaScript on your own. In the next lesson, you will be introduced to the fundamentals of JavaScript.