#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JavaScript Fundamentals

Introduction to JavaScript

JavaScript is the programming language of the web. HTML provides a page's structure, CSS controls its look, and JavaScript makes it respond: showing a menu when you tap a button, checking a form before it is sent, loading new data without a reload, or running a whole app in the browser. It also runs outside browsers, for example on servers with Node.js.

What JavaScript can do on a page

  • Change the content and styles of any element through the DOM.
  • React to user actions such as clicks, key presses and scrolling.
  • Validate form input before it reaches the server.
  • Fetch data from APIs in the background and update part of the page.
  • Store small amounts of data in the browser with localStorage.
  • Draw graphics and animations with canvas and other browser APIs.

Adding JavaScript to a page

Scripts go in a script element. You can write code inline, but for anything beyond a few lines put it in a separate .js file and link it with the src attribute. Adding defer tells the browser to download the file in the background and run it only after the HTML has been parsed, so your code can safely find elements on the page.

<!-- index.html -->
<head>
  <script src="app.js" defer></script>
</head>
<body>
  <button id="likeBtn">Like</button>
  <p id="count">0 likes</p>
</body>

// app.js
const button = document.getElementById("likeBtn");
const countText = document.getElementById("count");
let likes = 0;

button.addEventListener("click", () => {
  likes += 1;
  countText.textContent = `${likes} like${likes === 1 ? "" : "s"}`;
});

console.log("Script loaded");

Trying code in the console

Every modern browser has developer tools, usually opened with F12 or by right-clicking and choosing Inspect. The Console tab lets you type JavaScript and see the result immediately, which is perfect for experiments. It also shows errors from your scripts in red, with the file name and line number. Messages from console.log appear there too, making it the first place to look when something does not work.

Core ideas you will meet next

JavaScript is dynamically typed: a variable does not have a fixed type, and the same variable can hold a number now and a string later. You declare variables with let (can be reassigned) or const (cannot be reassigned). Code is organised into functions, and the browser runs your code in response to events. Statements usually end with semicolons; JavaScript can insert many of them automatically, but writing them yourself avoids surprises.

The script that cannot find its elements

The most common first error is "Cannot read properties of null (reading 'addEventListener')". It happens when a script in the head runs before the browser has created the button further down. getElementById returns null, and calling a method on null fails. The fix is to use defer, as in the example, or to place the script tag just before the closing body tag.

A second early confusion is between Java and JavaScript. Despite the name, they are separate languages with different purposes and designs. You do not need to know Java to learn JavaScript.

Where JavaScript runs

Inside a browser, an engine such as V8 (Chrome, Edge), SpiderMonkey (Firefox) or JavaScriptCore (Safari) executes your code. The language itself is the same everywhere, but the available tools differ: browsers provide document and window, while Node.js provides file system and network modules instead.

Takeaway: JavaScript adds behaviour to HTML and CSS, belongs in an external file loaded with defer, and is best explored hands-on in the browser console, where errors also appear.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course