变量:(variable)
JavaScript可以直接对变量进行赋值而不要事先声明
JavaScript语言里,变量和其他元素的名字都是区分大小写的
JavaScript变量允许包含字母、数字、美元符号和下划线,但不允许数字开头
为什么不能以数字开头?
避免计算机运算的混乱,乘法省略乘号,因此强制不能使用数字
<!doctype html> <html> <head> <meta charset="utf-8"> <title> javascript页面</title> <script type="text/javascript"> var hello = "你好"; var hello = "大家好"; document.write(hello); </script> </head> <body> </body> </html> |
数据类型:
JavaScript类型声明和其他语言一样,但是JavaScript是弱类型语言,不需要声明类型
弱类型就是意味着,可以随意改变某个变量的数据类型
<script type="text/javascript"> var num = 100; document.write(num); document.write("<br/>"); document.write(num+1); document.write("<br/>"); document.write(num+"1"); </script> |
数据类型:字符串、数值、布尔值、数组
转义字符:字符串自动转义 \
<script type="text/javascript"> var name = "小明"; document.write(name); document.write("<br/>"); var name = "小\‘‘明"; document.write(name); document.write("<br/>"); var name = ‘"小明"‘; document.write(name);//"小明" </script> |
数组声明:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>第一个javascript页面</title> <script type="text/javascript"> var week = new Array("sunday","monday","tuesday","wednesday","thirsday","friday","staturday"); document.write(week[0]); document.write("<br/>"); document.write(week. length); </script> </head> <body> </body> </html> |
运算符:
算术运算符加减乘除取余 ++ --
比较运算符> <>= <= == !=\
逻辑运算符&& || ! &
条件语句:
If语句 while() do… While () for () switch () case
函数:
需要多次使用同一组语句,可以打包成一个函数
<!doctype html> <html> <head> <meta charset="utf-8"> <title>第一个javascript页面</title> <script type="text/javascript"> var n =Math. floor( Math. random()*10000); document.write(n); document.write("随机数字为<br/>"); while(n != 1){ if (n%2 == 1) { n = n * 3 + 1; }else{ n/=2; } document.write(n); document.write("<br/>"); } </script> </head> <body> </body> </html> |
本文出自 “杜国栋个人PHP学习博文” 博客,请务必保留此出处http://duguodong.blog.51cto.com/7667978/1398214