JavaScript is a high-level, interpreted programming language that allows you to implement complex features on web pages. While HTML provides structure and CSS handles presentation, JavaScript adds interactivity and dynamic behavior to websites.
Despite the similar name, JavaScript is completely different from Java. JavaScript was created by Brendan Eich in 1995 and was originally called Mocha, then LiveScript, before settling on its current name.
JavaScript runs directly in the browser, interpreting code line by line. Modern browsers include JavaScript engines that compile JavaScript code into machine code for faster execution.
1
Loading
The browser loads the HTML document and encounters a JavaScript file or inline script.
2
Parsing
The JavaScript engine parses the code, checking for syntax errors.
3
Compilation
The engine compiles the code into an optimized form (just-in-time compilation).
4
Execution
The compiled code runs, manipulating the DOM, handling events, or performing calculations.
function greet() { alert('Hello, World!');}document.querySelector('button').addEventListener('click', greet);
The defer attribute tells the browser to continue parsing the HTML while the script is being downloaded, and to execute the script only after the HTML parsing is complete. This improves page load performance.
// Using let (block-scoped, can be reassigned)let age = 25;age = 26; // Valid reassignment// Using const (block-scoped, cannot be reassigned)const PI = 3.14159;// PI = 3; // Error: Assignment to constant variable// Using var (function-scoped, older way, less recommended)var name = "John";
// if statementlet age = 18;if (age >= 18) { console.log("You are an adult");} else if (age >= 13) { console.log("You are a teenager");} else { console.log("You are a child");}// switch statementlet day = "Monday";switch (day) { case "Monday": console.log("Start of the work week"); break; case "Friday": console.log("End of the work week"); break; default: console.log("Another day");}// Ternary operatorlet status = age >= 18 ? "adult" : "minor";
One of JavaScript’s most powerful features is its ability to manipulate the Document Object Model (DOM), allowing dynamic changes to web page content and structure.
Copy
// Selecting elementsconst heading = document.getElementById('main-heading');const paragraphs = document.getElementsByTagName('p');const buttons = document.getElementsByClassName('btn');const firstLink = document.querySelector('a');const allLinks = document.querySelectorAll('a');// Modifying contentheading.textContent = 'New Heading Text'; // Text onlyheading.innerHTML = 'New <span>Heading</span> Text'; // HTML content// Changing stylesheading.style.color = 'blue';heading.style.fontSize = '24px';// Adding/removing classesheading.classList.add('highlight');heading.classList.remove('old-class');heading.classList.toggle('active');// Creating new elementsconst newParagraph = document.createElement('p');newParagraph.textContent = 'This is a new paragraph.';document.body.appendChild(newParagraph);// Removing elementsconst oldElement = document.getElementById('old-element');oldElement.parentNode.removeChild(oldElement);// Or with newer syntaxoldElement.remove();