以下整理了 JavaScript 中最常用的語法與內建功能,涵蓋輸出、型別轉換、流程控制、函式、視窗操作、字串與數學處理、日期物件等,適合初學者隨手速查。
document.write("")。//。document.getElementById("元素ID").name(或 value)。parseInt()、parseFloat()。("" + 變數)。length。var。==、!=、>、>=、<、<=。if(condition){}else{}。for([初始];[條件];[更新]){...}。break。function functionName([parameter],...){statement[s]}。window.open()、window.close();視窗本身:self。window.status="字串";。window.alert("字串");。window.confirm();。window.prompt();。window.location.href="URL"。window.print()。string.toUpperCase()、string.toLowerCase()。String1.indexOf("String2")(回傳 -1 代表找不到)。StringA.charAt(9)。stringA.substring(2,6)。Math.PI、Math.max()、Math.pow()、Math.round()、Math.floor(Math.random()*(n+1))(隨機數)。string.concat(string2) 或以 += 連接。string.lastIndexOf(searchString[,startIndex])。string.match(regExpression)。string.replace(regExpression, replaceString)。string.split(分隔符)。string.substr(start[,length])。parseInt(string[,radix])、parseFloat(string[,radix])。isNaN(變數)。var today = new Date();。getTime()、getFullYear()、getMonth()、getDate()、getDay()、getHours()、getMinutes()、getSeconds(),以及對應的 setXxx() 設定方法(注意月份從 0 起算)。const;定義變數:var。isDisabled;設定禁止:disabled。length,回傳整數。window.focus();反之失去焦點:blur()。window.location.reload()。window.history.back()、forward()、go(n)。document.write() 不換行、document.writeln() 換行。這份清單涵蓋了 JavaScript 日常開發最常碰到的語法,可作為速查手冊。需提醒的是,部分寫法(如 document.all、showModalDialog)屬於舊式 API,在現代開發中應改用標準 DOM 方法與 window.showModalDialog 的替代方案(如 <dialog> 元素),以確保跨瀏覽器相容性。