Describe all the ways of creating arrays in Java script.

Creating Array in Java Script :-

Array एक प्रकार का data structure है जहा हम elements को ordered list में store कर सकते है।

Javascript में array को main 3 तरीके से create कर सकते है वह हम step by step detail

Assignment operator का इस्तेमाल करके javascript में array कोcreate कर सकते है।

Javascript में खास तोर पर assignment operator का इस्तेमाल करके array को create किया जाता है।.

 

Example:

Const language = [“php”,”javascript”,”python”,”java”];

अगर हम language array variable को console करते है तो array हमें कुछ इस तरह दिखेगा।.

Const language = [“php”,”javascript”,”python”,”java”];

Console.log( language );

New operator का इस्तेमाल करके javascript में array को create कर सकते है

Javascript में new keyword का इस्तेमाल करके दूसरी तरीके से array create कर सकते है।.

 

Syntax:

New Array()

यदि हम number parameter को parenthesis में pass करते है तो वह new array की length की तोर पर set होती है।.

 

Example:

New Array(4)

ऊपर के example में देख सकते है की 4 number parameter की तोर पर Array() में पास की है तो हम 4 empty slots की length के साथ एक array create करते है।.

Array() में multiple paramtersको पास करके array को कैसे create कर सकते है।.

 

Example:

Let tech = new Array(“HTML”,”css”,”Javascript”,”php”);

Console.log(tech);

Console.log(tech.length);

Console.log(tech[1]);

 

Array.of() method का इस्तेमाल करके javascript में array कोcreate कर सकते है

Array.of() method का इस्तेमाल करके array को create कर सकते है. यह method कई number of arguments लेती है और एक नया array बनाती है।.

 

Example:

Let tech = Array.of(“HTML”,”css”,”Javascript”,”php”)

Console.log(tech);

Console.log(tech.length);

Console.log(tech[1]);

ये method array constructor के साथ बिलकुल similar है।. लेकिन उसका key difference यह है की यदि हम Array.of() method का इस्तेमाल करके एक single number parameter के तोर पर pass करते है तो वह pass किया number के साथ array की length लौटाएगा लेकिन array constructor उस number के लिए empty sloteबनता है।.

 

Array.of() example:

Let myArr = Array.of(4);

Console.log( myArr );

Array constructor example:

Let myArr = new Array(4);

Console.log( myArr );

Split() method का इस्तेमाल करके string से array create कर सकते  है javascript में।

Split() method का इस्तेमाल करके string को array में convert kiyaजा सकता है।.

 

Javascript split() method syntax:

String.split(separator, limit);

 

Example:

Let text = “How are you doing today?”;

ConstmyArray = text.split(““);

Console.log( myArray  );

ऊपर के example में देख सकते है की “How are you doing today?” name की string है।. तो इस string को split() method के इस्तेमाल से break किया है।. इस string को हमने word के bitch के space से separate किया है।.

Speak Your Mind

*

error: Content is protected !!