JavaScript is a programming language used to make websites interactive. It works with HTML and CSS.
Try this simple example:
<!DOCTYPE html>
<html>
<head>
<title>My First JS</title>
</head>
<body>
<h1>Hello JavaScript</h1>
<script>
alert("Hello, World!");
</script>
</body>
</html>
This code shows a popup alert that says Hello, World!.
Variables store data. Example:
let name = "Alice";
let age = 25;
console.log(name); // Alice
console.log(age); // 25
Use let for variables, const for constants.
| Type | Example |
|---|---|
| String | "hello" |
| Number | 10, 3.14 |
| Boolean | true, false |
| Array | [1, 2, 3] |
| Object | { name: "Bob" } |
| null | null |
| undefined | undefined |
function greet(name) {
console.log("Hello, " + name + "!");
}
greet("Alice"); // Hello, Alice!
let age = 18;
if (age >= 18) {
console.log("You're an adult");
} else {
console.log("You're a minor");
}
for (let i = 1; i <= 5; i++) {
console.log("Number: " + i);
}
let fruits = ["apple", "banana", "orange"];
console.log(fruits[0]); // apple
let person = {
name: "Alice",
age: 25
};
console.log(person.name); // Alice
Try this interactive example:
Hello
document.getElementById('btnClickMe').addEventListener('click', function () {
document.getElementById('demo').innerHTML = 'You clicked the button!';
});