JavaScript में Array का use multiple different types of values को store करने के लिए किया जाता।


अभी तक हम जो variables use कर रहे थे , उन्हें हमें अलग अलग declare करना पड़ता था , but Array का use करके हम उन सभी variables को एक साथ define करके use में ले सकते हैं।

? JavaScript में array define करने के लिए हमें array का type define करने की जरूरत नहीं होती है। जैसे कि JAVA या C में हमें array define करते समय array का type define करने की जरूरत पड़ती है ।

JavaScript Array Syntax

JavaScript में Array define करते समय data type define करने की जरूरत नहीं पड़ती है , Array को दो तरह से define / declare किया जा सकता है।

  1. Using Brackets [ ] .
  2. Using Array Object .
let array_var1 = ['value1' , 'value2', 12 , 54.7];
let array_var2 = new Array('value1' , 'value2', 12 , 54.7);

Array define करने के लिए define किये गए दोनों syntax में से आप कोई भी syntax use कर सकते है , और किसी भी data type की value को store कर सकते हैं।

JavaScript Array Example

File : array.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript Array In Hindi </title>
  </head>
  <body>
    <script type="text/javascript">
      let array_var = [12, 34.5 , true , false , "string value"];
      document.write(array_var);
    </script>
  </body>
</html>
Output
12,34.5,true,false,string value

Access Array Values

JavaScript में Array store की गयी values को index number के रूप में associate करता है जो कि 0 start होता है। index का use करके हम array values को access कर सकते हैं।

for example

console.log(array_var[0]); // console first array element
console.log(array_var[1]); // console second array element

JavaScript Traversing Array

JavaScript में Array traverse / iterate करने के लिए for loop , for in loop , for of loop या while loop में से किसी का भी use कर सकते हैं।

See Example

File : traversearray.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript Iterating Array In Hindi </title>
  </head>
  <body>
    <script type="text/javascript">
      let array_var = [12, 34.5 , true , false , "string value"];
      let index;

      document.write(`Traversing using for loop <br>`);
      for(index = 0; index < array_var.length; index++){
        document.write(`index number :${index} : ${array_var[index]} <br>`);
      }

      document.write(`<br>Traversing using for in loop <br>`);
      for(index in array_var){
        document.write(`index number :${index} : ${array_var[index]} <br>`);
      }

      document.write(`<br>Traversing using for of loop <br>`);
      for(value of array_var){
        document.write(`${value} <br>`);
      }

      document.write(`<br>Traversing using while loop <br>`);
      index = 0;
      while(index < array_var.length){
        document.write(`index number :${index} : ${array_var[index]} <br>`);
        index++;
      }
    </script>
  </body>
</html>
Output
Traversing using for loop
index number :0 : 12
index number :1 : 34.5
index number :2 : true
index number :3 : false
index number :4 : string value

Traversing using for in loop
index number :0 : 12
index number :1 : 34.5
index number :2 : true
index number :3 : false
index number :4 : string value

Traversing using for of loop
12
34.5
true
false
string value

Traversing using while loop
index number :0 : 12
index number :1 : 34.5
index number :2 : true
index number :3 : false
index number :4 : string value

? Backticks (``) का use करके define की गयी string में हम ${ } ( dollar curly braces) के अंदर हम direct variables को print करा सकते हैं। और <br> का use line break करने के लिए करते हैं।

JavaScript में length operator का use किसी Array की length (numbers of elements) जानने के लिए किया जाता है।

? Numbers of element inside array not it's length

जी हाँ यह बिलकुल सही है , किसी भी array में number of elements, array की actual length नहीं होती है। Actually आप array length को manually manipulate कर सकते हैं , या array में new element insert करते समय indexes को manually skip कर सकते हैं।

See Example

File : array3.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript Manupulating Array Length In Hindi </title>
  </head>
  <body>
    <script type="text/javascript">
      /* you can also declare array like this */
      let array_var = [];
      array_var[0] = "first value";
      array_var[1] = "other value";

      /* now skip the array index via associating with new index */
      array_var[20] = "Another value";
      document.write(`Array Values : ${array_var} <br>`);
      document.write(`Array length : ${array_var.length} <br>`);

       /* no manually manupulate the length */
       array_var.length = 90;
       document.write(`New array length : ${array_var.length}`);
    </script>
  </body>
</html>
Output
Array Values : first value,other value,,,,,,,,,,,,,,,,,,,Another value
Array length : 21
New array length : 90      

Explanation : Example में आप देख सकते हैं कि जैसे ही आपने indexes को skip करके new value को 20 index पर store किया , automatically बाकी indexes पर empty / null value हो गयी। array length में नयी value assign करने पर भी Array की length change हो गयी।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers