๐ฑ 1. Variables and Data Types¶
๐ var, let, const¶
var a = 10; // function-scoped
let b = 20; // block-scoped
const c = 30; // constant
b = 25; // โ
allowed
// c = 40; โ TypeError
๐ Data Types¶
let str = "Hello"; // string
let num = 42; // number
let bool = true; // boolean
let obj = { name: "Yuvaraj" }; // object
let arr = [1, 2, 3]; // array
let und; // undefined
let n = null; // null
๐งฎ 2. Operators¶
let a = 5, b = 2;
// Arithmetic
console.log(a + b, a - b, a * b, a / b, a % b);
// Comparison
console.log(a > b, a = b, a ! b);
// Logical
console.log(a > 1 && b < 3); // AND
console.log(a < 1 || b < 3); // OR
console.log(!true); // NOT
๐งฉ 3. Functions¶
๐ Normal Function¶
๐ Arrow Function¶
๐ Default & Rest Parameters¶
function greet(name = "Guest") {
console.log(`Hello, ${name}`);
}
function sum(...nums) {
return nums.reduce((a, b) => a + b, 0);
}
greet("Yuvaraj"); // Hello, Yuvaraj
console.log(sum(1, 2, 3, 4)); // 10
๐ 4. Conditionals and Loops¶
let age = 20;
if (age >= 18) console.log("Adult");
else console.log("Minor");
// Loop
for (let i = 1; i <= 3; i++) {
console.log("Count:", i);
}
// While loop
let n = 0;
while (n < 3) {
console.log("n =", n);
n++;
}
๐งฐ 5. Arrays¶
๐ Common Methods¶
const nums = [1, 2, 3, 4, 5];
console.log(nums.map(n => n * 2)); // [2,4,6,8,10]
console.log(nums.filter(n => n > 2)); // [3,4,5]
console.log(nums.reduce((a, b) => a + b)); // 15
console.log(nums.find(n => n === 3)); // 3
๐งฑ 6. Objects¶
const user = {
name: "Yuvaraj",
age: 22,
greet() {
console.log(`Hello, I'm ${this.name}`);
}
};
user.greet();
console.log(user.age);
๐ Destructuring¶
๐ฏ 7. ES6 Features¶
๐ Template Literals¶
๐ Spread & Rest¶
let arr1 = [1, 2];
let arr2 = [...arr1, 3, 4]; // Spread
console.log(arr2);
function logArgs(...args) {
console.log(args);
}
logArgs("a", "b", "c"); // Rest
๐ง 8. Asynchronous JavaScript¶
๐ Callbacks¶
function fetchData(callback) {
setTimeout(() => {
callback("Data loaded โ
");
}, 1000);
}
fetchData(console.log);
๐ Promises¶
const getData = new Promise((resolve) => {
setTimeout(() => resolve("Promise resolved โ
"), 1000);
});
getData.then(console.log);
๐ Async / Await¶
๐ 9. Fetch API (Promise Example)¶
fetch("https://jsonplaceholder.typicode.com/users/1")
.then(res => res.json())
.then(data => console.log(data))
.catch(err => console.log("Error:", err));
or with async/await:
async function getUser() {
const res = await fetch("https://jsonplaceholder.typicode.com/users/1");
const data = await res.json();
console.log(data.name);
}
getUser();
๐งฉ 10. DOM Manipulation (Browser)¶
<div id="app"></div>
<button id="btn">Click</button>
<script>
const app = document.getElementById("app");
const btn = document.getElementById("btn");
btn.addEventListener("click", () => {
app.innerText = "Button Clicked ๐";
});
</script>
๐งฑ 11. Classes and OOP¶
class User {
constructor(name, role) {
this.name = name;
this.role = role;
}
greet() {
console.log(`Hello, I'm ${this.name}, a ${this.role}`);
}
}
const yuva = new User("Yuvaraj", "Full Stack Dev");
yuva.greet();
๐ฆ 12. Modules (ES Modules)¶
๐ Export (file: math.js)¶
๐ Import (file: app.js)¶
๐งน 13. Error Handling¶
try {
let result = riskyFunction(); // not defined
} catch (err) {
console.error("Error occurred:", err.message);
} finally {
console.log("Cleanup complete");
}
๐งญ 14. JSON Basics¶
const obj = { name: "Yuvaraj", age: 22 };
const json = JSON.stringify(obj); // Object โ JSON string
const parsed = JSON.parse(json); // JSON โ Object
console.log(json);
console.log(parsed.name);
๐ Summary¶
| Concept | Key Idea |
|---|---|
| Variables | var, let, const |
| Data Types | string, number, boolean, object, array |
| Functions | normal & arrow |
| Arrays/Objects | map, filter, destructuring |
| Async | callbacks โ promises โ async/await |
| DOM | manipulate HTML dynamically |
| Classes | OOP in JS |
| Modules | code reusability (import / export) |