1.Javascript有三种写入方式:
- 事件定义式
在事件定义时,直接写js


<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Test</title> </head> <body> <input type="button" value="btn" onclick="alert('hello world');"> </body> </html>
- 嵌入式
使用<script>标签


<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Test</title> </head> <script type="text/javascript"> function method() { alert("hello js"); } </script> <body> <input type="button" value="btn" onclick="method();"> </body> </html>
- 文件调用式
代码位于单独的.js文件中
html页面引用.js文件
需要注意的是,.js文件中不需要添加<script>标签,脚本文件中不需要脚本开始和结束声明
2. 变量声明
使用关键字var声明变量,没有初始化的变量则自动取值为undefined
变量没有类型,统一用关键字var声明。变量所引用的数据是有类型的。
3.数据类型
数字+字符串:数字转换成字符串
数字+布尔值:true转换成1,false转换为0
字符串+布尔值:布尔值转换为字符串true或false
布尔值+布尔值:布尔值转换为数值1或0
4.算数运算
java中的5/2结果为2
js中的5/2结果为2.5
5.javascript对象描述
— 内置对象(String Number Boolean Array Math Date RegExp Function)
— 外部对象
window对象(screen history location navigator)
dom对象
— 自定义对象