JavaScript Tutorial
Master JavaScript from scratch — learn variables, functions, DOM manipulation, and events. Complete beginner-friendly guide with practical examples.
Contents
- Introduction
- Where To
- Output
- Variables
- Operators
- Conditionals
- Loops
- Functions
- DOM Manipulation
- Events
- Best Practices
- FAQ
JavaScript Introduction
JavaScript is a programming language that makes websites interactive. It runs in browsers and lets you respond to user actions, validate forms, change HTML content dynamically, and update page styles.
Why learn JavaScript?
- Essential for web development — Required skill for all modern websites
- High demand — Most sought-after programming skill
- Beginner friendly — Easy to start, powerful to master
JavaScript Where To
JavaScript can be placed in three different locations in your HTML: inline in elements, inside <script> tags in the HTML, or in external .js files.
Internal JavaScript
Write JavaScript directly inside <script> tags in your HTML file:
<script> let name = "John"; alert("Hello " + name); </script>
External JavaScript
Create a separate .js file and link it to your HTML:
<script src="script.js"></script>
Inline JavaScript
JavaScript inside HTML attributes (not recommended for large projects):
<button onclick="alert('Clicked!')">Click Me</button>
JavaScript Output
There are several ways to display output in JavaScript: using the console, alert dialogs, HTML content, or browser document.
Methods to display output
// 1. Console (for debugging) alert("This is an alert box"); // 2. Alert dialog alert("Hello World"); // 3. Change HTML content document.getElementById("demo").innerHTML = "Hello!"; // 4. Write to document document.write("Hello World");
Getting Started
JavaScript can be added to HTML using <script> tags. Internal scripts go in the HTML file, while external scripts are in separate .js files.
<!DOCTYPE html> <html> <body> <h1>Hello World</h1> <script> let message = "Hello JavaScript"; </script> </body> </html>
Variables & Data Types
Variables store data values. Use let or const in modern JavaScript. const prevents reassignment, while let allows variable changes.
let name = "John"; let age = 25; let isActive = true; const PI = 3.14159;
Data types
Operators
Operators perform operations on variables. Arithmetic operators include +, -, *, /, and %. Comparison operators (==, ===, >, <) check values.
let a = 10, b = 5; let sum = a + b; // 15 let product = a * b; // 50 let isGreater = a > b; // true
Conditional Statements
Use if, else if, and else to execute code based on conditions. This controls program flow based on different scenarios.
let age = 20; if (age >= 18) { let status = "Adult"; } else { let status = "Minor"; }
Loops
for loops repeat code a set number of times. while loops continue until a condition is false. Use forEach to loop through arrays.
// For loop: repeat 5 times for (let i = 0; i < 5; i++) { // Code repeats } // Loop through array let fruits = ["apple", "banana"]; fruits.forEach(function(fruit) { // Process each fruit });
Functions
Functions are reusable code blocks that perform specific tasks. They can accept parameters (inputs) and return values (outputs).
// Function declaration function greet(name) { return "Hello, " + name; } greet("John"); // Returns: "Hello, John" // Arrow function (modern syntax) const add = (a, b) => a + b;
DOM Manipulation
The DOM represents HTML as a tree. JavaScript can select elements using getElementById(), querySelector(), and modify their content, styles, and properties.
// Select elements let elem = document.getElementById("myId"); let el = document.querySelector(".myClass"); // Change text elem.textContent = "New text"; elem.innerHTML = "<b>Bold</b>"; // Modify styles elem.style.color = "red"; elem.classList.add("active");
Event Handling
Events trigger when users interact with the page (click, type, submit). Use addEventListener() to respond to events with JavaScript code.
let btn = document.getElementById("myBtn"); // Click event btn.addEventListener("click", function() { alert("Button clicked!"); }); // Arrow function syntax btn.addEventListener("click", () => { alert("Clicked!"); });
Best Practices
Frequently Asked Questions
JavaScript is a programming language that runs in browsers and makes websites interactive. It responds to user actions, validates forms, and dynamically changes page content.
Variables store data values. Use let or const to declare variables. const prevents reassignment while let allows changes.
A function is a reusable code block that performs a task. Functions can accept parameters and return values.
DOM manipulation is changing HTML elements and their properties using JavaScript.
Events are triggered by user actions like clicks, typing, or form submissions. Use addEventListener to respond.
Start with basics, build small projects, use browser console to test code, and practice regularly.