JavaScript Basics
What is JavaScript?
JavaScript is the programming language of the web. It runs in the browser and lets you make pages interactive — responding to clicks, updating content, and communicating with servers.
Adding JavaScript to a Page
Place your script in a script tag at the bottom of your HTML body.
console.log("Hello from JavaScript!");
Or link to an external file.
Variables
Use let and const to declare variables.
let name = "Alice";
const age = 25;
Functions
Define reusable blocks of code with functions.
function greet(name) {
return "Hello, " + name + "!";
}
console.log(greet("Alice"));
DOM Manipulation
Use JavaScript to interact with HTML elements.
const heading = document.getElementById("main-heading");
heading.textContent = "Updated by JavaScript!";
heading.style.color = "red";
Events
Respond to user actions like clicks.
const btn = document.getElementById("my-button");
btn.addEventListener("click", function() {
alert("Button clicked!");
});
Conditionals and Loops
JavaScript supports the same logic structures as other languages.
if (age >= 18) {
console.log("Adult");
}
for (let i = 0; i < 5; i++) {
console.log(i);
}
Next Steps
Learn about fetch for API calls, ES6 arrow functions, and frameworks like React.