← Back to JavaScript Course | Chapter 3: Strings, Arrays & Data | Lesson 3 of 6

JS ऐरे

एक नंबर वाली shelf के बारे में सोचें, हर slot में एक item रखा है, और आप हमेशा उसके slot number से कोई चीज़ ढूंढ सकते हैं, पहला slot, दूसरा slot, वगैरह, बिना हर चीज़ के लिए अलग-अलग labeled boxes की ज़रूरत के। JavaScript ऐरे बिल्कुल वैसी ही shelf है, values की एक ordered list जो एक ही variable name के अंदर रखी होती है, जिसे उसकी position से access किया जाता है, जिसे index कहते हैं, और जो zero से शुरू होती है। असली कोड में ऐरे हर जगह मिलते हैं, tutorial titles की एक list, quiz scores का एक सेट, या cookiescursor.com पर पढ़ाई जाने वाली languages, यह सब इस ordered, indexed structure में स्वाभाविक रूप से फिट बैठते हैं।
Syntax
javascript
const arrayName = [item1, item2, item3];
arrayName[index];
arrayName.length;

ऐरे बनाना

ऐरे square brackets [ ] से बनाए जाते हैं, जिनके अंदर values की एक comma-separated list होती है, जो numbers, strings, objects, या यहाँ तक कि दूसरी ऐरे भी हो सकती हैं, सब एक ही ऐरे के अंदर।

Note: हालांकि ऐरे technically mixed types रख सकती है, लेकिन ऐरे के elements को एक ही type का रखना आमतौर पर code को समझना आसान बनाता है।
Warning: आखिरी element के बाद एक trailing comma, आमतौर पर हानिरहित होते हुए भी, context के हिसाब से कभी-कभी एक अनचाहा खाली slot बना सकता है।

उदाहरण: Creating Arrays

javascript
// Declare the constant `numbers`, set to `[1, 2, 3]`
// Declare the constant `numbers`, set to `[1, 2, 3]`
const numbers = [1, 2, 3];
// Declare the constant `mixed`, set to `["text", 5, true]`
// Declare the constant `mixed`, set to `["text", 5, true]`
const mixed = ["text", 5, true];
// Print `numbers, mixed` to the console
// Print `numbers, mixed` to the console
console.log(numbers, mixed);

Elements Access करना

ऐरे के elements को zero-based index के साथ square brackets से access किया जाता है, arrayName[0] पहला element लाता है, arrayName[1] दूसरा, वगैरह। जो index मौजूद ही नहीं है उसे पढ़ने की कोशिश करने पर error आने के बजाय undefined मिलता है।

Note: आखिरी element को भरोसेमंद तरीके से access करने के लिए arrayName[arrayName.length - 1] इस्तेमाल करें, चाहे ऐरे में कितने भी items हों।
Warning: किसी ऐसे index को access करना जो मौजूद ही नहीं है, जैसे 10-element ऐरे का index 20, error देने के बजाय undefined return करता है।

उदाहरण: Accessing Elements

javascript
const fruits = ["apple", "banana", "cherry"];
console.log(fruits[0]);
console.log(fruits[5]); // undefined, index doesn't exist

Array Length

हर ऐरे में एक length property होती है जो बताती है कि उसमें फिलहाल कितने elements हैं, और elements जोड़ने या हटाने पर यह अपने आप अपडेट हो जाती है। length को सीधे लिखा भी जा सकता है, और इसे मैन्युअली घटाने से ऐरे के अंत से elements असल में डिलीट हो जाते हैं।

Note: length का आमतौर पर उन loops में एक stopping condition के तौर पर इस्तेमाल होता है जिन्हें ऐरे के हर element तक जाना होता है।
Warning: ऐरे की length को मैन्युअली किसी छोटी संख्या पर सेट करना, जैसे myArray.length = 2, असल में extra elements को डिलीट कर देता है, यह गलती से होना आसान है।

उदाहरण: Array Length

javascript
const items = ["a", "b", "c"];
console.log(items.length);
items.length = 1;
console.log(items); // ["a"], shrinking deletes elements

Elements बदलना

किसी individual element को उसके index पर सीधे एक नई value assign करके बदला जा सकता है, arrayName[index] = newValue, जो सिर्फ उस एक slot को बदलता है, बाकी ऐरे पर कोई असर नहीं पड़ता।

Note: index से किसी element को बदलना एक direct, in-place modification है, कोई नई ऐरे नहीं बनती, original ऐरे खुद ही अपडेट हो जाती है।
Warning: ऐरे की current length से बहुत आगे के किसी index पर value assign करने से बीच में खाली slots बन जाते हैं, जिससे confusing gaps पैदा हो सकते हैं।

उदाहरण: Changing Elements

javascript
// Declare the constant `colors`, set to `["red", "green", "blue"]`
// Declare the constant `colors`, set to `["red", "green", "blue"]`
const colors = ["red", "green", "blue"];
colors[1] = "yellow";
// Print `colors` to the console
// Print `colors` to the console
console.log(colors);

Arrays के साथ Spread Operator

Spread operator ... किसी ऐरे के elements को अलग-अलग करके फैला देता है, जिससे पूरी ऐरे को copy करना या कई ऐरे को मिलाकर एक नई ऐरे बनाना आसान हो जाता है।

यह original को बदलने के बजाय एक नई ऐरे बनाता है, इसीलिए यह गलती से mutation से बचने का एक आम tool है।

Note: किसी नई ऐरे में spread करना, जैसे [...original], एक असली copy बनाता है, copy में किए गए बदलाव original ऐरे पर असर नहीं डालते।
Warning: Spread operator सिर्फ एक shallow copy करता है, अंदर मौजूद nested arrays या objects अभी भी shared references ही रहते हैं, deep copies नहीं।

उदाहरण: The Spread Operator With Arrays

javascript
// Declare the constant `original`, set to `[1, 2, 3]`
// Declare the constant `original`, set to `[1, 2, 3]`
const original = [1, 2, 3];
// Declare the constant `copy`, set to `[...original]`
// Declare the constant `copy`, set to `[...original]`
const copy = [...original];
// Declare the constant `combined`, set to `[...original, 4, 5]`
// Declare the constant `combined`, set to `[...original, 4, 5]`
const combined = [...original, 4, 5];
// Print `copy, combined` to the console
// Print `copy, combined` to the console
console.log(copy, combined);
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. यह भूल जाना कि arrays zero-indexed होते हैं, पहला element index 0 पर होता है, 1 पर नहीं।
  2. array की length से आगे किसी index को access करने की कोशिश करना, जो error throw करने के बजाय undefined return करता है।
  3. array length को last valid index से confuse करना, last index हमेशा length minus one होता है।
चैप्टर सारांश
  • Arrays values की ordered lists store करते हैं, जो एक zero-based numeric index से access की जाती हैं।
  • length property बताती है कि किसी array में कितने elements हैं, और किसी specific index पर assign करके elements बदले जा सकते हैं।
  • Spread operator arrays को जल्दी copy या combine कर सकता है, और दर्जनों built-in methods array contents को manipulate करते हैं।
ब्राउज़र सपोर्ट

Arrays और उनके core operations fundamental JavaScript features हैं जो हर modern browser में समान रूप से supported हैं।

🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 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.