การอ้างอิงคำสั่ง JavaScript
คำสั่งจาวาสคริปต์
ใน HTML คำสั่ง JavaScript คือ "คำสั่ง" ที่จะ "ดำเนินการ" โดยเว็บเบราว์เซอร์
คำสั่งนี้บอกให้เบราว์เซอร์เขียน "สวัสดี Dolly" ภายในองค์ประกอบ HTML ที่มี id="demo":
ตัวอย่าง
document.getElementById("demo").innerHTML = "Hello Dolly.";
สำหรับบทช่วยสอนเกี่ยวกับคำชี้แจง โปรดอ่าน บทช่วย สอนคำสั่ง JavaScriptของ เรา
ตัวระบุคำสั่ง JavaScript
คำสั่ง JavaScript มักจะเริ่มต้นด้วยตัวระบุคำสั่ง เพื่อระบุการทำงานของ JavaScript ที่จะดำเนินการ
ตัวระบุคำชี้แจงเป็นคำที่สงวนไว้และไม่สามารถใช้เป็นชื่อตัวแปรได้ (หรือสิ่งอื่นใด)
ตารางต่อไปนี้แสดงรายการตัวระบุคำสั่ง JavaScript ทั้งหมด:
Statement | Description |
---|---|
break | Exits a switch or a loop |
class | Declares a class |
const | Declares a variable with a constant value |
continue | Breaks one iteration (in the loop) if a specified condition occurs, and continues with the next iteration in the loop |
debugger | Stops the execution of JavaScript, and calls (if available) the debugging function |
do ... while | Executes a block of statements and repeats the block while a condition is true |
for | Loops through a block of code a number of times |
for ... in | Loops through the properties of an object |
for ... of | Loops through the values of an iterable object |
function | Declares a function |
if ... else ... else if | Marks a block of statements to be executed depending on a condition |
let | Declares a variable |
return | Stops the execution of a function and returns a value from that function |
switch | Marks a block of statements to be executed depending on different cases |
throw | Throws (generates) an error |
try ... catch ... finally | Marks the block of statements to be executed when an error occurs in a try block, and implements error handling |
var | Declares a variable |
while | Marks a block of statements to be executed while a condition is true |