JavaScript Environment Setup and Hello World

6 min read

JavaScript Environment Setup and Hello World 🚀

Welcome to the exciting world of JavaScript! Before we start making magic happen on the web, let's get our tools in order and understand why we're even bothering with this language. It’s like learning the alphabet before writing a novel, or in our case, before building the next big thing online.


1. The "Why" Before the "How": Why JavaScript? 🤔

Definition and Problem:
Before you dive into coding, you might be asking, "Why JavaScript? There are so many programming languages out there." The problem for any aspiring web developer is choosing a language that is both powerful and widely used. You need a language that can bring your websites to life, making them interactive and engaging for users.

How JavaScript Solves It:
JavaScript is the language of the web. If HTML is the skeleton of a webpage and CSS is the skin and clothes, then JavaScript is the brain and muscles. It's what makes a website do things—react to a button click, show an animation, or fetch new data without reloading the page.

What is it used for?

  • Interactive Frontend: This is the classic use case. Think of things like interactive maps, animated charts, and the instant feedback you get when you fill out a form online.
  • Backend Development: With Node.js, JavaScript can run on servers, handling everything from databases to user authentication.
  • Mobile Apps: Frameworks like React Native allow you to build mobile apps for both iOS and Android using JavaScript.
  • And Much More: From creating games to programming for the Internet of Things (IoT), JavaScript is incredibly versatile.

Interesting Fact:
JavaScript was created in just 10 days in 1995 by Brendan Eich at Netscape. It was originally named Mocha, then LiveScript, before being renamed JavaScript in a marketing move to piggyback on the popularity of Java at the time. The two languages, however, are very different!


2. Setting Up Your Environment: Browsers, DevTools, and IDEs 🛠️

To start writing JavaScript, you don't need much. In fact, you already have the most crucial tool: your web browser!

Browsers and Developer Tools (DevTools):
Every modern web browser like Chrome, Firefox, or Edge comes with a built-in set of tools for developers. These "DevTools" are your best friend for debugging and experimenting with code.

How to open DevTools:

  • Right-click on a webpage and select "Inspect".
  • Press F12 or Ctrl+Shift+I (Windows/Linux) or Cmd+Option+I (Mac).

Once open, you'll see several tabs. The most important one for us right now is the Console. This is where we can write and test JavaScript code directly.

Integrated Development Environments (IDEs) and Code Editors:
While the browser console is great for quick tests, you'll want a more robust tool for writing and organizing your code. This is where a code editor or an IDE comes in.

  • Definition: An IDE is a software application that provides comprehensive facilities to computer programmers for software development. A code editor is a more lightweight version of this.
  • Problem: Writing code in a plain text editor is possible, but it's like trying to build a house with only a hammer. You lack features that make your life easier, like syntax highlighting, code completion, and debugging tools.
  • Solution: A good code editor or IDE will help you write cleaner code, faster.

Popular Choices for JavaScript:

  • Visual Studio Code (VS Code): This is the most popular choice for web developers. It's free, open-source, and has a massive library of extensions to customize it to your needs.
  • WebStorm: A very powerful IDE from JetBrains, it's a paid tool but offers a lot of advanced features out of the box.
  • Sublime Text & Atom: These are other great, lightweight options.

Getting Started with VS Code (Recommended for beginners):

  1. Download and Install: Head over to the official Visual Studio Code website and grab the installer for your operating system.
  2. Install Essential Extensions: Once installed, open VS Code and go to the Extensions view (the icon with the squares on the left). Here are a couple of must-haves:
    • Live Server: This extension lets you launch a local development server with a live reload feature for static and dynamic pages.
    • Prettier - Code formatter: This will automatically format your code to keep it clean and consistent.

3. Linking JavaScript to HTML: Inline vs. External Scripts 🔗

The Problem: You've written some JavaScript code. Now, how do you get it to actually run on your webpage? You need to connect your JavaScript to your HTML file.

There are two primary ways to do this: inline and external.

Inline Scripts

You can write your JavaScript code directly inside your HTML file using the <script> tag.

html
<!DOCTYPE html>
<html>
<head>
    <title>My First JS Page</title>
</head>
<body>

    <h1>Welcome!</h1>

    <script>
        // Your JavaScript code goes here
        console.log("This is an inline script!");
    </script>

</body>
</html>

When to use it: This method is okay for very small scripts or for quick testing.

External Scripts

This is the industry-standard and best practice. You create a separate file for your JavaScript (with a .js extension) and then link to it from your HTML.

1. Create your HTML file (index.html):

html
<!DOCTYPE html>
<html>
<head>
    <title>My First JS Page</title>
</head>
<body>

    <h1>Welcome!</h1>

    <script src="app.js" defer></script>

</body>
</html>

2. Create your JavaScript file (app.js):

javascript
console.log("This is from an external script!");

Why is this better?

  • Organization: It keeps your HTML and JavaScript separate, making your code cleaner and easier to manage.
  • Reusability: You can use the same JavaScript file on multiple HTML pages.
  • Caching: Browsers can cache external JavaScript files, which can make your website load faster for repeat visitors.

Important Note: Notice the defer attribute in the <script> tag. This tells the browser to download the script while it's parsing the HTML, but to wait to execute it until after the HTML has been fully parsed. This is the modern and recommended approach for including external scripts.


4. Your First Program: console.log() and Saying Hello 👋

It's a tradition in programming to start by making the computer say "Hello, World!". We'll do this using the console.log() method.

Definition and Problem:
As you write code, you'll often need to check the values of variables or see if certain parts of your code are being executed. How can you get a peek "under the hood" to see what's going on?

How console.log() Solves It:
The console.log() method is a way to print messages to the browser's developer console. It's an essential tool for debugging.

Let's write our first program!

Open your app.js file (or write this in the browser console) and type the following:

javascript
console.log("Hello, World!");

To see this in action:

  1. Make sure your app.js and index.html files are in the same folder.
  2. If you're using VS Code with the Live Server extension, right-click index.html and choose "Open with Live Server".
  3. Your webpage will open in your browser.
  4. Open the developer tools (F12 or Ctrl+Shift+I) and click on the "Console" tab.

You should see the message "Hello, World!" printed in the console. 🎉

Congratulations! You've just written and executed your first JavaScript program!


📚 Extra Reading & References

Challenge

You are tasked with correctly setting up a webpage to run an external JavaScript file, following modern best practices.

  1. In the index.html code block, add a single <script> tag inside the <head> element to link to a file named app.js.
  2. You must include the specific boolean attribute on your <script> tag that ensures the script is executed only after the browser has finished parsing the entire HTML document. This prevents the script from blocking the rendering of the page.
  3. In the app.js code block, write a single JavaScript statement that prints the exact string External script loaded successfully! to the browser's developer console.

Expected result

The index.html file should be modified to include a <script> tag within the <head>. This tag must correctly reference app.js and use the defer attribute.

Correct index.html head:

html
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>External Script Challenge</title>
    
    <!-- Add your script tag here -->
    <script src="app.js" defer></script>
</head>

The app.js file should contain a single console.log statement with the specified string.

Correct app.js content:

javascript
// Add your console.log statement here
console.log("External script loaded successfully!");
html

Solve the challenge, then check it.

Ctrl+Enter

Course complete!

You've passed every challenge in this course.