当前位置:网站首页>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() 数组方法 匿名回调函数感谢各位的支持!
记得支持二锅头,一键三连奥!
边栏推荐
猜你喜欢

Linux操作系统(Centos7)安装MySQL

Introduction to SD card (based on spec3.0)

多线程一定能优化程序性能吗?

Basic operation of MATLAB

SD卡介绍(基于SPEC3.0)
![[log] what does a log do? What is a log factory? Configuration and use of log4j? log4j. Properties file configuration, log4j jar package coordinates](/img/ae/096b558bc1342447205b442a244aae.png)
[log] what does a log do? What is a log factory? Configuration and use of log4j? log4j. Properties file configuration, log4j jar package coordinates

Pulse style | exclusive interview with Committee -- Tencent engineer Zhang Dawei calls you to eat "crab"

Detailed explanation of various types of files in MySQL

This wechat plug-in is very easy to use

WPF布局之控件随着窗口等比放大缩小,适应多分辨率满屏填充应用
随机推荐
fastjson中@jsonType注解的功能简介说明
Analysis of HashSet internal principle
Shell implements the backup / recovery / migration functions of harbor v1/v2
常用工具函数 持续更新
Net 3 lines of code to realize the function of text to speech
Can multithreading optimize program performance?
Method parameter transfer mechanism of C #
Buckle 376 swing sequence greedy
C countdown tool
Heuristic merging on tree
OSS直连上传-Rails服务实践
Take you to wechat applet development in 3 minutes
数据库高级技术学习笔记1--Oracle部署和PL/SQL综述
Domain events and integration events are not so big
ARouter源码解析(三)
include 与 require include_once 与 require_once 的区别
This wechat plug-in is very easy to use
Translation recommendation | debugging bookkeeper protocol - unbounded ledger
OSS direct upload rails service practice
NTU Lin Xuantian's "machine learning cornerstone" problem solving and code implementation | [you deserve it]