1. 字元串: 2. 數值 js中,數值類型比較簡單,不區分整型浮點型等數字類型。就是這個數,帶不帶小數點,對js來說都是一樣進行識別。 <!DOCTYPE html><html><body> <script>var a = 1;var b = 2.3;var c = a + b;var name1 ...
1. 字元串:
<!DOCTYPE html> <html> <body> <script> var apple = "蘋果"; var banana = "香蕉"; var orange = "橘子"; var name1 = "小明"; var name2 = "小紅"; document.write(name1 + "說:老師我想吃" + apple + "<br>"); document.write(name2 + "說:老師我想吃" + banana + "和" + orange); </script> </body> </html>
2. 數值
js中,數值類型比較簡單,不區分整型浮點型等數字類型。就是這個數,帶不帶小數點,對js來說都是一樣進行識別。
<!DOCTYPE html>
<html>
<body>
<script>
var a = 1;
var b = 2.3;
var c = a + b;
var name1 = "小明";
var name2 = "小紅";
document.write(name1 + "問" + name2 + ":" + a + "+" + b + "等於幾呀?" + "<br>");
document.write(name2 + "回答說:" + a + "+" + b + "等於" + c + "呀!" + "<br>");
</script>
</body>
</html>
難道js中,字元串和數值可以直接相加進行拼接嗎?
實驗:
<!DOCTYPE html>
<html>
<body>
<script>
var a = 1;
var b = 2.3;
var c = a + b;
var name1 = "小明";
var name2 = "小紅";
document.write(name1 + "問" + name2 + ":" + a + "+" + b + "等於幾呀?" + "<br>");
document.write(name2 + "回答說:" + a + "+" + b + "等於" + c + "呀!" + "<br>");
document.write(a + name1 + b + name2);
</script>
</body>
</html>
結果:
小明問小紅:1+2.3等於幾呀?
小紅回答說:1+2.3等於3.3呀!
1小明2.3小紅
可以的。
3. 布爾類型
真和假,分別用true和false表示。
4. 數組
<!DOCTYPE html> <html> <body> <script> // 方法1 var i; var cars1 = new Array(); cars1[0] = "Audi"; cars1[1] = "BMW"; cars1[2] = "Volvo"; for (i=0;i<cars1.length;i++) { document.write(cars1[i] + "<br>"); } document.write("<br>"); // 方法2 var cars2 = new Array("Audi", "BMW", "Volvo"); for (i=0;i<cars2.length;i++) { document.write(cars2[i] + "<br>"); } </script> </body> </html>
結果:
Audi
BMW
Volvo
Audi
BMW
Volvo
5. JavaScript對象
<!DOCTYPE html>
<html>
<body>
<script charset = "utf-8">
var info = {
name: "小明",
age: 63,
addr: "象牙山村156號",
}
// js對象有兩種定址方式
// 定址1
document.write(info.name + "<br>");
// 定址2
document.write(info["addr"] + "<br>");
</script>
</body>
</html>
一開始運行出來,中文是亂碼,在<script charset = "utf-8">中,加入charset = "utf-8"後,解決了。