JavaScript ES2025: आज से उपयोग करना शुरू कर सकते हैं नई Features
ES2025 Pipe operator, Records & Tuples, Pattern Matching जैसी रोमांचक features लेकर आता है। जानें क्या ship हो रहा है और क्या अभी proposal stage में है।
JavaScript ES2025: नई Features
JavaScript लगातार विकसित हो रहा है। ES2025 (आधिकारिक रूप से ES16) कई रोमांचक features लेकर आता है जो code को साफ़ और अधिक expressive बनाते हैं।
1. Pipe Operator (|>)
Pipe operator आपको function calls को readable left-to-right flow में chain करने देता है। अब nested parentheses नहीं!
// Pipe के बिना
const result = capitalize(reverse(trim(input)));
// Pipe के साथ
const result = input
|> trim
|> reverse
|> capitalize;2. Records और Tuples
Objects और arrays के immutable versions। ये # prefix का उपयोग करते हैं और default रूप से deeply frozen होते हैं।
const record = #{ name: "w3primecode", lang: "hi" };
const tuple = #[1, 2, 3, 4];
// Deep equality काम करता है!
#{ a: 1, b: 2 } === #{ a: 1, b: 2 }; // true
#[1, 2, 3] === #[1, 2, 3]; // true3. Pattern Matching
Switch का बेहतर version। Object shapes, arrays, और patterns को match करें।
match (response) {
{ status: 200, data } => renderData(data),
{ status: 404 } => showNotFound(),
{ status: >= 500 } => showServerError(),
_ => showUnknownError()
}4. Array.prototype.group()
Array items को key द्वारा group करें। reduce लिखने से बेहतर।
const posts = [
{ title: "CSS Tips", category: "css" },
{ title: "HTML Guide", category: "html" },
{ title: "More CSS", category: "css" },
];
const grouped = posts.group(p => p.category);
// { css: [...], html: [...] }5. Array.prototype.findLast() और findLastIndex()
Array reverse किए बिना last matching element खोजें।
const lastEven = [1, 2, 3, 4, 5].findLast(n => n % 2 === 0); // 46. Object.hasOwn()
Object.prototype.hasOwnProperty.call() से सुरक्षित।
const obj = Object.create(null);
obj.key = "value";
Object.hasOwn(obj, "key"); // true
// obj.hasOwnProperty("key") त्रुटि देगा!7. Error Cause
Context के साथ errors chain करें। Debugging के लिए बढ़िया।
try {
await fetchData();
} catch (err) {
throw new Error("Failed to load user", { cause: err });
}8. structuredClone()
Hacks के बिना objects को deep clone करें (Dates, Maps, Sets सहित)।
const original = { date: new Date(), nested: { a: 1 } };
const copy = structuredClone(original);9. Top-level Await
ES modules के top level पर await का उपयोग करें। IIFE wrapper की ज़रूरत नहीं।
// module.mjs
const data = await fetch('/api/data').then(r => r.json());
export default data;10. Array.prototype.at()
Python की तरह negative indexing।
const arr = [10, 20, 30, 40];
arr.at(-1); // 40
arr.at(-2); // 30Browser Support
| Feature | Chrome | Firefox | Safari |
|---|---|---|---|
| Array.group | ✅ | ✅ | ✅ |
| findLast | ✅ | ✅ | ✅ |
| Object.hasOwn | ✅ | ✅ | ✅ |
| Error cause | ✅ | ✅ | ✅ |
| structuredClone | ✅ | ✅ | ✅ |
| Top-level await | ✅ | ✅ | ✅ |
| Array.at | ✅ | ✅ | ✅ |
| Pipe operator | ⚠️ | ⚠️ | ⚠️ |
| Records & Tuples | ⚠️ | ⚠️ | ⚠️ |
| Pattern matching | ⚠️ | ⚠️ | ⚠️ |
⚠️ = अभी भी proposal stage में, transpilation की आवश्यकता हो सकती है
निष्कर्ष
इनमें से अधिकांश features पहले से ही modern browsers में हैं। Pipe और Pattern Matching जैसे cutting-edge proposals के लिए, transpile करने के लिए Babel या TypeScript का उपयोग करें। group(), findLast(), और structuredClone() का उपयोग आज ही शुरू करें — आपका code धन्यवाद कहेगा।