学习JS基础知识:
1.引入方式:
点击查看代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!-- 内部脚本 -->
<!--<script>
alert("hello js");
</script>-->
<!-- 外部脚本 -->
<script src="../js/demo.js"></script>
</body>
</html>
2.输出语句:
点击查看代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
alert("hello js");//写入警告框,全称:window.alert
document.write("hello js 2");//写入html页面
console.log("hello js 3");//写入浏览器控制台
</script>
</body>
</html>
3.数据类型:
点击查看代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
alert("hello js");//写入警告框,全称:window.alert
document.write("hello js 2");//写入html页面
console.log("hello js 3");//写入浏览器控制台
</script>
</body>
</html>
4.运算符:
点击查看代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
/*
*
* ==:
* 1.判断类型是否一样,不一样则先进行类型转换
* 2.再比较其值
*
* ===:全等于
*
* */
/*var age1 = 20;
var age2 = "20";
alert(age1 == age2);//true
alert(age1 === age2);//false*/
/*
*
* 类型转换:
* *其他->number:
* *string:一般使用parseInt
* *按照字面值转为数字
* *字面值非数字->NaN
* *boolean:
* *true->1
* *false->0
*
* *其他->boolean:
* *number:
* *0和NaN->false
* *其他->true
* *string:
* *空字符串->false
* *其他->true
* *null:
* *->false
* *undefined
* *->false
*
* */
/*var str = +"abc";
var str1 = "20";
alert(str);
alert(parseInt(str1) + 1);*/
//var flag = 0;
//flag = 3;
//var flag = "";
//var flag = null;
var flag = undefined;
if (flag){
alert("to true");
}else {
alert("to false");
}
</script>
</body>
</html>
5.函数:
点击查看代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
function add(a,b){
return a+b;
}
//var result = add(1,2);
//var result = add(1,2,3,4);
var result = add(1);//NaN
alert(result);
</script>
</body>
</html>
6.数组:
点击查看代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
//var arr = new Array(1,2,3);
//var arr = [1,2,3];
//alert(arr);
//arr[0]=10;
//alert(arr);
//特点:相当于java的集合:变长变类型
//变长:
/*var arr = [1, 2, 3];
arr[10] = 10;
alert(arr[10]);
alert(arr[9]);*/
//变类型:
/*arr[5] = "hello";
alert(arr);*/
//属性:length:数组中元素个数
/*var arr = [1,2,3,4,];
for (let i = 0; i < arr.length; i++) {
alert(arr[i]);
}*/
//方法:
//push添加方法
var arr = [1,2,3];
/*arr.push(10);
alert(arr);*/
//splice删除方法
arr.splice(0,2)//从0开始,到2结束
alert(arr);
</script>
</body>
</html>