JavaScript Notes
1. Introduction to JavaScript:
- JavaScript is a lightweight, interpreted programming language.
- Used mainly for web development.
- Runs in the browser (client-side) and also on servers (Node.js).
2. Variables:
- Declared using var, let, or const.
let x = 10;
const y = 20;
3. Data Types:
- Primitive: String, Number, Boolean, Null, Undefined, Symbol
- Non-Primitive: Object, Array
4. Operators:
- Arithmetic: +, -, *, /, %
- Comparison: ==, ===, !=, !==, >, <, >=, <=
- Logical: &&, ||, !
5. Conditional Statements:
if (condition) {
// code
} else if (condition) {
// code
} else {
// code
6. Loops:
for (let i = 0; i < 5; i++) {
console.log(i);
while (condition) {
// code
do {
// code
} while (condition);
7. Functions:
function add(a, b) {
return a + b;
const multiply = (a, b) => a * b;
8. Arrays:
let arr = [1, 2, 3];
console.log(arr[0]);
arr.push(4);
9. Objects:
let person = {
name: "John",
age: 30
};
console.log(person.name);
10. DOM Manipulation:
document.getElementById("demo").innerHTML = "Hello";
document.querySelector(".class").style.color = "red";
11. Events:
document.getElementById("btn").addEventListener("click", function() {
alert("Button clicked!");
});
12. ES6 Features:
- Arrow Functions
- Template Literals: `Hello ${name}`
- Destructuring
- Spread and Rest Operators
13. JSON:
let jsonString = JSON.stringify(obj);
let jsonObj = JSON.parse(jsonString);
14. Promises & Async/Await:
function fetchData() {
return new Promise((resolve, reject) => {
// async code
});
async function getData() {
let data = await fetchData();
console.log(data);
15. Useful Methods:
- String: length, toUpperCase(), includes()
- Array: map(), filter(), reduce(), forEach()