JavaScript for Beginners: Variables, Functions & the DOM
JavaScript makes websites interactive: buttons that respond, forms that check input and content that updates without reloading. It's also the language behind React and Node.js.
By Learn Refill Mentors · Updated
Why learn JavaScript?
- The only language that runs in every browser
- Used for frontend (React) and backend (Node.js)
- Most in-demand skill for web developer jobs
1Store data in variables
Use const for values that don't change and let for values that do. Avoid the old var.
const name = "Aman";
let score = 10;
score = score + 5;
console.log(name, score); // Aman 152Make decisions with if / else
Conditions let your code choose what to do.
const marks = 92;
if (marks >= 90) {
console.log("Certificate unlocked!");
} else {
console.log("Keep practising");
}3Reuse code with functions
A function is a named block of code you can call again and again. Arrow functions are the modern short form.
const add = (a, b) => a + b;
console.log(add(2, 3)); // 54Work with arrays and objects
Arrays hold lists, objects hold related data. map and filter are the most useful array methods.
const skills = ["HTML", "CSS", "JS"];
const upper = skills.map((s) => s.toUpperCase());
const student = { name: "Riya", course: "MERN" };
console.log(student.course); // MERN5Make the page interactive (DOM + events)
Select an element, listen for a click and change the page.
<button id="btn">Click me</button>
<p id="msg"></p>
<script>
const btn = document.getElementById("btn");
btn.addEventListener("click", () => {
document.getElementById("msg").textContent = "You clicked!";
});
</script>Common mistakes
- Using == instead of === (=== also checks the type)
- Changing a const variable
- Running DOM code before the element exists on the page
Practice task
Build a counter app with + and − buttons, and a to-do list where you can add and delete items.
Frequently asked questions
Is JavaScript the same as Java?
No. They are completely different languages with a similar name.
Should I learn JavaScript before React?
Yes. React is JavaScript — strong JS basics make React much easier.