qfedu-web/www/js2.html

50 lines
1.4 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="author" content="flykhan" />
<meta name="keywords" content="c c++ 物联网" />
<title>JavaScript脚本开发</title>
<style></style>
</head>
<body>
<script>
// 字符串类型
let name = "123"; // 单引号
let title = "good"; // 双引号
let age = 29; // 整数
let score = 125.5; // 小数类型float
let content =
name + "-" + title + ", age is " + age + ", score is " + score;
alert(content);
let x = "12",
y = "45";
let ret = x * y; // 自动将数字的字符串转成数值
console.log(ret); // 控制台打印结果 540
let a = "123";
let a1 = parseInt(a); // 将 a 转化为 int
let a2 = Number(a); // 将 a 转化为数值整数int和小数float
console.info(a1);
console.error(a2); // 会议错误标签打印
let b = "123abc";
let b1 = parseInt(b); // 遇到非数字则停止转换
console.warn(b1);
let b2 = Number(b); // 如果字符串存在非数字,则返回 NaN Not a Number
console.error(b2);
let c = "1.25";
let c1 = parseInt(c); // 遇到小数点结束转换
let c2 = parseFloat(c);
let c3 = Number(c); // 可以直接转换为对应数字,如果遇到字母,则返回 NaN
console.log(c, c1, c2, c3);
</script>
</body>
</html>