No history yet

Introduction to JavaScript

Making Web Pages Come Alive

Think of a website as a house. HTML is the frame and the walls—the basic structure. CSS is the paint, the furniture, and the decorations—it’s all about the looks. But what makes the lights turn on, the faucet run, and the doorbell ring? That's JavaScript.

JavaScript is a high level, interpreted, programming language used to make web pages more interactive.

JavaScript, often shortened to JS, is the programming language that allows you to implement complex features on web pages. Every time you see an interactive map, an animated graphic, or a form that responds instantly to your input, you're seeing JavaScript in action. It's the engine that powers the dynamic, interactive parts of the web.

Lesson image

A Quick History

Back in 1995, the web was mostly static documents. A developer at Netscape named Brendan Eich was tasked with creating a simple scripting language to run in the Netscape Navigator browser. He famously created the first version in just 10 days.

Initially called Mocha, then LiveScript, it was eventually renamed JavaScript in a marketing move to align with the popular Java language. Despite the name, JavaScript and Java are completely different languages, like the difference between a car and a carpet.

To prevent any single company from controlling the language, JavaScript was submitted to ECMA International, a standards organization. This led to the creation of the ECMAScript standard, which all modern browsers follow. This ensures that a script written today will work consistently across different browsers like Chrome, Firefox, and Safari.

Setting Up Your Workspace

Getting started with JavaScript is surprisingly simple. You don't need any complex software. All you need are two tools you likely already have: a web browser and a text editor.

Any modern web browser like Google Chrome, Mozilla Firefox, or Microsoft Edge comes with powerful built-in developer tools. The most important one for us is the console, which lets you run JavaScript code and see its output directly.

While you could use a basic text editor like Notepad, a dedicated code editor makes writing code much easier. Editors like Visual Studio Code, Sublime Text, or Atom offer features like syntax highlighting, which color-codes your script to make it more readable, and auto-completion. We'll use a code editor to write our files.

Lesson image

Your First Script

JavaScript code is typically embedded within an HTML file using the <script> tag. The browser reads the HTML, and when it encounters a <script> tag, it executes the JavaScript code inside it. Let's create our first program.

First, create a new file and name it index.html. Then, type or paste the following code into it:

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

  <h1>Welcome!</h1>

  <script>
    // This is a single-line comment
    console.log("Hello, World!");
  </script>

</body>
</html>

Save this file and open it in your web browser. You'll see the "Welcome!" heading. But where's the "Hello, World!" text? It's in the developer console.

To see it, open the developer tools. You can usually do this by right-clicking on the page and selecting "Inspect," or by pressing F12 or Cmd+Option+I. Then, click on the "Console" tab. You should see your message printed there.

console.log() is a fundamental tool for any JavaScript developer. It's the primary way to check the values of variables and see what your code is doing behind the scenes.

Let's break down the basic syntax:

  • Statements: Each instruction is a statement. In our example, console.log("Hello, World!"); is a statement. Statements in JavaScript often end with a semicolon ;. While modern JavaScript can often work without them, it's a good habit to include them to avoid ambiguity.
  • Comments: Any text following a // on the same line is a comment. The browser ignores it. Comments are for developers to leave notes and explain their code.
  • Case Sensitivity: JavaScript is case-sensitive. myVariable is a different variable from myvariable.

Let's check your understanding of these core ideas.

Quiz Questions 1/5

In the common analogy of a website as a house, what role does JavaScript play?

Quiz Questions 2/5

True or False: JavaScript was created as a simplified version of the Java programming language.