Arrays

Introduction

जब हमें multiple values को एक ही variable में store करना होता है, तो हम Arrays का उपयोग करते हैं।

सोचिए—अगर आपको 10 students के names store करने हों, तो क्या आप 10 अलग-अलग variables बनाएंगे? यह मुश्किल और confusing हो जाएगा।

यहीं पर Arrays काम आते हैं। यह एक ही variable में कई values store करने की सुविधा देते हैं।

Web Development में Arrays बहुत important हैं, क्योंकि data को manage करना आसान हो जाता है।

Definition

Array एक ऐसा data structure है जिसमें multiple values को एक single variable में store किया जाता है।

Concept

1. Array Declaration

JavaScript में array को square brackets [] के अंदर define किया जाता है।

<script>
let fruits = ["Apple", "Mango", "Banana"];
console.log(fruits);
</script>

Output:
[“Apple”, “Mango”, “Banana”]

2. Accessing Array Elements

Array के elements को index के माध्यम से access किया जाता है।
Index हमेशा 0 से शुरू होता है।

<script>
let fruits = ["Apple", "Mango", "Banana"];
console.log(fruits[0]);
</script>

Output:
Apple

3. Modifying Array

<script>
let fruits = ["Apple", "Mango", "Banana"];
fruits[1] = "Orange";
console.log(fruits);
</script>

Output:
[“Apple”, “Orange”, “Banana”]

4. Array Length

Array में कितने elements हैं, यह जानने के लिए length property use होती है।

<script>
let fruits = ["Apple", "Mango", "Banana"];
console.log(fruits.length);
</script>

Output:
3

5. Adding Elements

(a) push() → end में add करता है

<script>
let fruits = ["Apple", "Mango"];
fruits.push("Banana");
console.log(fruits);
</script>

Output:
[“Apple”, “Mango”, “Banana”]

(b) unshift() → शुरुआत में add करता है

<script>
let fruits = ["Mango", "Banana"];
fruits.unshift("Apple");
console.log(fruits);
</script>

Output:
[“Apple”, “Mango”, “Banana”]

6. Removing Elements

(a) pop() → last element हटाता है

<script>
let fruits = ["Apple", "Mango", "Banana"];
fruits.pop();
console.log(fruits);
</script>

Output:
[“Apple”, “Mango”]

(b) shift() → first element हटाता है

<script>
let fruits = ["Apple", "Mango", "Banana"];
fruits.shift();
console.log(fruits);
</script>

Output:
[“Mango”, “Banana”]

7. Looping through Array

<script>
let fruits = ["Apple", "Mango", "Banana"];for(let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
</script>

Output:
Apple
Mango
Banana

Real-Life Example

मान लीजिए एक class में students के names store करने हैं:

Rahul
Aman
Ravi

इन सभी को अलग-अलग variable में रखने के बजाय array में store करना आसान और efficient होता है।

Important Points

Array multiple values store करता है
Index 0 से शुरू होता है
Square brackets [] का उपयोग होता है
length property useful है
push, pop, shift, unshift methods important हैं

निष्कर्ष

Arrays JavaScript का एक महत्वपूर्ण data structure है, जो multiple values को manage करने का आसान तरीका प्रदान करता है। इसका उपयोग data handling और looping में बहुत अधिक किया जाता है।

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top