当前位置:网站首页>j s的数组方法,循环
j s的数组方法,循环
2022-07-28 09:30:00 【来康二锅头】
一,函数
1.定义函数
function 函数名(参数1,参数2,… ) {
//JavaScript语句;
[return 返回值]
}
2.调用函数 事件名="函数名( )" ;
3.变量的作用域 (1)全局变量 (2)局部变量
var i=20;
function first(){
var i=5;
document.write("<br/>函数first中的i值为:"+i);
}
function second(){
var t=document.getElementById("number").value;
if(t>i){
document.write("函数second中的t值为:"+t);}
else{
document.write("全局变量中的i值为:"+i);
}
first();
}
二,数组(具有相同数据类型的一个或多个值的集合)
创建数组
var 数组名称 = new Array(size);
new:表示数组的关键字
size:表示数组中可存 放的元素总数
***JavaScript 中的数组用一个名称存储一系列的值,用下标区分数组中的每个值,数组的下标从0开始***
数组赋值
声明时赋值
var week= new Array ("星期日","星期一"……);
分别赋值
var week= new Array ( 7);
week[0] = "星期日";
week[1] = "星期一 ";
week[2] = "星期二";
week[3] = "星期三";
……
访问数组元素
通过数组的名称和下标直接访问数组的元素
数组常用方法和属性
| 类别 | 名称 | 说明 |
| 属性 | length | 设置或返回数组中元素的数目 |
| 方法 | join() | 把数组的所有元素放入一个字符串,通过一个的分隔符进行分隔 |
| sort() | 对数组排序 | |
| push() | 向数组末尾添加一个或更多 元素,并返回新的长度 |
js数组语法
push() 向数组末尾追加元素
pop() 删除数组末尾元素
unshift()向数组头部添加元素
shift() 删除数组头部元素
splice()删除数组中的元素(第一个参数为起始索引,第二个为删除数目)
三,循环
什么是循环?
循环就是在满足条件的情况下,去不断重复的执行某一个操作
循环的优势
1.解决重复操作
2.减少代码编写量,使代码结构清晰
3.增强代码的可读性
循环的结构
1.循环条件
2.循环操作
循环的分类
1.for循环结构
2.while循环结构
// 1、for循环 下标 / key / 键 【已知条件,已知范围(先判断,后执行)】
/* for(let i = 0; i < arr.length;i++){
document.write(arr[i]+"<br/>");
}
*/
// 2、 while()循环 下标 / key / 键 【未知条件,未知范围(先判断,后执行)】
/* let i = 0;
while(i < arr.length){
document.write(arr[i]+"<br/>");
i++;
} */
//3、do while()循环 下标 / key / 键 【至少执行一次(先执行,后判断)】
/* let i = 0;
do{
//循环代码块
document.write(arr[i]+"<br/>");
i++;
}
while(i < arr.length); */
// 4、for of 循环 ES5 值 / value / 元素 【循环遍历数组】
/* for(let value of arr){
document.write(value+"<br/>");
} */
// 5、 for in 循环 ES6 下标 / key / 键
/* for(let i in arr){
document.write(arr[i]+"<br/>");
} */
// 6、 forEach() 数组方法 匿名回调函数感谢各位的支持!
记得支持二锅头,一键三连奥!
边栏推荐
猜你喜欢

就这么一个简单的校验,80%的程序员却做不到,更不理解!

力扣376-摆动序列——贪心

SD卡介绍(基于SPEC3.0)

关于CLR GC调优的一些问题

软件测试与质量学习笔记2----黑盒测试

My vivado practice - single cycle CPU instruction analysis

Domain events and integration events are not so big

JS提升:实现flat平铺的底层原理

Business visualization - make your flowchart'run'(4. Actual business scenario test)

使用Xposed对软件进行破解
随机推荐
The secret behind three salary increases a year
MATLAB的数列与极限运算
Business visualization - make your flowchart'run'(4. Actual business scenario test)
Seeing clearly is more important than walking fast, because you can go far only when you walk right
数据库高级学习笔记--游标
Introduction to shardingsphere (I)
Retrofit源码解析
My vivado practice - single cycle CPU instruction analysis
SeekTiger生态通证STI 新进展,4月14日登录 ZB
OSS direct upload rails service practice
ActivityRouter源码解析
就这么一个简单的校验,80%的程序员却做不到,更不理解!
3 minutes to tell you how to become a hacker | zero foundation to hacker getting started guide, you only need to master these five abilities
CTF中常见的RSA相关问题总结:基础RSA加密与解密
NTU Lin Xuantian's "machine learning cornerstone" problem solving and code implementation | [you deserve it]
Domain events and integration events are not so big
Changes in the relationship between data and application in IT industry
【日志】日志干什么的?日志工厂是什么?log4j 的配置和使用? log4j.properties 文件配置、log4j jar包坐标
Deepin 下安装 LAMP
在Plato Farm新经济模型下,如何在游戏中获取更多MARK