🌙
Happy Karwa Chauth! Wishing all couples a blessed Karwa Chauth. May your bond grow stronger every year. From Team Job...
💬 Share
Web Development Tutorials

JavaScript Basics for Beginners: Variables, Functions, Arrays and the DOM

JavaScript makes web pages interactive. Learn JavaScript basics step by step with short examples and a mini fare-calculator project.

In this article · 10 sections
  1. Where JavaScript goes
  2. Variables
  3. Data types
  4. Functions
  5. Conditions and loops
  6. Arrays: useful methods
  7. The DOM: changing the page
  8. Debugging
  9. Next steps
  10. FAQs
  11. Is JavaScript the same as Java?
  12. Should I learn jQuery?

HTML builds the structure, CSS styles it, and JavaScript makes it interactive. These JavaScript basics cover what you need to start adding real features to websites: calculations, form checks, menus and live content.

Where JavaScript goes

<script src="/js/app.js" defer></script>

defer loads the file without blocking the page and runs it after the HTML is parsed.

Variables

const city = "Ludhiana";   // can't be reassigned
let fare = 3500;           // can change
fare = fare + 200;

Use const by default and let when the value changes. Avoid the older var.

Data types

const name = "Joboo";        // string
const km = 310;              // number
const isAC = true;           // boolean
const cars = ["Dzire", "Innova"];          // array
const booking = { from: "Ludhiana", to: "Delhi", km: 310 }; // object

Functions

function calcFare(km, ratePerKm) {
  return km * ratePerKm;
}
const total = calcFare(310, 12); // 3720

Arrow function version:

const calcFare = (km, rate) => km * rate;

Conditions and loops

if (km > 250) {
  console.log("Long trip");
} else {
  console.log("Short trip");
}

for (const car of cars) {
  console.log(car);
}

Arrays: useful methods

const prices = [2500, 3200, 4100];
const withToll = prices.map(p => p + 300);
const cheap = prices.filter(p => p < 3500);

The DOM: changing the page

const btn = document.querySelector("#calc");
const out = document.querySelector("#result");

btn.addEventListener("click", () => {
  const km = Number(document.querySelector("#km").value);
  out.textContent = "Estimated fare: ₹" + calcFare(km, 12);
});
<input id="km" type="number" placeholder="Distance in km">
<button id="calc" type="button">Calculate</button>
<p id="result"></p>

Use textContent (not innerHTML) when inserting user input, to avoid XSS.

Debugging

Open DevTools (F12) → Console to see errors and use console.log(). See Chrome DevTools guide.

Next steps

FAQs

Is JavaScript the same as Java?

No. They're different languages with different uses.

Should I learn jQuery?

It's useful for maintaining older sites, but modern JavaScript covers most of what jQuery was used for.

Was this article helpful?

Your answer helps us improve our guides

Share this article

Joboo Web

Web design, SEO & digital marketing team · Ludhiana, Punjab

Joboo Web has helped Indian businesses grow online since 2019 — building fast websites and running SEO, Google Ads and social campaigns for clients in 80+ cities. About us →

Keep reading

💬