function
用function来命名,function命令声明的代码区块,就是一个函数,function命令后面是函数名,函数名后面是一对圆括号,里面是传入函数的参数,函数体放在大括号里面。
add();
add();
function add(){
var x=10;
var y=20;
console.log(x+y);
}
add();
//注意add()可以放到前面调用也可以放到后面调用
//在控制台输出10,20,30
也可以改变里面参数的值
<script>
add(10,20);//10对应的x,20对应的y在控制台是输出30
add(20,20);
function add(x,y){
console.log(x + y);
}
add(30,30);
//add调用了3次,每一次里面的数字相加,所以就得到,30,40,60
</script>
return返回值
function add(x,y){
return x+y
}
var result =add(10,20);
console.log(result);
结果展示
Math()
Math.abs绝对值
math.max(1,2,6,4,)//返回6
Math.max最大值
math.max(1,2,6,4,)//返回6
Math.min最小值
math.min(1,2,4,6,4,)//返回1
Math.floor向下取整
math.floor(10.3)//返回10
Math.ceil向上取整
math.ceil(10.3)//返回11
Math.rangdom()大于等于0小于等于1的随机数
//十到二十之间的随机数
function aa(min,max){
var result=Math.random()*(max-min)+min
//因为Math.random的取值范围是[0,1)乘以10=[0,10)+最小值10=[10,20)
result=Math.floor(result)
console.log(result);
}
aa(10,20)
Date对象
date对象以1970年00:00:00:作为时间的零点,可以表示前后各1亿天。
Date.now()
get
getTime():返回距离1970年1月1日开始的毫秒数
getDate():返回对象对应每个月的几号(从1开始)
getDay():返回星期几,星期日为0,星期一为1
getYear():返回距离1900年的年数
getFulYear():返回四位年份
getMonth():返回月份(0表示1月,11表示12月)
getHours():返回小时0到23
getMilliseconds():返回毫秒(0-999)
getMinutes():返回分钟
getSeconds():返回秒
获取当前时间的年月日
<body>
<script>
console.log(Date.now());//获取时间托//"1707194430392"
var year=new
Date(1707194430392).getFullYear()//获取年
var month=new
Date(1707194430392).getMonth()+1//因为月默认是从0到11月,所以后面要加上1
var day=new Date(1707194430392).getDate()//获取日
//把他们用-连在一起就得到了当前日期的年-月-日
console.log(year+"-"+month+"-"+day)
</script>
</body>
结果展示