JS数组、字符串、Math用法

//数组末尾追加一个数
arr.push(num);

//数组末尾删除一个数
arr.pop();

//数组的最前面添加一个元素
arr.unshift(num);

//删除数组最前面的一个元素
arr.shift();

//是截取数组中的某些内容,按照数组的索引来截取(从哪一个索引位置开始,截取多少个,替换的新元素(可以不写))
arr.splice(index01,num,changecontent);

//数组反转
arr.reverse()

//数组排序
arr.sort();

//数组拼接(返回拼接后的新数组)
arr.concat(newArr)

//连接数组里面的内容,变成字符串
arr.join(sign);

//根据索引查找值(数组中没有返回-1)
arr.indexOf(index);

//遍历数组
arr.forEach(function(item,index,arr){
    // item 就是数组中的每一项
    // index 就是数组的索引
    // arr 就是原始数组
});

//对数组中每一项进行操作
arr.map(function(item,index,arr){
    return item + 100;
});

//根据条件筛选数据(返回新数组)
arr.filter(function(item,index,arr){
    return item > 20;
});

//找到字符串中指定索引位置的内容返回
str.charAt(index);

//返回对应索引位置的 unicode 编码
str.charCodeAt(index);

//按照字符找到对应的索引
str.indexOf('');

//截取字符串(不包含后面的索引)
str.subString(startIndex,endIndex);
str.subStr(startIndex,num)

//字符串转成 小写字母 和 大写字母
str.toLowerCase();
str.toUpperCase();
//随机生成0-1之间的数
Math.random();

//将小数四舍五入
Math.round(num);

//绝对值
Math.abs(num);

//向上取整
Math.ceil(num)

//向下取整
Math.floor(num);

//取最大值
Math.max(num1,num2,....num);

//取最小值
Math.min(num1,num2,....num);

//π的值
Math.PI;
//拿到当前时间
new Date();

//获取自己想要的时间
new Date(time);

//两个参数,(年,月),月从0开始
new Date(year,month);

//三个参数(年,月,日)
new Date(year,month,day);

//四个参数(年,月,日,时)
new Date(year,month,day,hour);

//五个参数(年,月,日,时,分)
new Date(year,month,day,hour,min);

//六个参数(年,月,日,时,分,秒)
new Date(year,month,day,hour,min,sec);

//传入字符串类型
new Date('time');

//获取年份
date.getFullYear();

//获取月份(月份从0开始)
date.getMonth();

//获取天
date.getDate();

//获取小时
date.getHours();

//获取分钟
date.getMinutes();

//获取秒
date.getSeconds();

//获取日期是一周的第几天(周日为0,周六为6)
date.getDay();

//获取毫秒数
date.getTime();



//定时器返回值是一个数值

//倒计时定时器(1秒后执行函数内语句)
var timerId = setTimeout(function () {
    //语句
}, 1000);

//间隔定时器(每隔一秒执行一次)
var timerId = setInterval(function () {
    //语句
}, 1000)

//关闭定时器(可以混用)
clearTimeout(timerId)     //关闭倒计时定时器
coearInterval(timerId2)   //关闭间隔定时器
//获取浏览器的高度、宽度
window.innerHeight;
window.innerWidth;

//弹出层
window.alter('content');

//询问框
window.confirm('content');

//输出框
window.prompt('content');

//url地址信息
window.location.url;
window.location.url = "url";   //赋值跳转

//重新加载页面(刷新)  注意:不要写在全局,不然浏览器就会一直处在刷新状态
window.location.reload();

//历史记录:回到前一个页面(前提是你要有上一条记录)
window.history.back();

//去到下一个历史记录里面
window.history.forward();

//事件(页面加载完毕执行)
window.onload = function () {
    console.log('页面已经加载完毕')
}

//浏览器滚动时触发事件
window.onscroll = function () {
    console.log('浏览器滚动了')
}

//浏览器向上滚动的距离(Chrome浏览器没有 DOCTYPE 声明的时候,用 document.body.scrollTop;有 DOCTYPE 声明的时候,用 document.documentElement.scrollTop)
document.body.scrollTop;
document.documentElement.scrollTop

window.onscroll = function () {
    console.log(document.body.scrollTop)
    console.log(document.documentElement.scrollTop)
}

//页面向左滚动的距离
document.body.scrollLeft;
document.documentElementLeft;

window.onscroll = function () {
    console.log(document.body.scrollLeft)
    console.log(document.documentElement.scrollLeft)
}

//本地存储
localStorage.setItem("name","zhang");   //增
localStorage.getItem("name");           //取
localStorage.removeItem("name");        //删
localStorage.clear();                   //清空


sessionStorage.setItem("name","kerwin");  //增
sessionStorage.getItem("name");           //取
sessionStorage.removeItem("name");        //删
sessionStorage.clear();                   //清空

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值