जावास्क्रिप्ट में समान नाम वाले पैरामीटर

अब मान लीजिए कि बाहरी और आंतरिक फ़ंक्शन में समान नाम वाले पैरामीटर हैं:

javascriptfunction test(num) {
  function func(num) {
    console.log(num); // 1 प्रिंट करेगा
  }
  
  func(num);
};

test(1);

इस स्थिति में, आंतरिक फ़ंक्शन में एक स्थानीय चर num होगा। इसके मान को आंतरिक फ़ंक्शन में बदलने का बाहरी चर num पर कोई प्रभाव नहीं पड़ेगा:

javascriptfunction test(num) {
  function func(num) {
    num = 2; // स्थानीय चर num को बदल रहे हैं
  }
  
  func(num);
  console.log(num); // 1 प्रिंट करेगा - कुछ नहीं बदला
}

test(1);

ऐसा होगा कि आंतरिक फ़ंक्शन बाहरी चर num tका उसके मान को बदलने के लिए किसी भी तरह से उस तक नहीं पहुंच पाएगा:

javascriptfunction test(num) {
  function func(num) {
    // यहां बाहरी चर num तक पहुंच प्राप्त नहीं की जा सकती
  }
  
  func(num);
}
test(1);
Задание 1

कोड चलाए बिना निर्धारित करें कि कंसोल में क्या प्रिंट होगा:

javascriptfunction test(num) {
  function func(num) {
    console.log(num);
  }
  
  func(num);
}

test(1);
Задание 2

कोड चलाए बिना निर्धारित करें कि कंसोल में क्या प्रिंट होगा:

javascriptfunction test(num) {
  function func(num) {
    num = 2;
  }
  
  func(num);
  console.log(num);
}

test(1);
Задание 3

कोड चलाए बिना निर्धारित करें कि कंसोल में क्या प्रिंट होगा:

javascriptfunction test(num) {
  function func(num) {
    console.log(num);
  }
  
  num = 2;
  func(num);
}

test(1);
Задание 4

कोड चलाए बिना निर्धारित करें कि कंसोल में क्या प्रिंट होगा:

javascriptfunction test(num) {
  function func(num) {
    console.log(num);
  }
  
  func(num);
  num = 2;
}

test(1);