摘要
js代码位置、输出、语法 其他注意事项
位置
js脚本放在 <body> 标签中,脚本内容放在 <script> 。
使用外部js:<script src = "demo.js"> </script>
数据输出
警告框:window.alert()
Eg. window.alert("hello world")
页面直接输出:document.write()
Eg. document.write("<p>hello world</p>")
html元素输出:innerHTML
Eg. document.getElementById("demo").innerHTML = "hello world"
控制台输出:console.log()
Eg. console.log("hello world")
语法
字面量(literal): 固定值的表示方法。理解为描述自己的量,一眼就明白的量
数字字面量
Eg.
50 e 1.23
字符串字面量
Eg. "hello" "world" 'JavaScript'
表达式字面量
Eg. 2*3 4+5
数组(Array)字面量
Eg. [1,2,3,"a","b"]
对象(Object)字面量
Eg.
var man = {name:"Mike",sex:"male","age":18,"phone number":"18888888888"};
//属性名用双引号减少发生错误的概率。末尾加分号。
console.log(man["phone number"]);
document.write(man["name"]);
函数(Function)字面量
Eg. function demo(a,b){ return a + b; }
注意事项
- JavaScript 对大小写是敏感的。
- JavaScript 使用 Unicode 字符集