← Back to JavaScript Course | Chapter 12: Reference & Interview | Lesson 8 of 9

JS का ES6 / 2015

ES6, जिसे ES2015 भी कहते हैं, language के बनने के बाद से JavaScript का सबसे महत्वपूर्ण single update था -- let/const, arrow functions, classes, template literals, destructuring, default parameters, promises, और modules पेश करते हुए, ये सभी वे features हैं जो अब तय करते हैं कि 'modern JavaScript' कैसा दिखता है।
Syntax
javascript
let variable = value;
const CONSTANT = value;
const fn = (x) => x;
class ClassName { }
`template ${expr}`

let और const: Block Scoping

let और const ने असली block scoping पेश की (सबसे पास के { } तक सीमित), var के उलझाने वाले function-scoping और hoisting व्यवहार को ठीक करते हुए -- const इसके अलावा reassignment भी रोकता है, programmer के इरादे (यह value नहीं बदलेगी) को खुद code में explicit बनाते हुए।

Note: किसी भी ऐसे variable के लिए const को default बनाएं जो reassign नहीं होगा, और जो होगा उसके लिए let -- modern code में दोनों काफी हद तक var की जगह ले लेते हैं।
Warning: const variable को खुद reassign होने से रोकता है, पर वह जो object या array रखता है उसे immutable नहीं बनाता -- उसकी properties या elements अभी भी बदल सकते हैं।

उदाहरण: let and const: Block Scoping

javascript
{
  let blockScoped = "inside";
  const fixed = "cannot reassign";
  console.log(blockScoped, fixed);
}
// console.log(blockScoped); // ReferenceError, not visible out here

Arrow Functions और Template Literals

Arrow functions (x => x * 2) एक छोटा function syntax देते हैं और अपना खुद का this define करने के बजाय अपने surrounding scope से inherit करते हैं, और template literals (Hello ${name}) आपको backticks इस्तेमाल करके expressions को सीधे स्ट्रिंग के अंदर embed करने देते हैं, अजीब string concatenation की जगह लेते हुए।

Note: किसी भी ऐसी string के लिए डिफ़ॉल्ट रूप से template literals इस्तेमाल करें जिसमें कोई variable या expression शामिल हो, क्योंकि वे + concatenation से ज़्यादा साफ पढ़ने में आते हैं।
Warning: Arrow functions का inherited this व्यवहार एक feature है, कोई bug नहीं, पर इसका मतलब है कि वे हर जगह जहाँ एक regular function इस्तेमाल होता था वहाँ सीधा replacement नहीं हैं, खासतौर पर अपने खुद के this पर निर्भर object methods के लिए।

उदाहरण: Arrow Functions and Template Literals

javascript
// Declare the constant `double` as an arrow function taking `x`
// Declare the constant `double` as an arrow function taking `x`
const double = x => x * 2;
// Declare the constant `name`, set to "Sam"
// Declare the constant `name`, set to "Sam"
const name = "Sam";
// Print `double(5), `Hello ${name}`` to the console
// Print `double(5), `Hello ${name}`` to the console
console.log(double(5), `Hello ${name}`);

Destructuring और Default Parameters

Destructuring आपको एक line में किसी array या object से values को individual variables में unpack करने देता है (const { name, age } = person), और default parameters किसी function parameter को caller के कोई value न देने पर अपने आप एक तय value पर fallback होने देते हैं।

Note: जब एक ही object या array से कई properties निकालनी हों तो destructuring इस्तेमाल करें, क्योंकि यह कई अलग-अलग property-access lines से ज़्यादा साफ पढ़ने में आता है।
Warning: किसी ऐसी property को destructure करना जो source object पर मौजूद ही नहीं है, error नहीं undefined देता है, जो safe extraction के लिए default values के साथ स्वाभाविक रूप से मिल जाता है।

उदाहरण: Destructuring and Default Parameters

javascript
// Declare the constant `person`, set to `{ name: "Sam", age: 30 }`
// Declare the constant `person`, set to `{ name: "Sam", age: 30 }`
const person = { name: "Sam", age: 30 };
// Destructure {name, age} out of `person`
// Destructure {name, age} out of `person`
const { name, age } = person;
// Define the function `greet` taking `greeting`
// Define the function `greet` taking `greeting`
function greet(greeting = "Hello") { console.log(greeting); }
// Call `greet()`
// Call `greet()`
greet();
// Print `name, age` to the console
// Print `name, age` to the console
console.log(name, age);

Classes और Promises

ES6 का class keyword constructor functions और उनके prototype methods (जो अंदर से JavaScript का मौजूदा inheritance model है) define करने के लिए साफ, ज़्यादा जाना-पहचाना syntax देता है, और Promise object ने asynchronous operations संभालने को standardize किया, उससे पहले के असंगत, ad-hoc callback patterns की जगह लेते हुए।

Note: ES6 classes को JavaScript के मौजूदा prototype system के ऊपर एक साफ syntax के रूप में सीखें, किसी दूसरी language से उधार लिए गए बिल्कुल नए तरह के object model के रूप में नहीं।
Warning: एक Promise-based function को अभी भी proper error handling (.catch() या await के साथ try/catch) चाहिए -- Promises pattern को standardize करते हैं, पर failures संभालने की ज़रूरत को खत्म नहीं करते।

उदाहरण: Classes and Promises

javascript
// Define the class `Animal`
// Define the class `Animal`
class Animal {
  constructor(name) { this.name = name; }
}
// Create a new `Promise` instance with `resolve => resolve("done")).then(v => console.log(v)`
// Create a new `Promise` instance with `resolve => resolve("done")).then(v => console.log(v)`
new Promise(resolve => resolve("done")).then(v => console.log(v));
// Print `new Animal("Rex").name` to the console
// Print `new Animal("Rex").name` to the console
console.log(new Animal("Rex").name);

Modules: import और export

ES6 ने एक native module system पेश किया -- export किसी variable, function, या class को दूसरी files के लिए उपलब्ध बनाता है, और import उसे किसी दूसरी file में लाता है -- यह standardize करते हुए कि JavaScript code कई files में कैसे बांटा जाता है, ऐसी चीज़ जिसके लिए language के पास पहले कोई built-in mechanism नहीं था।

Note: modern JavaScript में कई files में code organize करने के standard तरीके के रूप में ES6 modules इस्तेमाल करें (अपने script tag पर type="module" के साथ, या बड़े projects में एक bundler)।
Warning: type="module" से load किया गया एक script अपने आप strict mode में चलता है और उसका अपना अलग scope होता है, जो पुराने global-script मान्यताओं पर निर्भर code को हैरान कर सकता है।

उदाहरण: Modules: import and export

javascript
// math.js: export function add(a, b) { return a + b; }
// main.js:
import { add } from './math.js';
console.log(add(2, 3));
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि class syntax का मतलब है JavaScript को Java या C++ जैसी असली classical inheritance मिल गई -- ES6 classes JavaScript की existing prototype-based inheritance के ऊपर syntax sugar हैं, कोई fundamentally नया object model नहीं।
  2. नए code में आदतन var इस्तेमाल करना, block-scoping के उन फायदों को miss करते हुए जिनके लिए let और const खासतौर पर introduce किए गए थे।
  3. यह भूल जाना कि arrow functions this keyword को regular functions से अलग तरीके से handle करती हैं, जो पुराने code को लापरवाही से arrow function syntax में convert करते समय bugs का कारण बन सकता है।
चैप्टर सारांश
  • let और const ने सही block scoping introduce की, ज़्यादातर नए code के लिए var की function-scoping quirks की जगह लेते हुए।
  • Arrow functions, template literals, destructuring, और default parameters widely-used ES6 syntax improvements हैं।
  • Promises और module system (import/export) ने JavaScript को asynchronous code और code organization के लिए standardized tools दिए।
ब्राउज़र सपोर्ट

ES6/ES2015 features लगभग 2016 से release हुए हर browser में natively supported हैं, और current browsers में प्रभावी रूप से universal हैं।

🔒

Chapter Quiz — Complete all 9 topics to unlock

0/9 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.