Javascript

Javascript is a versatile programming language used for creating dynamic and interactive web content.

Basic Concepts

Functions and Events

Breaking Tasks into Functions

Functions are like mini-machines: you give them input, and they give you output. They help organize your code into chunks you can reuse.

function greet(person) {
  alert("Hello, " + person + "!");
}

greet("Sam");

Here, greet is a function that says hello to anyone you want!

Responding to Events

JavaScript can react to things that happen, like clicks or key presses. These are called events.

document.getElementById("myButton").onclick = function() {
  alert("Button clicked!");
};

This code makes a button alert a message when clicked.

Why Use Functions and Events?

  • Make code easier to read and reuse
  • Respond to user actions, making websites interactive

Real-World Use Cases

  • A function that calculates the total price in a shopping cart
  • An event that submits a form when you press "Enter"

Examples

  • Making a character jump when you press a key in a game

  • Calculating your age from your birth year

In a Nutshell

Functions organize your code, and events let your website respond to users.