In this article · 12 sections
After HTML and CSS, the next step is JavaScript, the language that makes web pages interactive: menus that open, forms that validate, prices that update, content that loads without refreshing. These JavaScript basics for beginners focus on what you'll actually use on websites.
1. Where JavaScript goes
<script src="/js/main.js" defer></script>
defer loads the script without blocking the page and runs it after the HTML is parsed.
2. Variables
const siteName = "Joboo Web"; // won't be reassigned
let count = 0; // can change
3. Functions
function formatPrice(amount) {
return "₹" + amount.toLocaleString("en-IN");
}
formatPrice(14999); // "₹14,999"
4. Select elements
const btn = document.querySelector(".menu-btn");
const items = document.querySelectorAll(".faq-item");
5. Events
btn.addEventListener("click", () => {
document.querySelector(".nav").classList.toggle("open");
});
This is the core of a mobile menu. See responsive navbar tutorial.
6. Change content and styles
document.querySelector("#total").textContent = formatPrice(4999);
document.querySelector(".alert").classList.add("show");
7. Loops and arrays
const cities = ["Ludhiana", "Jalandhar", "Amritsar"];
cities.forEach(city => console.log(city));
8. Simple form validation
const form = document.querySelector("#enquiry");
form.addEventListener("submit", (e) => {
const phone = form.phone.value.trim();
if (!/^[6-9]\d{9}$/.test(phone)) {
e.preventDefault();
alert("Please enter a valid 10-digit mobile number");
}
});
Always validate on the server too. See form design best practices.
9. Fetch data without reloading
const res = await fetch("/api/rates?city=ludhiana");
const data = await res.json();
10. Debug with DevTools
Open DevTools (F12) → Console to see errors and console.log() output. Most beginner problems show up here.
Good habits
- Keep JavaScript small; heavy scripts slow pages. See reduce unused JavaScript
- Make features work without JavaScript where possible (forms, links)
- Never trust user input on the client side alone
- Use clear names and comments
FAQs
Should I learn jQuery?
Modern JavaScript covers most jQuery use cases. Learn plain JavaScript first; you'll still recognise jQuery on older sites.
What should I learn after the basics?
DOM events in depth, fetch/async, then a framework if your projects need one.
Joboo Web builds fast sites with lightweight, purposeful JavaScript. See what we build.