
JavaScript (JS) is a high-level, interpreted programming language used mainly to make web pages interactive and dynamic.
It works alongside:
- HTML โ to structure the web page
- CSS โ to style the web page
- JavaScript โ to add behavior and logic
Example:
๐ HTML can display a button,
๐ CSS can make it look nice,
๐ JavaScript can make it do something when clicked.
๐ง Brief History
- Created by Brendan Eich in 1995 for Netscape Navigator browser.
- Originally called LiveScript, later renamed to JavaScript.
- Now standardized as ECMAScript (ES) โ the latest versions are ES6, ES7, etc.
- Runs in all modern web browsers (Chrome, Firefox, Safari, Edge).
โ๏ธ How JavaScript Works
JavaScript is:
- Client-side โ runs directly in the userโs browser (not on the server).
- Interpreted โ executes line by line, not compiled beforehand.
- Event-driven โ responds to user actions (like clicks or keypresses).
You can also run JavaScript outside browsers using Node.js.
๐ป Where to Write JavaScript
You can write JavaScript:
- Inside HTML files
<script> alert("Hello, world!"); </script> - In an external file
<script src="script.js"></script>Inscript.js:alert("This is external JavaScript!");
๐ก Basic Syntax and Structure
JavaScript code is written as a series of statements, each ending with a semicolon (;).
Example:
let name = "Raj";
console.log("Hello " + name);
๐ข Variables
Variables are containers for storing data.
Declaring variables:
| Keyword | Use | Scope |
|---|---|---|
var | old, function-scoped | โ not recommended |
let | modern, block-scoped | โ preferred |
const | for constants (cannot be reassigned) | โ preferred for fixed values |
Example:
let age = 20;
const name = "Riya";
๐งฎ Data Types
JavaScript has primitive and non-primitive data types.
Primitive Types:
| Type | Example |
|---|---|
| String | "Hello" |
| Number | 42, 3.14 |
| Boolean | true, false |
| Undefined | declared but not assigned |
| Null | empty or no value |
| Symbol | unique identifier |
| BigInt | for very large numbers |
Example:
let name = "Rohan"; // string
let age = 18; // number
let isStudent = true; // boolean
let city; // undefined
โ Operators
Used to perform operations on values.
| Type | Examples |
|---|---|
| Arithmetic | + - * / % ** |
| Assignment | = += -= *= /= |
| Comparison | == === != !== > < >= <= |
| Logical | `&& |
| String | + (concatenation) |
Example:
let x = 5, y = 10;
console.log(x + y); // 15
console.log(x > y); // false
๐ Control Structures
Used to control program flow.
1. If-Else
let age = 18;
if (age >= 18) {
console.log("You are an adult");
} else {
console.log("You are a minor");
}
2. Switch
let day = 2;
switch(day) {
case 1: console.log("Monday"); break;
case 2: console.log("Tuesday"); break;
default: console.log("Other day");
}
3. Loops
for(let i = 1; i <= 5; i++) {
console.log(i);
}
let j = 1;
while(j <= 5) {
console.log(j);
j++;
}
๐งฉ Functions
Functions let you group reusable code.
Example:
function greet(name) {
console.log("Hello " + name);
}
greet("Aarav"); // Output: Hello Aarav
Arrow Function (ES6+)
const greet = (name) => console.log(`Hello ${name}`);
๐ฆ Arrays
Arrays store multiple values in a single variable.
let fruits = ["Apple", "Banana", "Mango"];
console.log(fruits[1]); // Banana
fruits.push("Orange"); // add
fruits.pop(); // remove last
๐ Objects
Objects store data in key-value pairs.
let student = {
name: "Riya",
age: 21,
course: "B.Tech"
};
console.log(student.name); // Riya
๐๏ธ Events and the DOM
JavaScript can interact with HTML through the DOM (Document Object Model).
Example:
<button id="myBtn">Click Me</button>
<script>
document.getElementById("myBtn").addEventListener("click", function() {
alert("Button Clicked!");
});
</script>
๐ Comments
Use comments to describe code.
// This is a single-line comment
/*
This is
a multi-line comment
*/
โ๏ธ JavaScript in the Browser
You can:
- Log messages โ
console.log("message"); - Show alerts โ
alert("Hi!"); - Prompt user โ
prompt("Enter name:"); - Confirm actions โ
confirm("Are you sure?");
๐ Modern JavaScript (ES6+ Features)
| Feature | Example |
|---|---|
| Template Literals | `Hello ${name}` |
| Let & Const | let, const |
| Arrow Functions | () => {} |
| Destructuring | const {a,b} = obj; |
| Spread Operator | [...arr1, ...arr2] |
| Classes | class Person {} |
| Modules | import / export |
๐งญ Advantages of JavaScript
โ
Runs in all browsers
โ
Easy to learn and flexible
โ
Can be used for both front-end and back-end (via Node.js)
โ
Large community and library support
โ
Powers modern frameworks (React, Angular, Vue)
โ ๏ธ Limitations
โ ๏ธ Can be misused for insecure code (if not careful)
โ ๏ธ Different browsers may interpret JS slightly differently
โ ๏ธ Single-threaded (though async fixes this)
๐งฉ Example: Simple Interactive Web Page
<!DOCTYPE html>
<html>
<head>
<title>JS Example</title>
</head>
<body>
<h2 id="greeting">Hello!</h2>
<button onclick="changeText()">Click Me</button>
<script>
function changeText() {
document.getElementById("greeting").innerText = "You clicked the button!";
}
</script>
</body>
</html>
๐ When you click the button, JavaScript updates the text dynamically.
๐ Summary
| Topic | Description |
|---|---|
| Language Type | Interpreted, high-level scripting language |
| Used For | Adding interactivity to web pages |
| Runs In | Browser (and Node.js for server-side) |
| Key Features | Dynamic typing, functions, objects, events |
| Related To | HTML (structure), CSS (style) |

Thanks for sharing this! Do you think this method works well with [related tech or framework]? Iโm experimenting with it in my project.
This is outstanding work.