JS ऐरे
In this page:
const arrayName = [item1, item2, item3];
arrayName[index];
arrayName.length;
ऐरे बनाना
ऐरे square brackets [ ] से बनाए जाते हैं, जिनके अंदर values की एक comma-separated list होती है, जो numbers, strings, objects, या यहाँ तक कि दूसरी ऐरे भी हो सकती हैं, सब एक ही ऐरे के अंदर।
उदाहरण: Creating Arrays
// 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 मिलता है।
उदाहरण: Accessing Elements
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 असल में डिलीट हो जाते हैं।
उदाहरण: Array Length
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 को बदलता है, बाकी ऐरे पर कोई असर नहीं पड़ता।
उदाहरण: Changing Elements
// 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 है।
उदाहरण: The Spread Operator With Arrays
// 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);
- यह भूल जाना कि arrays zero-indexed होते हैं, पहला element index 0 पर होता है, 1 पर नहीं।
- array की length से आगे किसी index को access करने की कोशिश करना, जो error throw करने के बजाय undefined return करता है।
- 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: