डिफ़ॉल्ट पैरामीटर्स
यह फ़ंक्शन पैरामीटर्स को आसानी से हैंडल करने के लिए आया। आसानी से डिफ़ॉल्ट पैरामीटर्स सेट करें ताकि फॉर्मल पैरामीटर्स को डिफ़ॉल्ट वैल्यूज़ के साथ इनिशियलाइज़ किया जा सके। यह केवल तभी संभव है जब कोई वैल्यू या undefined पास न किया जाए।
उदाहरण
<html>
<body>
<script>
// default is set to 1
function inc(val1, inc = 1) {
return val1 + inc;
}
document.write(inc(10,10));
document.write("<br>");
document.write(inc(10));
</script>
</body>
</html>
रेस्ट पैरामीटर्स
ES6 डेवलपर्स के काम को आसान बनाने के लिए रेस्ट पैरामीटर लाया। आर्ग्यूमेंट्स ऑब्जेक्ट्स के लिए, रेस्ट पैरामीटर्स तीन डॉट्स ... द्वारा इंगित किए जाते हैं और एक पैरामीटर से पहले आते हैं।
उदाहरण
<html>
<body>
<script>
function addition(...numbers) {
var res = 0;
numbers.forEach(function (number) {
res += number;
});
return res;
}
document.write(addition(3));
document.write(addition(5,6,7,8,9));
</script>
</body>
</html>
डिस्ट्रक्चरिंग
ES6 में पेश किया गया पैरामीटर पैटर्न मैचिंग के साथ बाइंडिंग के लिए। यदि वैल्यू नहीं मिलती है, तो यह undefined लौटाता है। आइए देखें कि ES6 कैसे ऐरे को इंडिविजुअल वेरिएबल्स में डिस्ट्रक्चर करने की अनुमति देता है।
उदाहरण
<html>
<body>
<script>
let marks = [92, 95, 85];
let [val1, val2, val3] = marks;
document.write("Value 1: "+val1);
document.write("<br>Value 2: "+val2);
document.write("<br>Value 3: "+val3);
</script>
</body>
</html>