学习来源:尚硅谷JavaScript基础&实战丨JS入门到精通全套完整版
var arr = new Array();
数组[索引] = 值
数组[索引]
,如果读取不存在的索引,他不会报错而是返回undefined。数组.length
,对于连续的数组,使用length可以获取到数组的长度(元素的个数),对于非连续的数组,使用length会获取到数组的最大的索引+1,尽量不要创建非连续的数组。arr.length = 10;
,如果修改的length大于原长度,则多出部分会空出来,如果修改的length小于原长度,则多出的元素会被删除。数组[数组.length] = 值;
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript">
//创建数组对象
var arr = new Array();
//向数组中添加元素
arr[0] = 10;
arr[1] = 33;
arr[2] = 22;
arr[3] = 44;
//获取数组的长度
console.log(arr.length);
//修改length
//如果修改的length大于原长度,则多出部分会空出来
//如果修改的length小于原长度,则多出的元素会被删除
arr.length = 10;
console.log(arr.length);
//向数组的最后一个位置添加元素
//语法:数组[数组.length] = 值;
arr[arr.length] = 70;
arr[arr.length] = 80;
arr[arr.length] = 90;
console.log(arr);
script>
head>
<body>
body>
html>
语法:[]
。var arr = [1,2,3,4,5,10];
。,
隔开,var arr2 = new Array(10,20,30);
arr2 = new Array(10);
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript">
//使用字面量来创建数组,语法:[]
var arr = [];
//使用字面量创建数组时,可以在创建时就指定数组中的元素
var arr = [1,2,3,4,5,10];
//使用构造函数创建数组时,也可以同时添加元素,将要添加的元素作文构造函数的参数传递
//元素之间使用,隔开
var arr2 = new Array(10,20,30);
//创建一个长度为10的数组
arr2 = new Array(10);
//数组中的元素可以是任意的数据类型
arr = ["hello",1,true,null,undefined];
//可以是对象
var obj = {name:"孙悟空"};
arr[arr.length] = obj;
arr = [{name:"孙悟空"},{name:"沙和尚"},{name:"猪八戒"}];
//也可以是一个函数
arr = [function(){alert(1)},function(){alert(2)}];
//数组中也可以放数组,如下这种数组我们称为二维数组
arr = [[1,2,3],[3,4,5],[5,6,7]];
console.log(arr[1]);
script>
head>
<body>
body>
html>
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript">
//创建一个数组
var arr = ["孙悟空","猪八戒","沙和尚"];
//该方法可以向数组的末尾添加一个或多个元素,并返回数组的新的长度
var result = arr.push("唐僧","蜘蛛精","白骨精","玉兔精");
console.log(arr);
console.log("result = "+result);
script>
head>
<body>
body>
html>
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript">
//创建一个数组
var arr = ["孙悟空","猪八戒","沙和尚"];
result = arr.pop();
console.log(arr);
console.log("result = "+result);
script>
head>
<body>
body>
html>
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript">
//创建一个数组
var arr = ["孙悟空","猪八戒","沙和尚"];
result = arr.unshift("牛魔王","二郎神");
console.log(arr);
console.log("result = "+result);
script>
head>
<body>
body>
html>
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript">
//创建一个数组
var arr = ["孙悟空","猪八戒","沙和尚"];
result = arr.shift();
console.log(arr);
console.log("result = "+result);
script>
head>
<body>
body>
html>
-1
:倒数第一个DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript">
var arr = ["孙悟空","猪八戒","沙和尚","唐僧","白骨精"];
var result = arr.slice(1,4);
console.log(result);
result = arr.slice(3);
console.log(result);
result = arr.slice(1,-2);
console.log(result);
script>
head>
<body>
body>
html>
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript">
var arr = ["孙悟空","猪八戒","沙和尚","唐僧","白骨精"];
var result = arr.splice(3,1,"牛魔王","铁扇公主","红孩儿");
console.log(arr);
console.log(result);
script>
head>
<body>
body>
html>
DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>title>
<script type="text/javascript">
var arr = ["孙悟空","猪八戒","沙和尚"];
var arr2 = ["白骨精","玉兔精","蜘蛛精"];
var arr3 = ["二郎神","太上老君","玉皇大帝"];
var result = arr.concat(arr2,arr3,"牛魔王","铁扇公主");
console.log(result)
script>
head>
<body>
body>
html>
,
作为连接符。DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>title>
<script type="text/javascript">
var arr = ["孙悟空","猪八戒","沙和尚"];
var arr2 = ["白骨精","玉兔精","蜘蛛精"];
var arr3 = ["二郎神","太上老君","玉皇大帝"];
result = arr.join("@-@");
console.log(result)
//孙悟空@-@猪八戒@-@沙和尚
script>
head>
<body>
body>
html>
DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>title>
<script type="text/javascript">
var arr = ["孙悟空","猪八戒","沙和尚"];
var arr2 = ["白骨精","玉兔精","蜘蛛精"];
var arr3 = ["二郎神","太上老君","玉皇大帝"];
arr.reverse();
console.log(arr);
script>
head>
<body>
body>
html>
DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>title>
<script type="text/javascript">
var arr = ["孙悟空","猪八戒","沙和尚"];
var arr2 = ["白骨精","玉兔精","蜘蛛精"];
var arr3 = ["二郎神","太上老君","玉皇大帝"];
arr.sort();
console.log(arr);
script>
head>
<body>
body>
html>
DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>title>
<script type="text/javascript">
arr = [5,4,2,1,3,6,8,7];
arr.sort(function(a,b){
//前边的大
if(a > b){
return -1;
}else if(a < b){
return 1;
}else{
return 0;
}
//升序排列
//return a - b;
//降序排列
return b - a;
});
console.log(arr);
script>
head>
<body>
body>
html>
for循环遍历
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript">
//创建一个数组
var arr = ["孙悟空","猪八戒","沙和尚","唐僧","白骨精"];
//所谓的遍历数组,就是将数组中所有的元素都取出来
/*console.log(arr[0]);
console.log(arr[1]);
console.log(arr[2]);
console.log(arr[3]);*/
for(var i=0 ; i<arr.length ; i++){
console.log(arr[i]);
}
script>
head>
<body>
body>
html>
forEach()
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript">
//创建一个数组
var arr = ["孙悟空","猪八戒","沙和尚","唐僧","白骨精"];
arr.forEach(function(value , index , obj){
console.log(value);
});
script>
head>
<body>
body>
html>
call()
和apply()
:
call()
方法可以将实参在对象之后依次传递apply()
方法需要将实参封装到一个数组中统一传递DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript">
function fun(a,b) {
console.log("a = "+a);
console.log("b = "+b);
}
var obj = {
name: "obj",
sayName:function(){
alert(this.name);
}
};
//实参在对象之后依次传递
fun.call(obj,2,3);
//实参封装到一个数组中统一传递
fun.apply(obj,[2,3]);
var obj2 = {
name: "obj2"
};
obj.sayName.apply(obj2);
script>
head>
<body>
body>
html>
this
arguments
arguments.length
可以用来获取实参的长度。DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript">
function fun(a,b){
//false,arguments对象虽然可以通过索引访问其元素,但它并不是一个真正的数组。
console.log(arguments instanceof Array);
//false,同样的原因,arguments对象也不是一个数组类型。
console.log(Array.isArray(arguments));
//true,传入的第二个参数为true,所以arguments[1]返回的值为true。
console.log(arguments[1]);
//2,传入的参数个数为2。
console.log(arguments.length);
//true,arguments.callee指向当前正在执行的函数本身,所以这里输出true。
console.log(arguments.callee == fun);
}
fun("hello",true);
script>
head>
<body>
body>
html>
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript">
//创建一个Date对象
//如果直接使用构造函数创建一个Date对象,则会封装为当前代码执行的时间
var d = new Date();
console.log(d);
//创建一个指定的时间对象
//需要在构造函数中传递一个表示时间的字符串作为参数
//日期的格式 月份/日/年 时:分:秒
var d2 = new Date("7/29/2023 18:13:14");
console.log(d2);
//getDate():获取当前日期对象是几日
var date = d2.getDate();
console.log("date = "+date);
//getDay():获取当前日期对象是周几
//会返回一个0-6的值,0 表示周日,1表示周一
var day = d2.getDay();
console.log("day = "+day);
//getMonth():获取当前时间对象的月份
//会返回一个0-11的值,0 表示1月,1 表示2月
var month = d2.getMonth();
console.log("month = "+month);
//getFullYear():获取当前日期对象的年份
var year = d2.getFullYear();
console.log(year);
//getTime():获取当前日期对象的时间戳
//时间戳,指的是从格林威治标准时间的1970年1月1日,0时0分0秒,到当前日期所花费的毫秒数(1秒 = 1000毫秒)
//计算机底层在保存时间时使用都是时间戳
var time = d2.getTime();
console.log(time);
//利用时间戳来测试代码的执行的性能
//获取当前的时间戳
var start = Date.now();
for(var i=0 ; i<100 ; i++){
console.log(i);
}
var end = Date.now();
console.log("执行了:"+(end - start)+"毫秒");
script>
head>
<body>
body>
html>
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript">
console.log(Math.PI);
//abs()可以用来计算一个数的绝对值
console.log(Math.abs(-1));
//Math.ceil()
//可以对一个数进行向上取整,小数位只有有值就自动进1
console.log(Math.ceil(3.123))
//Math.floor()
//可以对一个数进行向下取整,小数部分会被舍掉
console.log(Math.floor(3.123))
//Math.round()
//可以对一个数进行四舍五入取整
console.log(Math.round(1.4));
//Math.random()
x=10;
y=100;
//可以用来生成一个0-x之间的随机数
console.log(Math.round(Math.random()*x));
//生成一个x-y之间的随机数
console.log(Math.round(Math.random()*(y-x)+x));
//max() 可以获取多个数中的最大值
//min() 可以获取多个数中的最小值
var max = Math.max(10,45,30,100);
console.log(max);
var min = Math.min(10,45,30,100);
console.log(min);
//Math.pow(x,y):返回x的y次幂
console.log(Math.pow(12,3));
//Math.sqrt():用于对一个数进行开方运算
console.log(Math.sqrt(2));
script>
head>
<body>
body>
html>
在JS中为我们提供了三个包装类,通过这三个包装类可以将基本数据类型的数据转换为对象:
String()
:可以将基本数据类型字符串转换为String对象。Number()
:可以将基本数据类型的数字转换为Number对象。Boolean()
:可以将基本数据类型的布尔值转换为Boolean对象。DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript">
//创建一个Number类型的对象
var num = new Number(3);
var str = new String("hello");
var str2 = new String("hello");
var bool = new Boolean(true);
//向num中添加一个属性
num.hello = "abcdefg";
console.log(num.hello);
console.log(str === str2);
if(bool){
alert("我运行了~~~");
}
var s = 123;
s = s.toString();
s.hello = "你好";
//由于s变量是一个基本类型的字符串,而不是一个字符串对象,
//所以s.hello无法访问到该属性,输出结果为undefined。
console.log(s.hello);
script>
head>
<body>
body>
html>
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript">
//创建一个字符串
var str = "Hello Atguigu";
//在底层字符串是以字符数组的形式保存的,["H","e","l"]
console.log(str.length);
console.log(str[5]);
script>
head>
<body>
body>
html>
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript">
//创建一个字符串
var str = "Hello Atguigu";
str = "中Hello Atguigu";
var result = str.charAt(0);
console.log(result);
script>
head>
<body>
body>
html>
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript">
//创建一个字符串
var str = "Hello Atguigu";
str = "中Hello Atguigu";
result = str.charCodeAt(0);
console.log(result);//20013
script>
head>
<body>
body>
html>
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript">
//创建一个字符串
var str = "Hello Atguigu";
str = "中Hello Atguigu";
result = String.fromCharCode(0x2692);
console.log(result);//锤子??!
script>
head>
<body>
body>
html>
+
一样。DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript">
//创建一个字符串
var str = "Hello Atguigu";
str = "中Hello Atguigu";
result = str.concat("你好","再见");
console.log(result);
script>
head>
<body>
body>
html>
indexof()
lastIndexOf();
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript">
//创建一个字符串
var str = "Hello Atguigu";
result = str.indexOf("H",1);
console.log(result);//-1
result = str.indexOf("H");
console.log(result);//0
result = str.lastIndexOf("g",5);
console.log(result);//-1
script>
head>
<body>
body>
html>
参数
:
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript">
str = "abcdefghijk";
result = str.slice(1,4);
console.log(result);//bcd
result = str.slice(1,-1);
console.log(result);//bcdefghij
script>
head>
<body>
body>
html>
参数
:DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript">
str = "abcdefghijk";
result = str.substring(0,1);
console.log(result);//a
redult = str.substring(1,-1);
console.log(result);//a
script>
head>
<body>
body>
html>
参数
:
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript">
str = "abcdefghijk";
result = str.substr(3,2);
console.log(result);//de
script>
head>
<body>
body>
html>
参数
:需要一个字符串作为参数,将会根据该字符串去拆分数组DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript">
str = "abcbcdefghij";
result = str.split("d");
console.log(result);
//如果传递一个空串作为参数,则会将每个字符都拆分为数组中的一个元素
result = str.split("");
console.log(result);
script>
head>
<body>
body>
html>
toUpperCase()
:将一个字符串转换为大写并返回。toLowerCase()
:将一个字符串转换为小写并返回。DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title>
<script type="text/javascript">
str = "abcbcdefghij";
result = str.toUpperCase();
console.log(result);//ABCBCDEFGHIJ
str = "ABCDEFG";
result = str.toLowerCase();
console.log(result);//abcdefg
script>
head>
<body>
body>
html>