JS
语句
alert :弹出警告框
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script type="text/javascript">
alert('wanglan');
</script>
</body>
</html>
注释
// :当行注释 /* */ :多行注释
控制台输出
<script type="text/javascript">
console.log('wanglan'); //输出到浏览器F12中的console中
</script>
var :定义变量关键字 ,大小写敏感
<script type="text/javascript">
var a = 10;
var A = 10;
</script>
prompt :弹出让用户输入的对话框,输入的都是字符串类型
<script type="text/javascript">
var a = prompt('吃了吗');
console.log(a);
</script>
变量的类型
数值型:number
<script type="text/javascript">
var a = 10;
console.log(typeof a); // typeof:获取变量的类型,只要是数字,无论正负/整数/浮点数都是number
</script>
字符串:string
<script type="text/javascript">
var a = 'wanglan';
console.log(typeof a);
</script>
变量的赋值
<script type="text/javascript">
var a = "3";
var b = 2;
console.log(a-b)
</script>
# 注意 :字符串 - 数值 = 数值
数据类型转换
字符串转数字
1.parseint() :
<script type="text/javascript">
var a = parseInt("5");
console.log(typeof a)
</script>
parseint 自带净化功能,只保留字符串最开头的数字,后面的字符串自动消失
var a = parseInt("5wanglan");
console.log(a);
//当数字在中间时会找不到, NaN
var a = parseInt("wang5lan");
console.log(a);
自动截断小数的功能:取整,不四舍五入
var a = parseInt(5.8) + parseInt(4.7); console.log(a);
var a = parseInt(5.8 + 4.7); console.log(a);
2.Number()
var a = Number('5');
console.log(typeof a);
3.parseFloat():小数,只保留15位
var a = Number('5.1234534534645645645645645');
console.log(parseFloat(a));
数值转字符串
1.String()和toString()
var a = 5;
var astr = String(a);
console.log(typeof astr);
console.log(typeof astr.toString());
2.数值+' '
var a = 5 +'';
console.log(a)
任何数据类型都可以转成boolean
var b1 = '123'; //true
var b2 = 0; //false
var b3 = -123; //true
var b4 = Infinity; //无限大 true
var b5 = NaN; //false
var b6; //undefined false
var b7 = null; //fasle