Console (到底什麼是console ?)
若是對字串使用+號則會合併字串輸出:
console類似於一個控制器,用來控制顯示重要訊息(例如錯誤)。
大部分的網頁操作都是不可視,如果想要在螢幕上看到東西,就要使用console的print或是log功能。
log的語法:
console.log(5); //print 5 |
另外也能夠在括號內進行運算:
console.log(3 + 4); // Prints 7 console.log(5 - 1); // Prints 4 console.log(4 * 2); // Prints 8 console.log(9 / 3); // Prints 3 |
console.log('One' + ', ' + 'two' + ', ' + 'three!'); // Prints 'One, two, three!' |
另外,在字串後面加句號能夠接上一些額外功能:
.length 字串長度
.toUpperCase() 轉換為全大寫
.toUpperCase() 轉換為全大寫
.trim() 去除頭尾所有空白
.startsWith('X') 檢查是否以"X"開始
console.log('Hello'.length); // Prints 5 console.log('Bahamut'.toUpperCase()); // Prints BAHAMUT console.log(' Remove whitespace '.trim()); // Prints Remove whitespace console.log('Hey'.startsWith('H')); // Prints true |
關於更多字串的函數功能可以看這裡
註解
JS的註解和C++一樣,可以在單行中使用//來註解,
也能使用/* */進行區塊註解。