1. Introduction to Client-Side Scripting with JavaScript
JavaScript is a versatile scripting language used for creating dynamic content on web pages. It is primarily executed on the client’s browser, enabling interactive and responsive user interfaces.
2. Variables
Variables are used to store and manipulate data. In JavaScript, you can declare variables using the var, let, or const keyword.
var name = "John";
let age = 25;
const PI = 3.14;
3. Functions
Functions are blocks of reusable code. They are defined using the function keyword.
function greet(name) {
alert("Hello, " + name + "!");
}
// Function invocation
greet("John");
4. Conditions
Conditional statements allow you to make decisions in your code based on certain conditions.
var age = 18;
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are a minor.");
}
5. Loops and Repetition
Loops are used to repeatedly execute a block of code.
For loop
for (var i = 0; i < 5; i++) {
console.log(i);
}
While loop
var i = 0;
while (i < 5) {
console.log(i);
i++;
}
Do while loop
var i = 0;
do {
console.log(i);
i++;
} while (i < 5);
6. Pop-up Boxes
Pop-up boxes are often used for user interactions. JavaScript provides three types: alert, confirm, and prompt.
Alert
alert("This is an alert!");
Confirm
var result = confirm("Do you want to proceed?");
if (result) {
console.log("User clicked OK.");
} else {
console.log("User clicked Cancel.");
}
Prompt
var name = prompt("Please enter your name:", "John Doe");
console.log("User entered: " + name);