当前位置:网站首页>JS create an array (literal)
JS create an array (literal)
2022-06-23 15:11:00 【Liujiayi_】
(1) Use literals to create arrays // grammar :[]
var arr = [];
(2) When you create an array using literals , You can specify the elements in the array at creation time
var arr = [1,2,3,4,5,10];
(3) When creating an array using a constructor , You can also add elements at the same time , The element to be added is passed as a parameter of the constructor Use... Between elements , separate
var arr2 =new Array( 10,20,30);
console.log( arr2);
(4) Create an array with only one element 10
arr = [ 10];
(5) Create a length of 10 Array of
arr2 = new Array ( 10);
(6) The elements in the array can be any data type
arr = [ "hello",1,true,nul1,undefined];
It could be an object
arr= [{name : " The Monkey King "},{name : " The sand monk "},{name : " Pig eight quit "}];
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
arr = [{ name: " The Monkey King " }, { name: " The sand monk " }, { name: " Pig eight quit " }];
alert(arr[1].name);
</script>
</body>
</html>

(7) It can also be a function
arr = [function(){alert(1)},function(){alert(2)}];
console.log( arr);
arr[0]();

(8) You can also put an array in the array , The following array is called two-dimensional array
arr = [[1,2,3],[3,4,5],[5,6,7]];
console.log( arr[1]);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
arr = [
[1, 2, 3],
[3, 4, 5],
[5, 6, 7],
];
alert(arr[1]);
</script>
</body>
</html>

边栏推荐
- 力扣解法汇总513-找树左下角的值
- Idea view View the class file idea Class folder
- Millions of bonuses are waiting for you to get. The first China Yuan universe innovation and application competition is in hot Recruitment!
- 2021-04-15
- useState vs useRef 和 useReducer:相同点、不同点和用例
- 山东:美食“隐藏款”,消费“扫地僧”
- SFOD:无源域适配升级优化,让检测模型更容易适应新数据(附论文下载)
- JS创建一个数组(字面量)
- The team of China University of Mines developed an integrated multi-scale deep learning model for RNA methylation site prediction
- Google &huggingface| zero sample language model structure with the strongest ability
猜你喜欢

Self inspection is recommended! The transaction caused by MySQL driver bug is not rolled back. Maybe you are facing this risk!

【二级等保】过二级等保用哪个堡垒机品牌好?

Uniswap acquires genie, an NFT transaction aggregator. Will the NFT transaction market change?

Auto - vérification recommandée! Les bogues MySQL ne font pas reculer les transactions, peut - être êtes - vous à risque!

K8s-- deploy stand-alone MySQL and persist it

js的slice()和splice()

Ie mode of selenium edge

SQL注入漏洞(原理篇)

5分钟快速上线Web应用和API(Vercel)

AXI_ Round_ Robin_ Arbiter design - aw and W channels
随机推荐
WebService interface publishing and calling
k8s--部署单机版MySQL,并持久化
Introduction to helm basics helm introduction and installation
Raspberry PI installing the wiring pi
Xampp中mysql无法启动问题的解决方法
力扣解法汇总513-找树左下角的值
港股今年最大IPO来了,660亿身家,坐在矿山上的“大王”
力扣解法匯總513-找樹左下角的值
Self inspection is recommended! The transaction caused by MySQL driver bug is not rolled back. Maybe you are facing this risk!
AI intelligent robot saves us time and effort
如何解决 Iterative 半监督训练 在 ASR 训练中难以落地的问题丨RTC Dev Meetup
How is it safe to open an account for futures? Which futures company has a relatively low handling fee for futures and is suitable for retail investors to open an account?
How to use note taking software flowus and note for interval repetition? Based on formula template
用OBS做直播推流简易教程
K8s-- deploy stand-alone MySQL and persist it
Summary of operating system underlying knowledge (interview)
MySQL advanced statement 2
What do you mean by waiting for insurance records? Where should I go for filing?
2021-04-15
Uniswap 收购 NFT交易聚合器 Genie,NFT 交易市场将生变局?