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

JS टाइप्ड ऐरे

एक साधारण JavaScript ऐरे एक flexible shopping bag जैसी होती है जो किसी भी size की कोई भी चीज़ रख सकती है, ज़रूरत के हिसाब से बढ़ती-घटती हुई; एक typed array ज़्यादा एक labeled egg carton जैसी है, slots की एक तय संख्या जिनमें से हर एक बिल्कुल एक खास तरह का number ही रखता है। यह कठोरता ही पूरी बात है: यह JavaScript को binary data को उतनी ही कुशलता से पढ़ने और लिखने देती है जितनी lower-level languages।
Syntax
javascript
const typedArray = new Int8Array(length);
const typedArray = new Float64Array([value1, value2]);

Typed Array क्या है

Typed array एक fixed-length, fixed-type array जैसा object है जो raw binary memory पर आधारित होता है, और pixel values, audio samples, या network packets जैसे numeric data पर सीधे काम करने के लिए इस्तेमाल होता है।

साधारण ऐरे के उलट, आप इसमें elements push या pop नहीं कर सकते, क्योंकि इसकी length बनते समय एक बार तय हो जाती है।

उदाहरण: What Is a Typed Array

javascript
const arr = new Int8Array(3);
arr[0] = 5;
console.log(arr); // fixed length, no push/pop

ArrayBuffer और Views

ArrayBuffer raw, untyped binary memory का एक टुकड़ा है जिसे सीधे पढ़ने या लिखने का कोई तरीका नहीं है; एक typed array एक view है जो उन raw bytes को किसी खास type के numbers के रूप में समझती है।

अलग-अलग types की कई views बिल्कुल एक ही underlying ArrayBuffer को share कर सकती हैं, और उन्हीं bytes को अलग-अलग तरीके से समझ सकती हैं।

उदाहरण: ArrayBuffer and Views

javascript
// Declare the constant `buffer` as a new `ArrayBuffer` instance
// Declare the constant `buffer` as a new `ArrayBuffer` instance
const buffer = new ArrayBuffer(4);
// Declare the constant `view` as a new `Int32Array` instance
// Declare the constant `view` as a new `Int32Array` instance
const view = new Int32Array(buffer);
view[0] = 42;
// Print `view[0]` to the console
// Print `view[0]` to the console
console.log(view[0]);

आम Typed Array Types

JavaScript कई typed array constructors देता है जो अलग-अलग sizes और signedness को कवर करते हैं, जिनमें single bytes के लिए Int8Array और Uint8Array, बड़े whole numbers के लिए Int16Array और Int32Array, और decimals के लिए Float32Array और Float64Array शामिल हैं।

सही type चुनना memory usage और आपको store करने वाली values की range के बीच संतुलन बनाता है।

उदाहरण: Common Typed Array Types

javascript
// Declare the constant `bytes` as a new `Uint8Array` instance
// Declare the constant `bytes` as a new `Uint8Array` instance
const bytes = new Uint8Array(2);
// Declare the constant `ints` as a new `Int16Array` instance
// Declare the constant `ints` as a new `Int16Array` instance
const ints = new Int16Array(4);
// Declare the constant `floats` as a new `Float32Array` instance
// Declare the constant `floats` as a new `Float32Array` instance
const floats = new Float32Array(2);
// Print `bytes.length, ints.length, floats.length` to the console
// Print `bytes.length, ints.length, floats.length` to the console
console.log(bytes.length, ints.length, floats.length);

Mixed-Type Access के लिए DataView

DataView किसी ArrayBuffer के अंदर खास byte offsets पर values पढ़ने और लिखने पर fine-grained, explicit control देता है, जिसमें हर operation के लिए little-endian या big-endian byte order चुनना भी शामिल है।

यह उस समय सही tool है जब आप किसी binary file format या network protocol को parse कर रहे हों जो तय positions पर अलग-अलग data types मिलाता है।

उदाहरण: DataView for Mixed-Type Access

javascript
const buffer = new ArrayBuffer(4);
const view = new DataView(buffer);
view.setInt16(0, 300, true); // little-endian
console.log(view.getInt16(0, true));

Typed Arrays का इस्तेमाल क्यों करें

Typed arrays इसलिए मौजूद हैं क्योंकि JavaScript को WebGL graphics, audio processing, WebSockets, और file parsing जैसे कामों के लिए binary data को कुशलता से संभालने का एक तरीका चाहिए था, जहाँ एक साधारण flexible ऐरे कहीं ज़्यादा धीमी होती और ज़्यादा memory इस्तेमाल करती।

ये performance-sensitive या binary-protocol code के लिए बने lower-level tools हैं, रोज़मर्रा की application logic के लिए नहीं।

उदाहरण: Why Use Typed Arrays

javascript
const pixelData = new Uint8ClampedArray([255, 0, 0, 255]);
console.log(pixelData);
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. यह उम्मीद करना कि कोई typed array normal array की तरह बढ़ेगी, जबकि इसकी length बनते समय fixed हो जाती है और push() मौजूद नहीं होता।
  2. type की range से बाहर values store करना, जैसे new Uint8Array([300]), जो चुपचाप 44 पर wrap हो जाता है।
  3. buffer में fit न होने वाले offset या length के साथ एक view बनाना, जो RangeError throw करता है।
चैप्टर सारांश
  • Strings text के साथ काम करने के लिए methods रखती हैं।
  • Arrays values की lists store करते हैं और उन्हें बदलने और process करने के लिए methods रखते हैं।
  • Numbers और typed arrays numeric data handle करते हैं।
🔒

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.