What Is JavaScript?
JavaScript is a programming language that adds behavior and logic to websites.
HTML creates the content and structure of a page. CSS controls how that page looks. JavaScript lets the page respond when somebody clicks, types, submits a form, changes a value or performs another action.
For example, imagine an online shopping page.
HTML can create an Add to Cart button. CSS can change its color and size. JavaScript can increase the cart count when somebody clicks it.
That is the basic role of JavaScript in frontend web development.
What You Will Learn
By the end of this lesson, you will understand:
- what JavaScript is
- why websites use JavaScript
- how HTML, CSS and JavaScript work together
- how JavaScript runs in a browser
- what JavaScript can change on a webpage
- how to add JavaScript to HTML
- how to run your first JavaScript code
- where JavaScript is used outside normal webpages
- what to learn next
You do not need previous programming experience for this lesson.
Basic HTML and CSS knowledge will make the examples easier to understand.
What Is JavaScript?
JavaScript is a programming language used to add programming logic and interactivity to websites and applications.
Without JavaScript, a basic webpage can still contain:
- headings
- paragraphs
- images
- links
- forms
- buttons
But those elements may have limited behavior.
JavaScript lets you decide what should happen when users interact with them.
For example:
<button id="welcomeButton">Show Message</button>
<p id="message"></p>This HTML creates a button and an empty paragraph.
Now add JavaScript:
const button = document.querySelector("#welcomeButton");
const message = document.querySelector("#message");
button.addEventListener("click", () => {
message.textContent = "Welcome to JavaScript!";
});When the user clicks the button, the paragraph changes to:
Welcome to JavaScript!The webpage did not need to reload.
JavaScript reacted to the click and changed the page.
HTML, CSS and JavaScript Have Different Jobs
A useful way to understand frontend development is to separate the jobs of HTML, CSS and JavaScript.
| Technology | Main job | Example |
|---|---|---|
| HTML | Creates content and structure | Button, heading, image |
| CSS | Controls appearance and layout | Color, spacing, size |
| JavaScript | Adds behavior and logic | Clicks, updates, validation |
Consider this button:
<button id="buyButton">Buy Now</button>HTML creates it.
CSS can style it:
#buyButton {
padding: 12px 20px;
background: green;
color: white;
}JavaScript can make it react:
const buyButton = document.querySelector("#buyButton");
buyButton.addEventListener("click", () => {
console.log("Buy button clicked");
});The three technologies work together.
You do not normally choose between HTML, CSS and JavaScript. A modern frontend often uses all three.
A Real Website Example
Suppose you are building an ecommerce product page.
HTML may contain:
- product name
- product image
- price
- quantity field
- Add to Cart button
CSS controls how those elements look.
JavaScript can handle behavior such as:
- increasing quantity
- calculating a new total
- checking stock
- opening product images
- showing a cart message
- updating the cart count
For example:
<button id="addButton">Add to Cart</button>
<p id="cartMessage"></p>const addButton = document.querySelector("#addButton");
const cartMessage = document.querySelector("#cartMessage");
addButton.addEventListener("click", () => {
cartMessage.textContent = "Product added to your cart";
});This is why JavaScript matters to HTML and CSS developers.
It turns the page from something people only look at into something they can interact with.
What Can JavaScript Do on a Website?
JavaScript can perform many jobs in the browser.
Change Page Content
JavaScript can change text already displayed on the page.
<h2 id="title">Old Title</h2>const title = document.querySelector("#title");
title.textContent = "New Title";The browser updates the heading.
Show and Hide Elements
A menu may start hidden and open when somebody clicks a button.
const menu = document.querySelector("#menu");
menu.classList.toggle("open");This pattern is commonly used for:
- mobile menus
- accordions
- dropdowns
- FAQ sections
- modal windows
Validate Forms
JavaScript can check input before a form continues.
const email = "";
if (email === "") {
console.log("Please enter your email");
}A real form can use this idea to show an error beside an empty field.
Calculate Values
JavaScript can calculate totals.
const price = 500;
const quantity = 3;
const total = price * quantity;
console.log(total);Output:
1500Website uses include:
- shopping carts
- discount calculators
- EMI calculators
- quantity selectors
- shipping estimates
Load Data
JavaScript can request information from APIs.
For example, a weather application might request the current weather for a city and then display the response on the page.
You will learn this later in the Fetch API and async/await lessons.
How Does JavaScript Work in a Browser?
You do not need to understand browser internals before learning JavaScript.
Start with this basic flow.
Step 1: The Browser Loads HTML
The browser first receives the HTML document.
HTML describes the content and structure.
For example:
<h1>Hello</h1>
<button>Click Me</button>Step 2: The Browser Applies CSS
CSS controls how the elements look.
Step 3: The Browser Loads JavaScript
The page may contain JavaScript directly or load it from a separate .js file.
Step 4: The JavaScript Engine Executes the Code
Modern browsers contain JavaScript engines.
The engine reads and executes JavaScript instructions.
Step 5: JavaScript Can Interact With the Page
Browser features allow JavaScript to work with the webpage.
The basic flow looks like this:
HTML loads
↓
CSS styles the page
↓
JavaScript loads
↓
Browser executes JavaScript
↓
User performs an action
↓
JavaScript responds
↓
Page changesThis is the simple model you need as a beginner.
What Is the DOM?
You will study the DOM properly in a later lesson.
For now, think of the DOM as the browser’s representation of the HTML page.
Suppose your HTML contains:
<p id="status">Waiting</p>JavaScript can find that element:
const status = document.querySelector("#status");Then change it:
status.textContent = "Complete";The text displayed on the webpage becomes:
CompleteThis is called DOM manipulation.
You do not need to memorize DOM methods yet.
The important point is:
JavaScript can use browser APIs to find and change webpage elements.
JavaScript Is More Than the Browser
Beginners usually meet JavaScript through webpages.
But JavaScript can also run outside the browser.
For example, Node.js lets developers execute JavaScript on servers.
JavaScript is therefore used in areas such as:
- frontend web development
- backend development
- web applications
- mobile applications
- desktop applications
- browser games
- development tools
For this tutorial series, the main focus is frontend JavaScript because that is the foundation you need before TypeScript and Angular.
How Do You Add JavaScript to HTML?
There are several ways.
JavaScript Inside the HTML File
You can place JavaScript inside a <script> element.
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Hello JavaScript</h1>
<script>
console.log("JavaScript is working");
</script>
</body>
</html>The browser executes the code inside <script>.
External JavaScript File
For real websites, you will usually keep JavaScript in a separate file.
Create:
script.jsAdd:
console.log("JavaScript is working");Then connect the file to the HTML:
<script src="script.js" defer></script>Using external files keeps your project easier to organize.
The next supporting lesson will explain internal scripts, external files, placement and defer in detail.
Run Your First JavaScript Program
You can run JavaScript without creating a project.
Open your browser’s Developer Tools.
Choose the Console tab.
On many Windows keyboards, F12 opens Developer Tools.
Type:
console.log("My first JavaScript program");Press Enter.
You should see:
My first JavaScript programconsole.log() prints information to the developer console.
Developers regularly use it while learning and debugging code.
JavaScript Is Case-Sensitive
JavaScript treats uppercase and lowercase letters differently.
This works:
const userName = "Riya";
console.log(userName);This does not refer to the same variable:
console.log(username);userName and username are different names.
This is a small rule, but beginners frequently encounter errors because of it.
JavaScript and ECMAScript
You may see words such as:
- ECMAScript
- ES6
- ES2015
- modern JavaScript
ECMAScript is the standard that defines the JavaScript language.
JavaScript implementations follow that standard.
You do not need to study the specification as a beginner.
When tutorials mention ES6 or ES2015, they are usually talking about an important version of the ECMAScript standard that introduced features now common in modern JavaScript.
Is JavaScript the Same as Java?
No.
Java and JavaScript are separate programming languages.
The similar names confuse many beginners.
JavaScript is heavily used in web development.
Java is used in different types of software, backend systems and other application development.
Learning JavaScript does not mean you are learning Java.
Why Learn JavaScript Before TypeScript and Angular?
Angular applications commonly use TypeScript.
TypeScript builds on JavaScript.
If JavaScript fundamentals are unclear, TypeScript and Angular will feel harder than necessary.
Before Angular, become comfortable with:
- variables
- functions
- arrays
- objects
- array methods
- classes
- modules
- promises
async/await
Your learning path is:
HTML → CSS → JavaScript → TypeScript → Angular
That is why this series begins with JavaScript rather than jumping directly into Angular.
Common Beginner Mistakes
Expecting JavaScript to Replace HTML
JavaScript can create or change HTML elements, but HTML should still provide the core page structure.
Learn how the technologies work together.
Copying Code Without Matching the HTML
This JavaScript:
document.querySelector("#button");expects an element with:
id="button"If the element does not exist, your JavaScript cannot use it as expected.
Always check that selectors match your HTML.
Ignoring the Browser Console
When JavaScript fails, check the Console.
It often tells you which line caused the problem.
Jumping Into Frameworks Too Early
Do not rush directly into Angular.
Learn the JavaScript fundamentals first.
Framework code becomes much easier once plain JavaScript makes sense.
Beginner Exercise
Create this HTML:
<h2 id="greeting">Hello</h2>
<button id="greetingButton">Change Greeting</button>Then add:
const greeting = document.querySelector("#greeting");
const greetingButton = document.querySelector("#greetingButton");
greetingButton.addEventListener("click", () => {
greeting.textContent = "Welcome to JavaScript";
});Open the page and click the button.
The heading should change.
What each line does
document.querySelector("#greeting") finds the heading.
document.querySelector("#greetingButton") finds the button.
addEventListener("click", ...) listens for the click.
textContent changes the heading text.
Challenge Exercise
Add another button:
<button id="resetButton">Reset</button>Make it change the heading back to:
HelloTry to solve it without copying the first example exactly.
Frequently Asked Questions
What is JavaScript in simple words?
JavaScript is a programming language that lets websites respond to users, change content, perform calculations and run application logic.
What is JavaScript mainly used for?
In web development, JavaScript is commonly used for user interactions, dynamic page updates, form validation, calculations, API requests and web applications.
Do I need HTML and CSS before learning JavaScript?
You can learn JavaScript without them, but basic HTML and CSS knowledge is strongly recommended for frontend development.
Is JavaScript a programming language?
Yes. JavaScript is a programming language with variables, functions, objects, conditions, loops and many other programming features.
Does JavaScript run only in browsers?
No. JavaScript also runs in environments such as Node.js outside the browser.
Do I need to install JavaScript?
Normally, no. Modern browsers already contain JavaScript engines.
Is JavaScript difficult to learn?
JavaScript is straightforward to start, but more advanced topics require practice. Learn one concept at a time and write code regularly.
Should I learn JavaScript before Angular?
Yes. Learn JavaScript fundamentals and then TypeScript before moving deeply into Angular.
Summary
JavaScript adds behavior and programming logic to websites.
HTML creates the page structure. CSS controls the appearance. JavaScript can respond to clicks, change content, validate forms, calculate values and communicate with APIs.
In a browser, JavaScript runs through the browser’s JavaScript engine and can interact with the webpage through browser APIs such as the DOM.
You do not need to learn everything at once.
At this stage, you only need to understand:
- what JavaScript is
- why websites use it
- how it works with HTML and CSS
- how the browser runs it
- how to execute simple JavaScript
