六角學院|變數

常見資料類型

數字:35
字串:hello world
布林:true/false


變數 number 介紹、加減乘除

var price=30;

var宣告變數名稱
price名稱為價格
=指定一個數目

script應該緊接在內文body渲染之後,最後再以JS呈現


變數 string 介紹

var indexName(文字字串)='______'

因為JS並無法知道你今日code是你自己打的文字字串或是var後接的一個變數.
故今日你若想打出你寫出的文字內容或是將文字印在網頁上需用"____"或是'____'

var indexName='淑珍';
var polite="hello ";

alert(indexName);

document.getElementById('myName').textContent=polite + indexName;

也可以寫成

var indexName='淑珍';
var polite="hello ";
var totalPolite=polite+indexName;

alert(indexName);

document.getElementById('myName').textContent=totalPolite;

*alert用於瀏覽器彈跳出一個輸入的文字或內容之字串

如何輸出內容

若不想彈跳視窗,輸入console.log,可從a轉成b

(a)
var score=90;

alert(score);

score=80;
alert(score);

score='hello ';

alert(score);
(b)
var score=90;

console.log(score);

變數規則

-不能數字跟變數混用
-變數命名不接受 -&.
-不能用關鍵字.例如:for迴圈.int等等.https://www.w3schools.com/js/js_reserved.asp
-大小寫有區分
-變數需要有語意化

變數 數字新增刪除修改

*undefined:空值(尚未定義)

document.getElementById('input').textContent = "Hello"; 
  1. document //取得html文件
  2. getElementById //在當中透過Id來找到我想要的元素
  3. ('input') //我想要找到input的元素
  4. textContent // 取得這個元素的文字內容
  5. = "Hello" // 把文字內容設定成 "Hello"

评论

热门博文