国际通用命名规则<html lang="en"> <head> <meta charset="UTF-8"> <title>命名规则</title> </head> <body> <!--国际通用的命名规则:驼峰法则 <一:变量> 1:单个单词的全小写 var name = "lzw"; 2:多个单词 第一个单词全小写,其余单词首字母大写 var myName = "lzw"; var showHelloWord = "HelloWord"; <二:类> 3:全部单词的首字母都要大写 PersonName <三:常量> 4:所有单词的字母全大写 COUNTRYNAME --> <!--类和常量与Java相关--> </body> <script> var name = "lzw"; var myName = "lzw"; </script> </html>
Number函数<html lang="en"> <head> <meta charset="UTF-8"> <title>Number函数</title> </head> <body> <script> var b = "1"; var i = 1; alert(b === i); /*alert(Number(true));*/ </script> </body> </html>
结果图:
Typeof类型<html lang="en"> <head> <meta charset="UTF-8"> <title>Typeof</title> </head> <body> <script> /*undefined*/ var i; // alert(typeof i); /*boolean*/ var b = true; // alert(typeof b); /*string*/ var s = "lzw"; // alert(typeof s); /*number*/ var i = 3; // alert(typeof i); var obj = { name:"lzw", age:"保密" } // alert(typeof obj); function show() { alert("hello"); } alert(typeof show); </script> </body> </html>
结果图:
Prompt<html lang="en"> <head> <meta charset="UTF-8"> <title>Prompt</title> </head> <body> <script> var name = prompt("请输入用户姓名","张三"); alert(name); </script> </body> </html>
结果图:
自增自减<html lang="en"> <head> <meta charset="UTF-8"> <title>自增自减</title> </head> <body> <!-- ++自增运算 1:++在运算数的前面:先+1 再运算 2:++在运算数的后面:先运算 再+1 --> <script> /* var i = 2; j = i++; alert(j); alert(i);*/ var i = 2; j = ++i; alert(j); alert(i); </script> </body> </html>
结果图:
水仙花数<html lang="en"> <head> <meta charset="UTF-8"> <title>水仙花数</title> </head> <body> <script> var num = 153; var n1 = parseInt(num/100); var n2 = parseInt(num/10%10); var n3 = parseInt(num%10); var result = n1*n1*n1+n2*n2*n2+n3*n3*n3; Math.pow(n1,3)+Math.pow(n2,3)+Math.pow(n3,3); if (num == result){ alert("是水仙花数!!")} else{ alert("非水仙花数!!") } </script> </body> </html>
结果图:
赋值运算符<html lang="en"> <head> <meta charset="UTF-8"> <title>赋值运算符</title> </head> <body> <script> var i = 5; i += 5; // 等效 i +=5; 效率高 alert(i); var i2 = 10; i2 -= 7;// i2 = i2 - 7; var i3 = 6; i3 = i3 * 6; // i3 *= 6; var i4 = 12; i4 = i4 / 3; // i4 /= 3; var i5 = 16; i5 = i5 % 6;// i5 %= 6; </script> </body> </html>
结果图:
if-else语句<html lang="en"> <head> <meta charset="UTF-8"> <title>if-else</title> </head> <body> <script> var i = parseInt(prompt("输入分数")); if (i >= 90) { alert("优"); } else if ( i >= 80) { alert("良"); } else if ( i >= 60) { alert("中"); } else { alert("差"); } </script> </body> </html>
结果图:
嵌套if结构
<html lang="en"> <head> <meta charset="UTF-8"> <title>嵌套if结构</title> </head> <body> <script> var score = parseInt(prompt("考试成绩")); var daily = parseInt(prompt("考勤成绩")); if (score >= 60 && daily >= 75) { alert("毕业"); } else { if (score < 60 && daily < 75) { alert("都不合格"); } else if (score < 60) { alert("考试不合格"); } else if (daily < 75) { alert("考勤不合格"); } } </script> </body> </html>
结果图:
switch-case语句
<html lang="en"> <head> <meta charset="UTF-8"> <title>switch-case</title> </head> <body> <script> var month = parseInt(prompt("请输入月份")); switch (month){ case 1: alert("第一季度"); break; case 2: alert("第一季度"); break; case 3: alert("第一季度"); break; case 4: alert("第二季度"); break; case 5: alert("第二季度"); break; case 6: alert("第二季度"); break; case 7: alert("第三季度"); break; case 8: alert("第三季度"); break; case 9: alert("第三季度"); break; case 10: alert("第四季度"); break; case 11: alert("第四季度"); break; case 12: alert("第四季度"); break; default: alert("输入的月份有误"); break; } </script> </body> </html>
结果图:
时间: 2024-10-26 00:29:34