Mastering JavaScript and SQL Book
Mastering JavaScript and SQL Book
Christopher Honeywick is a seasoned web developer and database expert with years of
experience in building enterprise-level web applications. This book is a product of his
commitment to empowering aspiring developers with hands-on, practical, and modern
programming skills.
Chapter 1: Introduction to JavaScript
<!DOCTYPE html>
<html>
<head>
<title>JS Example</title>
</head>
<body>
<h1 id="heading">Hello World</h1>
<button onclick="changeText()">Click Me</button>
<script>
function changeText() {
document.getElementById("heading").innerHTML = "Text
Changed!";
}
</script>
</body>
</html>
Chapter 1: Introduction to JavaScript
<!DOCTYPE html>
<html>
<head>
<title>JS Example</title>
</head>
<body>
<h1 id="heading">Hello World</h1>
<button onclick="changeText()">Click Me</button>
<script>
function changeText() {
document.getElementById("heading").innerHTML = "Text
Changed!";
}
</script>
</body>
</html>
Chapter 2: JavaScript Syntax, Variables, and Data Types
Understanding the basic syntax and structure of JavaScript is crucial. In this chapter, you'll
learn how to declare variables, use different data types, and write syntactically correct
JavaScript code.
Variables
JavaScript uses three main keywords for variable declaration: var, let, and const.
- `var` was traditionally used, but it's now recommended to use `let` or `const`.
- `let` allows variable reassignment.
- `const` defines a constant, which cannot be reassigned.
Example:
Data Types
Example:
<!DOCTYPE html>
<html>
<head>
<title>JS Example</title>
</head>
<body>
<h1 id="heading">Hello World</h1>
<button onclick="changeText()">Click Me</button>
<script>
function changeText() {
document.getElementById("heading").innerHTML = "Text
Changed!";
}
</script>
</body>
</html>
Chapter 2: JavaScript Syntax, Variables, and Data Types
Understanding the basic syntax and structure of JavaScript is crucial. In this chapter, you'll
learn how to declare variables, use different data types, and write syntactically correct
JavaScript code.
Variables
JavaScript uses three main keywords for variable declaration: var, let, and const.
- `var` was traditionally used, but it's now recommended to use `let` or `const`.
- `let` allows variable reassignment.
- `const` defines a constant, which cannot be reassigned.
Example:
Data Types
Example:
Functions are reusable blocks of code that perform a specific task. Scope refers to the
context in which variables are accessible. Understanding how functions and scope work is
essential to writing clean and effective JavaScript.
1. **Function Declaration**
function greet(name) {
return "Hello, " + name + "!";
}
greet("Alice");
2. **Function Expression**
Scope in JavaScript
Scope determines the visibility of variables. There are two types of scope:
function showScope() {
let localVar = "I'm local";
console.log(globalVar);
console.log(localVar);
}
showScope();
console.log(localVar); // Error: localVar is not defined
Chapter 1: Introduction to JavaScript
<!DOCTYPE html>
<html>
<head>
<title>JS Example</title>
</head>
<body>
<h1 id="heading">Hello World</h1>
<button onclick="changeText()">Click Me</button>
<script>
function changeText() {
document.getElementById("heading").innerHTML = "Text
Changed!";
}
</script>
</body>
</html>
Chapter 2: JavaScript Syntax, Variables, and Data Types
Understanding the basic syntax and structure of JavaScript is crucial. In this chapter, you'll
learn how to declare variables, use different data types, and write syntactically correct
JavaScript code.
Variables
JavaScript uses three main keywords for variable declaration: var, let, and const.
- `var` was traditionally used, but it's now recommended to use `let` or `const`.
- `let` allows variable reassignment.
- `const` defines a constant, which cannot be reassigned.
Example:
Data Types
Example:
Functions are reusable blocks of code that perform a specific task. Scope refers to the
context in which variables are accessible. Understanding how functions and scope work is
essential to writing clean and effective JavaScript.
1. **Function Declaration**
function greet(name) {
return "Hello, " + name + "!";
}
greet("Alice");
2. **Function Expression**
Scope in JavaScript
Scope determines the visibility of variables. There are two types of scope:
function showScope() {
let localVar = "I'm local";
console.log(globalVar);
console.log(localVar);
}
showScope();
console.log(localVar); // Error: localVar is not defined
Chapter 4: DOM Manipulation and Events
The Document Object Model (DOM) allows JavaScript to access and modify HTML content
dynamically. You can use JavaScript to change the structure, style, and content of a web
page in response to user actions.
- `getElementById()`
- `getElementsByClassName()`
- `querySelector()`
- `querySelectorAll()`
Example:
Example:
Handling Events
JavaScript can respond to user events like clicks, typing, and form submissions.
Example:
<!DOCTYPE html>
<html>
<head>
<title>JS Example</title>
</head>
<body>
<h1 id="heading">Hello World</h1>
<button onclick="changeText()">Click Me</button>
<script>
function changeText() {
document.getElementById("heading").innerHTML = "Text
Changed!";
}
</script>
</body>
</html>
Chapter 2: JavaScript Syntax, Variables, and Data Types
Understanding the basic syntax and structure of JavaScript is crucial. In this chapter, you'll
learn how to declare variables, use different data types, and write syntactically correct
JavaScript code.
Variables
JavaScript uses three main keywords for variable declaration: var, let, and const.
- `var` was traditionally used, but it's now recommended to use `let` or `const`.
- `let` allows variable reassignment.
- `const` defines a constant, which cannot be reassigned.
Example:
Data Types
Example:
Functions are reusable blocks of code that perform a specific task. Scope refers to the
context in which variables are accessible. Understanding how functions and scope work is
essential to writing clean and effective JavaScript.
1. **Function Declaration**
function greet(name) {
return "Hello, " + name + "!";
}
greet("Alice");
2. **Function Expression**
Scope in JavaScript
Scope determines the visibility of variables. There are two types of scope:
function showScope() {
let localVar = "I'm local";
console.log(globalVar);
console.log(localVar);
}
showScope();
console.log(localVar); // Error: localVar is not defined
Chapter 4: DOM Manipulation and Events
The Document Object Model (DOM) allows JavaScript to access and modify HTML content
dynamically. You can use JavaScript to change the structure, style, and content of a web
page in response to user actions.
- `getElementById()`
- `getElementsByClassName()`
- `querySelector()`
- `querySelectorAll()`
Example:
Example:
Handling Events
JavaScript can respond to user events like clicks, typing, and form submissions.
Example:
Objects and arrays are fundamental data structures in JavaScript. They allow you to group
related data and manipulate collections efficiently.
Objects
Objects store data in key-value pairs. Each key is a string, and the value can be any type.
Example:
const person = {
name: "Alice",
age: 30,
isStudent: false
};
console.log(person.name); // Alice
Modifying Objects
Example:
person.age = 31;
person.city = "New York";
delete person.isStudent;
Arrays
Arrays are ordered collections of items. You can access elements using index numbers.
Example:
Array Methods
Example:
fruits.push("Orange");
fruits.forEach(function(fruit) {
console.log(fruit);
});
Chapter 1: Introduction to JavaScript
<!DOCTYPE html>
<html>
<head>
<title>JS Example</title>
</head>
<body>
<h1 id="heading">Hello World</h1>
<button onclick="changeText()">Click Me</button>
<script>
function changeText() {
document.getElementById("heading").innerHTML = "Text
Changed!";
}
</script>
</body>
</html>
Chapter 2: JavaScript Syntax, Variables, and Data Types
Understanding the basic syntax and structure of JavaScript is crucial. In this chapter, you'll
learn how to declare variables, use different data types, and write syntactically correct
JavaScript code.
Variables
JavaScript uses three main keywords for variable declaration: var, let, and const.
- `var` was traditionally used, but it's now recommended to use `let` or `const`.
- `let` allows variable reassignment.
- `const` defines a constant, which cannot be reassigned.
Example:
Data Types
Example:
Functions are reusable blocks of code that perform a specific task. Scope refers to the
context in which variables are accessible. Understanding how functions and scope work is
essential to writing clean and effective JavaScript.
1. **Function Declaration**
function greet(name) {
return "Hello, " + name + "!";
}
greet("Alice");
2. **Function Expression**
Scope in JavaScript
Scope determines the visibility of variables. There are two types of scope:
function showScope() {
let localVar = "I'm local";
console.log(globalVar);
console.log(localVar);
}
showScope();
console.log(localVar); // Error: localVar is not defined
Chapter 4: DOM Manipulation and Events
The Document Object Model (DOM) allows JavaScript to access and modify HTML content
dynamically. You can use JavaScript to change the structure, style, and content of a web
page in response to user actions.
- `getElementById()`
- `getElementsByClassName()`
- `querySelector()`
- `querySelectorAll()`
Example:
Example:
Handling Events
JavaScript can respond to user events like clicks, typing, and form submissions.
Example:
Objects and arrays are fundamental data structures in JavaScript. They allow you to group
related data and manipulate collections efficiently.
Objects
Objects store data in key-value pairs. Each key is a string, and the value can be any type.
Example:
const person = {
name: "Alice",
age: 30,
isStudent: false
};
console.log(person.name); // Alice
Modifying Objects
Example:
person.age = 31;
person.city = "New York";
delete person.isStudent;
Arrays
Arrays are ordered collections of items. You can access elements using index numbers.
Example:
Array Methods
Example:
fruits.push("Orange");
fruits.forEach(function(fruit) {
console.log(fruit);
});
Chapter 6: Conditional Statements and Loops
Conditional statements and loops allow you to control the flow of your code. Conditions
decide what code runs, while loops allow you to repeat blocks of code.
Conditional Statements
- `if`
- `else if`
- `else`
- `switch`
Example (if-else):
Example (switch):
- `for` loop
- `while` loop
- `do...while` loop
- `for...of` and `for...in` loops
let i = 0;
while (i < 3) {
console.log("i is " + i);
i++;
}
<!DOCTYPE html>
<html>
<head>
<title>JS Example</title>
</head>
<body>
<h1 id="heading">Hello World</h1>
<button onclick="changeText()">Click Me</button>
<script>
function changeText() {
document.getElementById("heading").innerHTML = "Text
Changed!";
}
</script>
</body>
</html>
Chapter 2: JavaScript Syntax, Variables, and Data Types
Understanding the basic syntax and structure of JavaScript is crucial. In this chapter, you'll
learn how to declare variables, use different data types, and write syntactically correct
JavaScript code.
Variables
JavaScript uses three main keywords for variable declaration: var, let, and const.
- `var` was traditionally used, but it's now recommended to use `let` or `const`.
- `let` allows variable reassignment.
- `const` defines a constant, which cannot be reassigned.
Example:
Data Types
Example:
Functions are reusable blocks of code that perform a specific task. Scope refers to the
context in which variables are accessible. Understanding how functions and scope work is
essential to writing clean and effective JavaScript.
1. **Function Declaration**
function greet(name) {
return "Hello, " + name + "!";
}
greet("Alice");
2. **Function Expression**
Scope in JavaScript
Scope determines the visibility of variables. There are two types of scope:
function showScope() {
let localVar = "I'm local";
console.log(globalVar);
console.log(localVar);
}
showScope();
console.log(localVar); // Error: localVar is not defined
Chapter 4: DOM Manipulation and Events
The Document Object Model (DOM) allows JavaScript to access and modify HTML content
dynamically. You can use JavaScript to change the structure, style, and content of a web
page in response to user actions.
- `getElementById()`
- `getElementsByClassName()`
- `querySelector()`
- `querySelectorAll()`
Example:
Example:
Handling Events
JavaScript can respond to user events like clicks, typing, and form submissions.
Example:
Objects and arrays are fundamental data structures in JavaScript. They allow you to group
related data and manipulate collections efficiently.
Objects
Objects store data in key-value pairs. Each key is a string, and the value can be any type.
Example:
const person = {
name: "Alice",
age: 30,
isStudent: false
};
console.log(person.name); // Alice
Modifying Objects
Example:
person.age = 31;
person.city = "New York";
delete person.isStudent;
Arrays
Arrays are ordered collections of items. You can access elements using index numbers.
Example:
Array Methods
Example:
fruits.push("Orange");
fruits.forEach(function(fruit) {
console.log(fruit);
});
Chapter 6: Conditional Statements and Loops
Conditional statements and loops allow you to control the flow of your code. Conditions
decide what code runs, while loops allow you to repeat blocks of code.
Conditional Statements
- `if`
- `else if`
- `else`
- `switch`
Example (if-else):
Example (switch):
- `for` loop
- `while` loop
- `do...while` loop
- `for...of` and `for...in` loops
let i = 0;
while (i < 3) {
console.log("i is " + i);
i++;
}
This project demonstrates how to build a fully functional To-Do List using HTML, CSS, and
JavaScript. It covers essential web development skills including DOM manipulation, event
handling, and localStorage.
Project Structure
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>To-Do List App</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>To-Do List</h1>
<input type="text" id="taskInput" placeholder="Add a new task">
<button id="addTask">Add Task</button>
<ul id="taskList"></ul>
</div>
<script src="script.js"></script>
</body>
</html>
style.css
body {
font-family: Arial, sans-serif;
background: #f4f4f4;
display: flex;
justify-content: center;
padding: 50px;
}
.container {
background: #fff;
padding: 30px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
width: 300px;
}
input {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
width: 100%;
padding: 10px;
background: #28a745;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
ul {
list-style: none;
padding: 0;
}
li {
padding: 10px;
border-bottom: 1px solid #ddd;
display: flex;
justify-content: space-between;
align-items: center;
}
li.completed {
text-decoration: line-through;
color: #888;
}
script.js
document.addEventListener("DOMContentLoaded", () => {
const taskInput = document.getElementById("taskInput");
const addTask = document.getElementById("addTask");
const taskList = document.getElementById("taskList");
addTask.addEventListener("click", () => {
const text = taskInput.value.trim();
if (text !== "") {
renderTask(text);
tasks.push({ text, completed: false });
localStorage.setItem("tasks", JSON.stringify(tasks));
taskInput.value = "";
}
});
li.addEventListener("click", () => {
li.classList.toggle("completed");
tasks = tasks.map(t => t.text === text ? { ...t, completed: !
t.completed } : t);
localStorage.setItem("tasks", JSON.stringify(tasks));
});
li.appendChild(removeBtn);
taskList.appendChild(li);
}
});
Chapter 1: Introduction to JavaScript
<!DOCTYPE html>
<html>
<head>
<title>JS Example</title>
</head>
<body>
<h1 id="heading">Hello World</h1>
<button onclick="changeText()">Click Me</button>
<script>
function changeText() {
document.getElementById("heading").innerHTML = "Text
Changed!";
}
</script>
</body>
</html>
Chapter 2: JavaScript Syntax, Variables, and Data Types
Understanding the basic syntax and structure of JavaScript is crucial. In this chapter, you'll
learn how to declare variables, use different data types, and write syntactically correct
JavaScript code.
Variables
JavaScript uses three main keywords for variable declaration: var, let, and const.
- `var` was traditionally used, but it's now recommended to use `let` or `const`.
- `let` allows variable reassignment.
- `const` defines a constant, which cannot be reassigned.
Example:
Data Types
Example:
Functions are reusable blocks of code that perform a specific task. Scope refers to the
context in which variables are accessible. Understanding how functions and scope work is
essential to writing clean and effective JavaScript.
1. **Function Declaration**
function greet(name) {
return "Hello, " + name + "!";
}
greet("Alice");
2. **Function Expression**
Scope in JavaScript
Scope determines the visibility of variables. There are two types of scope:
function showScope() {
let localVar = "I'm local";
console.log(globalVar);
console.log(localVar);
}
showScope();
console.log(localVar); // Error: localVar is not defined
Chapter 4: DOM Manipulation and Events
The Document Object Model (DOM) allows JavaScript to access and modify HTML content
dynamically. You can use JavaScript to change the structure, style, and content of a web
page in response to user actions.
- `getElementById()`
- `getElementsByClassName()`
- `querySelector()`
- `querySelectorAll()`
Example:
Example:
Handling Events
JavaScript can respond to user events like clicks, typing, and form submissions.
Example:
Objects and arrays are fundamental data structures in JavaScript. They allow you to group
related data and manipulate collections efficiently.
Objects
Objects store data in key-value pairs. Each key is a string, and the value can be any type.
Example:
const person = {
name: "Alice",
age: 30,
isStudent: false
};
console.log(person.name); // Alice
Modifying Objects
Example:
person.age = 31;
person.city = "New York";
delete person.isStudent;
Arrays
Arrays are ordered collections of items. You can access elements using index numbers.
Example:
Array Methods
Example:
fruits.push("Orange");
fruits.forEach(function(fruit) {
console.log(fruit);
});
Chapter 6: Conditional Statements and Loops
Conditional statements and loops allow you to control the flow of your code. Conditions
decide what code runs, while loops allow you to repeat blocks of code.
Conditional Statements
- `if`
- `else if`
- `else`
- `switch`
Example (if-else):
Example (switch):
- `for` loop
- `while` loop
- `do...while` loop
- `for...of` and `for...in` loops
let i = 0;
while (i < 3) {
console.log("i is " + i);
i++;
}
This project demonstrates how to build a fully functional To-Do List using HTML, CSS, and
JavaScript. It covers essential web development skills including DOM manipulation, event
handling, and localStorage.
Project Structure
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>To-Do List App</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>To-Do List</h1>
<input type="text" id="taskInput" placeholder="Add a new task">
<button id="addTask">Add Task</button>
<ul id="taskList"></ul>
</div>
<script src="script.js"></script>
</body>
</html>
style.css
body {
font-family: Arial, sans-serif;
background: #f4f4f4;
display: flex;
justify-content: center;
padding: 50px;
}
.container {
background: #fff;
padding: 30px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
width: 300px;
}
input {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
width: 100%;
padding: 10px;
background: #28a745;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
ul {
list-style: none;
padding: 0;
}
li {
padding: 10px;
border-bottom: 1px solid #ddd;
display: flex;
justify-content: space-between;
align-items: center;
}
li.completed {
text-decoration: line-through;
color: #888;
}
script.js
document.addEventListener("DOMContentLoaded", () => {
const taskInput = document.getElementById("taskInput");
const addTask = document.getElementById("addTask");
const taskList = document.getElementById("taskList");
addTask.addEventListener("click", () => {
const text = taskInput.value.trim();
if (text !== "") {
renderTask(text);
tasks.push({ text, completed: false });
localStorage.setItem("tasks", JSON.stringify(tasks));
taskInput.value = "";
}
});
li.addEventListener("click", () => {
li.classList.toggle("completed");
tasks = tasks.map(t => t.text === text ? { ...t, completed: !
t.completed } : t);
localStorage.setItem("tasks", JSON.stringify(tasks));
});
li.appendChild(removeBtn);
taskList.appendChild(li);
}
});
Chapter 8: Introduction to SQL
SQL (Structured Query Language) is used to manage and manipulate relational databases. It
allows you to query, insert, update, and delete data using simple and powerful syntax.
A basic SQL statement includes a command followed by the target table and conditions.
Example:
<!DOCTYPE html>
<html>
<head>
<title>JS Example</title>
</head>
<body>
<h1 id="heading">Hello World</h1>
<button onclick="changeText()">Click Me</button>
<script>
function changeText() {
document.getElementById("heading").innerHTML = "Text
Changed!";
}
</script>
</body>
</html>
Chapter 2: JavaScript Syntax, Variables, and Data Types
Understanding the basic syntax and structure of JavaScript is crucial. In this chapter, you'll
learn how to declare variables, use different data types, and write syntactically correct
JavaScript code.
Variables
JavaScript uses three main keywords for variable declaration: var, let, and const.
- `var` was traditionally used, but it's now recommended to use `let` or `const`.
- `let` allows variable reassignment.
- `const` defines a constant, which cannot be reassigned.
Example:
Data Types
Example:
Functions are reusable blocks of code that perform a specific task. Scope refers to the
context in which variables are accessible. Understanding how functions and scope work is
essential to writing clean and effective JavaScript.
1. **Function Declaration**
function greet(name) {
return "Hello, " + name + "!";
}
greet("Alice");
2. **Function Expression**
Scope in JavaScript
Scope determines the visibility of variables. There are two types of scope:
function showScope() {
let localVar = "I'm local";
console.log(globalVar);
console.log(localVar);
}
showScope();
console.log(localVar); // Error: localVar is not defined
Chapter 4: DOM Manipulation and Events
The Document Object Model (DOM) allows JavaScript to access and modify HTML content
dynamically. You can use JavaScript to change the structure, style, and content of a web
page in response to user actions.
- `getElementById()`
- `getElementsByClassName()`
- `querySelector()`
- `querySelectorAll()`
Example:
Example:
Handling Events
JavaScript can respond to user events like clicks, typing, and form submissions.
Example:
Objects and arrays are fundamental data structures in JavaScript. They allow you to group
related data and manipulate collections efficiently.
Objects
Objects store data in key-value pairs. Each key is a string, and the value can be any type.
Example:
const person = {
name: "Alice",
age: 30,
isStudent: false
};
console.log(person.name); // Alice
Modifying Objects
Example:
person.age = 31;
person.city = "New York";
delete person.isStudent;
Arrays
Arrays are ordered collections of items. You can access elements using index numbers.
Example:
Array Methods
Example:
fruits.push("Orange");
fruits.forEach(function(fruit) {
console.log(fruit);
});
Chapter 6: Conditional Statements and Loops
Conditional statements and loops allow you to control the flow of your code. Conditions
decide what code runs, while loops allow you to repeat blocks of code.
Conditional Statements
- `if`
- `else if`
- `else`
- `switch`
Example (if-else):
Example (switch):
- `for` loop
- `while` loop
- `do...while` loop
- `for...of` and `for...in` loops
let i = 0;
while (i < 3) {
console.log("i is " + i);
i++;
}
This project demonstrates how to build a fully functional To-Do List using HTML, CSS, and
JavaScript. It covers essential web development skills including DOM manipulation, event
handling, and localStorage.
Project Structure
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>To-Do List App</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>To-Do List</h1>
<input type="text" id="taskInput" placeholder="Add a new task">
<button id="addTask">Add Task</button>
<ul id="taskList"></ul>
</div>
<script src="script.js"></script>
</body>
</html>
style.css
body {
font-family: Arial, sans-serif;
background: #f4f4f4;
display: flex;
justify-content: center;
padding: 50px;
}
.container {
background: #fff;
padding: 30px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
width: 300px;
}
input {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
width: 100%;
padding: 10px;
background: #28a745;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
ul {
list-style: none;
padding: 0;
}
li {
padding: 10px;
border-bottom: 1px solid #ddd;
display: flex;
justify-content: space-between;
align-items: center;
}
li.completed {
text-decoration: line-through;
color: #888;
}
script.js
document.addEventListener("DOMContentLoaded", () => {
const taskInput = document.getElementById("taskInput");
const addTask = document.getElementById("addTask");
const taskList = document.getElementById("taskList");
addTask.addEventListener("click", () => {
const text = taskInput.value.trim();
if (text !== "") {
renderTask(text);
tasks.push({ text, completed: false });
localStorage.setItem("tasks", JSON.stringify(tasks));
taskInput.value = "";
}
});
li.addEventListener("click", () => {
li.classList.toggle("completed");
tasks = tasks.map(t => t.text === text ? { ...t, completed: !
t.completed } : t);
localStorage.setItem("tasks", JSON.stringify(tasks));
});
li.appendChild(removeBtn);
taskList.appendChild(li);
}
});
Chapter 8: Introduction to SQL
SQL (Structured Query Language) is used to manage and manipulate relational databases. It
allows you to query, insert, update, and delete data using simple and powerful syntax.
A basic SQL statement includes a command followed by the target table and conditions.
Example:
In this chapter, we explore SQL queries in greater detail. SQL allows you to filter, sort, and
retrieve data precisely according to your needs.
Example:
Example:
Comparison Operators
- `=` : Equal to
- `!=` or `<>` : Not equal to
- `>` : Greater than
- `<` : Less than
- `>=`, `<=` : Greater than or equal to, less than or equal to
Logical Operators
Example:
ORDER BY Clause
Example:
<!DOCTYPE html>
<html>
<head>
<title>JS Example</title>
</head>
<body>
<h1 id="heading">Hello World</h1>
<button onclick="changeText()">Click Me</button>
<script>
function changeText() {
document.getElementById("heading").innerHTML = "Text
Changed!";
}
</script>
</body>
</html>
Chapter 2: JavaScript Syntax, Variables, and Data Types
Understanding the basic syntax and structure of JavaScript is crucial. In this chapter, you'll
learn how to declare variables, use different data types, and write syntactically correct
JavaScript code.
Variables
JavaScript uses three main keywords for variable declaration: var, let, and const.
- `var` was traditionally used, but it's now recommended to use `let` or `const`.
- `let` allows variable reassignment.
- `const` defines a constant, which cannot be reassigned.
Example:
Data Types
Example:
Functions are reusable blocks of code that perform a specific task. Scope refers to the
context in which variables are accessible. Understanding how functions and scope work is
essential to writing clean and effective JavaScript.
1. **Function Declaration**
function greet(name) {
return "Hello, " + name + "!";
}
greet("Alice");
2. **Function Expression**
Scope in JavaScript
Scope determines the visibility of variables. There are two types of scope:
function showScope() {
let localVar = "I'm local";
console.log(globalVar);
console.log(localVar);
}
showScope();
console.log(localVar); // Error: localVar is not defined
Chapter 4: DOM Manipulation and Events
The Document Object Model (DOM) allows JavaScript to access and modify HTML content
dynamically. You can use JavaScript to change the structure, style, and content of a web
page in response to user actions.
- `getElementById()`
- `getElementsByClassName()`
- `querySelector()`
- `querySelectorAll()`
Example:
Example:
Handling Events
JavaScript can respond to user events like clicks, typing, and form submissions.
Example:
Objects and arrays are fundamental data structures in JavaScript. They allow you to group
related data and manipulate collections efficiently.
Objects
Objects store data in key-value pairs. Each key is a string, and the value can be any type.
Example:
const person = {
name: "Alice",
age: 30,
isStudent: false
};
console.log(person.name); // Alice
Modifying Objects
Example:
person.age = 31;
person.city = "New York";
delete person.isStudent;
Arrays
Arrays are ordered collections of items. You can access elements using index numbers.
Example:
Array Methods
Example:
fruits.push("Orange");
fruits.forEach(function(fruit) {
console.log(fruit);
});
Chapter 6: Conditional Statements and Loops
Conditional statements and loops allow you to control the flow of your code. Conditions
decide what code runs, while loops allow you to repeat blocks of code.
Conditional Statements
- `if`
- `else if`
- `else`
- `switch`
Example (if-else):
Example (switch):
- `for` loop
- `while` loop
- `do...while` loop
- `for...of` and `for...in` loops
let i = 0;
while (i < 3) {
console.log("i is " + i);
i++;
}
This project demonstrates how to build a fully functional To-Do List using HTML, CSS, and
JavaScript. It covers essential web development skills including DOM manipulation, event
handling, and localStorage.
Project Structure
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>To-Do List App</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>To-Do List</h1>
<input type="text" id="taskInput" placeholder="Add a new task">
<button id="addTask">Add Task</button>
<ul id="taskList"></ul>
</div>
<script src="script.js"></script>
</body>
</html>
style.css
body {
font-family: Arial, sans-serif;
background: #f4f4f4;
display: flex;
justify-content: center;
padding: 50px;
}
.container {
background: #fff;
padding: 30px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
width: 300px;
}
input {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
width: 100%;
padding: 10px;
background: #28a745;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
ul {
list-style: none;
padding: 0;
}
li {
padding: 10px;
border-bottom: 1px solid #ddd;
display: flex;
justify-content: space-between;
align-items: center;
}
li.completed {
text-decoration: line-through;
color: #888;
}
script.js
document.addEventListener("DOMContentLoaded", () => {
const taskInput = document.getElementById("taskInput");
const addTask = document.getElementById("addTask");
const taskList = document.getElementById("taskList");
addTask.addEventListener("click", () => {
const text = taskInput.value.trim();
if (text !== "") {
renderTask(text);
tasks.push({ text, completed: false });
localStorage.setItem("tasks", JSON.stringify(tasks));
taskInput.value = "";
}
});
li.addEventListener("click", () => {
li.classList.toggle("completed");
tasks = tasks.map(t => t.text === text ? { ...t, completed: !
t.completed } : t);
localStorage.setItem("tasks", JSON.stringify(tasks));
});
li.appendChild(removeBtn);
taskList.appendChild(li);
}
});
Chapter 8: Introduction to SQL
SQL (Structured Query Language) is used to manage and manipulate relational databases. It
allows you to query, insert, update, and delete data using simple and powerful syntax.
A basic SQL statement includes a command followed by the target table and conditions.
Example:
In this chapter, we explore SQL queries in greater detail. SQL allows you to filter, sort, and
retrieve data precisely according to your needs.
Example:
Example:
Comparison Operators
- `=` : Equal to
- `!=` or `<>` : Not equal to
- `>` : Greater than
- `<` : Less than
- `>=`, `<=` : Greater than or equal to, less than or equal to
Logical Operators
Example:
ORDER BY Clause
Example:
Joins are used in SQL to combine rows from two or more tables, based on a related column
between them. Understanding joins is essential for managing relationships in relational
databases.
Types of Joins
Table: `users`
| id | name |
|----|----------|
| 1 | Alice |
| 2 | Bob |
Table: `orders`
| id | user_id | product |
|----|---------|-------------|
|1 |1 | Laptop |
|2 |2 | Smartphone |
|3 |1 | Mouse |
Example:
<!DOCTYPE html>
<html>
<head>
<title>JS Example</title>
</head>
<body>
<h1 id="heading">Hello World</h1>
<button onclick="changeText()">Click Me</button>
<script>
function changeText() {
document.getElementById("heading").innerHTML = "Text
Changed!";
}
</script>
</body>
</html>
Chapter 2: JavaScript Syntax, Variables, and Data Types
Understanding the basic syntax and structure of JavaScript is crucial. In this chapter, you'll
learn how to declare variables, use different data types, and write syntactically correct
JavaScript code.
Variables
JavaScript uses three main keywords for variable declaration: var, let, and const.
- `var` was traditionally used, but it's now recommended to use `let` or `const`.
- `let` allows variable reassignment.
- `const` defines a constant, which cannot be reassigned.
Example:
Data Types
Example:
Functions are reusable blocks of code that perform a specific task. Scope refers to the
context in which variables are accessible. Understanding how functions and scope work is
essential to writing clean and effective JavaScript.
1. **Function Declaration**
function greet(name) {
return "Hello, " + name + "!";
}
greet("Alice");
2. **Function Expression**
Scope in JavaScript
Scope determines the visibility of variables. There are two types of scope:
function showScope() {
let localVar = "I'm local";
console.log(globalVar);
console.log(localVar);
}
showScope();
console.log(localVar); // Error: localVar is not defined
Chapter 4: DOM Manipulation and Events
The Document Object Model (DOM) allows JavaScript to access and modify HTML content
dynamically. You can use JavaScript to change the structure, style, and content of a web
page in response to user actions.
- `getElementById()`
- `getElementsByClassName()`
- `querySelector()`
- `querySelectorAll()`
Example:
Example:
Handling Events
JavaScript can respond to user events like clicks, typing, and form submissions.
Example:
Objects and arrays are fundamental data structures in JavaScript. They allow you to group
related data and manipulate collections efficiently.
Objects
Objects store data in key-value pairs. Each key is a string, and the value can be any type.
Example:
const person = {
name: "Alice",
age: 30,
isStudent: false
};
console.log(person.name); // Alice
Modifying Objects
Example:
person.age = 31;
person.city = "New York";
delete person.isStudent;
Arrays
Arrays are ordered collections of items. You can access elements using index numbers.
Example:
Array Methods
Example:
fruits.push("Orange");
fruits.forEach(function(fruit) {
console.log(fruit);
});
Chapter 6: Conditional Statements and Loops
Conditional statements and loops allow you to control the flow of your code. Conditions
decide what code runs, while loops allow you to repeat blocks of code.
Conditional Statements
- `if`
- `else if`
- `else`
- `switch`
Example (if-else):
Example (switch):
- `for` loop
- `while` loop
- `do...while` loop
- `for...of` and `for...in` loops
let i = 0;
while (i < 3) {
console.log("i is " + i);
i++;
}
This project demonstrates how to build a fully functional To-Do List using HTML, CSS, and
JavaScript. It covers essential web development skills including DOM manipulation, event
handling, and localStorage.
Project Structure
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>To-Do List App</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>To-Do List</h1>
<input type="text" id="taskInput" placeholder="Add a new task">
<button id="addTask">Add Task</button>
<ul id="taskList"></ul>
</div>
<script src="script.js"></script>
</body>
</html>
style.css
body {
font-family: Arial, sans-serif;
background: #f4f4f4;
display: flex;
justify-content: center;
padding: 50px;
}
.container {
background: #fff;
padding: 30px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
width: 300px;
}
input {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
width: 100%;
padding: 10px;
background: #28a745;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
ul {
list-style: none;
padding: 0;
}
li {
padding: 10px;
border-bottom: 1px solid #ddd;
display: flex;
justify-content: space-between;
align-items: center;
}
li.completed {
text-decoration: line-through;
color: #888;
}
script.js
document.addEventListener("DOMContentLoaded", () => {
const taskInput = document.getElementById("taskInput");
const addTask = document.getElementById("addTask");
const taskList = document.getElementById("taskList");
addTask.addEventListener("click", () => {
const text = taskInput.value.trim();
if (text !== "") {
renderTask(text);
tasks.push({ text, completed: false });
localStorage.setItem("tasks", JSON.stringify(tasks));
taskInput.value = "";
}
});
li.addEventListener("click", () => {
li.classList.toggle("completed");
tasks = tasks.map(t => t.text === text ? { ...t, completed: !
t.completed } : t);
localStorage.setItem("tasks", JSON.stringify(tasks));
});
li.appendChild(removeBtn);
taskList.appendChild(li);
}
});
Chapter 8: Introduction to SQL
SQL (Structured Query Language) is used to manage and manipulate relational databases. It
allows you to query, insert, update, and delete data using simple and powerful syntax.
A basic SQL statement includes a command followed by the target table and conditions.
Example:
In this chapter, we explore SQL queries in greater detail. SQL allows you to filter, sort, and
retrieve data precisely according to your needs.
Example:
Example:
Comparison Operators
- `=` : Equal to
- `!=` or `<>` : Not equal to
- `>` : Greater than
- `<` : Less than
- `>=`, `<=` : Greater than or equal to, less than or equal to
Logical Operators
Example:
ORDER BY Clause
Example:
Joins are used in SQL to combine rows from two or more tables, based on a related column
between them. Understanding joins is essential for managing relationships in relational
databases.
Types of Joins
Table: `users`
| id | name |
|----|----------|
| 1 | Alice |
| 2 | Bob |
Table: `orders`
| id | user_id | product |
|----|---------|-------------|
|1 |1 | Laptop |
|2 |2 | Smartphone |
|3 |1 | Mouse |
Example:
Advanced SQL concepts help you write more powerful and efficient queries. This chapter
introduces `GROUP BY`, `HAVING`, subqueries, and indexing techniques.
GROUP BY Clause
`GROUP BY` groups rows that have the same values into summary rows. It's commonly used
with aggregate functions like COUNT, SUM, AVG, etc.
Example:
HAVING Clause
`HAVING` is used to filter records after grouping (similar to WHERE but for groups).
Example:
Subqueries
Indexes
Indexes speed up data retrieval operations. They work like an index in a book.
Example:
While indexes improve read performance, they may slow down write operations like
INSERT or UPDATE.