Lesson 1: Introduction to JavaScript

Learn the fundamental concepts of JavaScript and why it's essential for modern web development.

1.1 What is JavaScript?

JavaScript is a scripting language that works with HTML to enhance web pages and make them more interactive by accessing and modifying the content in a webpage.

A scripting language is made up of a sequence of statements that give instructions for the computer to perform certain tasks, for examples, like providing a response to the users, to play a song, to start a slideshow, to display certain advertisements and so on.

JavaScript can turn a web page into a lively interactive platform for the world wide web users! JavaScript is a scripting language for HTML. Besides that, you can add sound, date, time, change the color of the web page according to a certain day, prevalidate data entered into a form by the user before it is sent to the server, search through a database, set options based on users preferences and much more.

Interactive Demo: JavaScript in Action

Display Alert

Click the button to see a JavaScript alert:

Change Content

Click the button to modify this text:

Original text content

Dynamic Styling

Click the button to change styling:

This element's style will change

1.2 Why Use JavaScript?

Why use JavaScript? You may have this question in mind. Well, let me get to the point. HTML or HyperText Markup Language was the only language that was used to present text and graphics as well as links to the world wide web users in the '90s of the last century. Although it was a vast improvement from the earlier text-only browsers like Gopher, it was relatively passive and static, it cannot interact much with the user. That is why we need JavaScript to make browsing the web a more interesting and useful experience for Internet users.

JavaScript is essential for creating dynamic, interactive web experiences that engage users and provide functionality beyond static content.

JavaScript vs. HTML Comparison

HTML Only

Static content with no interactivity:

Welcome to our website!

Last updated: 2023-01-01

HTML + JavaScript

Dynamic content with interactivity:

Welcome back, Guest!

Current date:

// JavaScript adds interactivity to static HTML
function updatePage() {
  // Get current date
  const now = new Date();
  document.getElementById('current-date').textContent = now.toLocaleDateString();
  
  // Update username
  const user = localStorage.getItem('username') || 'Guest';
  document.getElementById('username').textContent = user;
}

// Simulate login
function login() {
  const name = prompt('Enter your name:');
  if (name) {
    localStorage.setItem('username', name);
    updatePage();
  }
}

// Update page on load
window.addEventListener('load', updatePage);

1.3 The Building Blocks of JavaScript

As JavaScript is an object-oriented programming language; therefore building block of the JavaScript program code is made up of objects as well as methods, properties, and events associated with the objects.

Objects

The purpose of a JavaScript program is to manipulate the elements of a web page, such as documents, forms, radio buttons, checkboxes, buttons, windows and more. All the aforementioned elements are known as objects.

Properties

Every object of a web page has many properties. The properties of an object reflect its characteristics or its behaviors. For example, the properties of a text box are name, height, width, background color, with a border or no border, font type and size and more.

Methods

A method is a task carried out by an object. For example, to display a phrase on a web page, we can use the write method, using the syntax document.write('Phrase"). For example, document.write("Welcome to JavaScript") will display the "Welcome to JavaScript " message.

Events

An event is an execution of the JavaScript code triggered by an action from the user via the keyboard or mouse. For example, clicking the submit button on a web page will trigger an event that validates the input data and transmits the data to the server.

JavaScript Object Example

// Create a simple JavaScript object
const car = {
  // Properties
  make: 'Toyota',
  model: 'Camry',
  year: 2022,
  color: 'blue',
  
  // Method
  startEngine: function() {
    return 'Engine started! Vroom vroom!';
  },
  
  // Method using property values
  getInfo: function() {
    return `${this.year} ${this.make} ${this.model} (${this.color})`;
  }
};

// Accessing properties
console.log(car.make); // Output: Toyota

// Calling methods
console.log(car.startEngine()); // Output: Engine started! Vroom vroom!
console.log(car.getInfo()); // Output: 2022 Toyota Camry (blue)

Object Demo

Click the buttons to interact with the car object

1.3.4 Events and Event Handlers

Examples of events are Click, Load, KeyDown, KeyPress, DblClick (Double Click), Select and more. To respond to the aforementioned events, JavaScript uses event handlers. Some of the common event handlers are:

OnClick, OnChange, OnLoad, onSubmit, OnSelect, OnMouseOver, KeyPress

We will learn how to write codes for event handlers in later lessons.

Event Handling Demo

Click Event

Click the button to trigger an event:

Event result will appear here

Mouse Events

Hover over and out of this box:

Hover over me!