Array Iterations & Regular Expressions
forEach runs a function for each array item. It does not return a new array — use map for that.
forEach
forEach runs a function for each array item. It does not return a new array — use map for that.
Real-life example: Calling each student name from attendance sheet — read only, no new list.
["apple", "mango"].forEach((fruit, index) => {
console.log(index, fruit);
});map, filter, reduce
map — new array with changes. filter — new array with matches. reduce — fold all items into one value (sum, max, object).
Real-life example: reduce — add all bill amounts into one total at month end.
const bills = [100, 250, 75];
const total = bills.reduce((sum, bill) => sum + bill, 0);
console.log(total); // 425some and every
some — true if at least one item passes. every — true if all items pass.
Real-life example: some — at least one egg is broken. every — all eggs are fresh.
[1, 2, 3].some((n) => n > 2); // true
[1, 2, 3].every((n) => n > 0); // true
[1, 2, 3].every((n) => n > 1); // falseRegExp — patterns in text
Regular expressions match patterns in strings. Write /pattern/ or new RegExp("pattern").
Real-life example: RegExp is a stencil — only text that fits the hole shape is selected.
const pattern = /hello/i; // i = ignore case
pattern.test("Hello World"); // truetest and match
test returns true/false. match returns matched parts or null.
Real-life example: test — "Does this look like an email?" match — "Show me the phone number part."
/\d+/.test("Room 42"); // true — has digits
"Price: 499".match(/\d+/); // ["499"]Common patterns — email and digits
Simple checks: email has @ and dot; digits use \d. Real validation needs stricter rules, but these help in learning.
Real-life example: Email pattern is a basic gate check at school entry — not full security, but catches obvious mistakes.
const email = "user@rishtaara.com";
const emailOk = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
const phone = "9876543210";
const allDigits = /^\d{10}$/.test(phone);