JavaScript is a programming language used to make web pages interactive. It runs in the browser and can do things like:
- Show/hide content
- Create image sliders
- Validate forms
- Handle user events like clicks or key presses
๐ ๏ธ Setting Up JavaScript
You can write JS directly in an HTML file:
htmlCopyEdit<!DOCTYPE html>
<html>
<head>
<title>JS Intro</title>
</head>
<body>
<h1>Hello, JavaScript!</h1>
<script>
alert('Welcome to JavaScript!');
</script>
</body>
</html>
๐ JS Basics (With Examples)
1. โ Variables
Used to store data.
jsCopyEditlet name = "Alice";
const age = 25;
var city = "New York";
2. ๐ Data Types
jsCopyEditlet text = "Hello"; // String
let number = 100; // Number
let isCool = true; // Boolean
let nothing = null; // Null
let something; // Undefined
let items = [1, 2, 3]; // Array
3. ๐ Functions
jsCopyEditfunction greet(name) {
console.log("Hello, " + name);
}
greet("Sam");
4. ๐ Conditional Statements
jsCopyEditlet age = 20;
if (age >= 18) {
console.log("You are an adult");
} else {
console.log("You are a minor");
}
5. ๐ Loops
jsCopyEditfor (let i = 1; i <= 5; i++) {
console.log("Count: " + i);
}
6. ๐ฆ Objects
jsCopyEditlet person = {
name: "John",
age: 30,
isStudent: false
};
console.log(person.name); // Output: John
๐ฑ๏ธ Interacting with the Browser (DOM)
Example: Changing content on click
htmlCopyEdit<p id="demo">Hello!</p>
<button onclick="changeText()">Click Me</button>
<script>
function changeText() {
document.getElementById("demo").innerHTML = "You clicked the button!";
}
</script>
๐ Next Steps
- Practice: Build small things like a counter, a to-do list, or a quiz.
- Learn more:
Would you like me to turn this into a mini-course or build a beginner project (like a calculator or to-do list) with you step-by-step?