JavaScript Fundamentals for Beginners: Variables, Functions, DOM Manipulation & Events
Learn JavaScript from scratch—variables, functions, DOM manipulation, events, and build an interactive mini-project with copy-pasteable code.
Introduction: Why JavaScript?
JavaScript is the language of the web. It makes pages interactive—handling clicks, validating forms, updating content without reloading. Every modern web app relies on JavaScript.
1. Variables: Storing Data
JavaScript has three ways to declare variables: var, let, and const. Modern code uses let and const.
let name = "Amina";
const age = 18;
var isStudent = true;
console.log(name); // Amina
console.log(age); // 18
Data Types
- String — text:
"hello" - Number —
42,3.14 - Boolean —
true/false - Array —
[1, 2, 3] - Object —
{ name: "Amina", age: 18 }
2. Functions: Reusable Logic
function greet(name) {
return "Hello, " + name + "!";
}
console.log(greet("Rahim")); // Hello, Rahim!
// Arrow function (modern)
const add = (a, b) => a + b;
console.log(add(3, 5)); // 8
3. Conditionals and Loops
let score = 75;
if (score >= 80) {
console.log("Excellent");
} else if (score >= 60) {
console.log("Good");
} else {
console.log("Needs improvement");
}
for (let i = 1; i <= 5; i++) {
console.log("Count: " + i);
}
4. DOM Manipulation: Changing the Page
The DOM (Document Object Model) represents the page. JavaScript can read and modify it.
<h1 id="title">Original Title</h1>
<button id="changeBtn">Change Title</button>
<script>
const title = document.getElementById("title");
const btn = document.getElementById("changeBtn");
btn.addEventListener("click", () => {
title.textContent = "Title Changed!";
});
</script>
Adding Elements Dynamically
const list = document.getElementById("list");
const item = document.createElement("li");
item.textContent = "New item";
list.appendChild(item);
5. Events: Responding to User Actions
Events include clicks, keypresses, mouse movements, and form submissions.
const input = document.getElementById("nameInput");
input.addEventListener("input", (event) => {
console.log("User typed: " + event.target.value);
});
6. Mini-Project: Interactive To-Do List
Build a simple to-do app that adds and removes tasks.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>To-Do List</title>
</head>
<body>
<input id="taskInput" placeholder="Enter a task">
<button id="addBtn">Add</button>
<ul id="taskList"></ul>
<script>
const input = document.getElementById("taskInput");
const addBtn = document.getElementById("addBtn");
const list = document.getElementById("taskList");
addBtn.addEventListener("click", () => {
const text = input.value.trim();
if (text === "") return;
const li = document.createElement("li");
li.textContent = text;
const removeBtn = document.createElement("button");
removeBtn.textContent = " Remove";
removeBtn.addEventListener("click", () => li.remove());
li.appendChild(removeBtn);
list.appendChild(li);
input.value = "";
});
</script>
</body>
</html>
7. Debugging Tips
- Use
console.log()frequently. - Open DevTools (F12) to inspect errors.
- Check the Console tab for messages.
- Use
debugger;to pause execution.
Conclusion
You now know variables, functions, DOM manipulation, and events. The to-do list is a real interactive project—extend it with localStorage or editing features. Next, we will connect front-end code to a backend with PHP and MySQL.
Ready to Launch Your Website Online?
Once you finish building your project locally, you need fast and reliable hosting to put it online. Students and developers can get an automatic 20% discount (code: AR10-5) on their first order at Hostnin NVMe Web Hosting (plans start from ৳149/mo with free SSL and bKash/Nagad support), or view full details in our Eduvalle Student Shop.
Whether you are launching an online store, agency, or portfolio without coding from scratch, explore finished, mobile-ready websites with free full setup and 100% code ownership at Nowvelle.
Eduvalle-এর একাডেমিক টিম: HSC ও ভর্তি পরীক্ষার প্রস্তুতির জন্য যাচাইকৃত নোট ও কুইজ।