คำสั่ง JavaScript

ใน HTML คำสั่ง JavaScript หลอดโดยออร์เดอร์ที่เครื่องนอนเว็บบราวเซอร์ "บุกเบิก"。

ตัวอย่าง

var x, y, z;	// คำสั่ง 1
x = 22;		// คำสั่ง 2
y = 11;		// คำสั่ง 3
z = x + y;	// คำสั่ง 4

ลองด้วยตัวเอง

ระบบ JavaScript

โปรแกรมคอมพิวเตอร์เป็นลำดับของออร์เดอร์ที่คอมพิวเตอร์ "บุกเบิก"。

ในภาษาการเขียนโปรแกรมเหล่านี้ออร์เดอร์เรียกว่าคำสั่ง。

ระบบ JavaScriptคือลำดับของการเขียนโปรแกรมคำสั่ง

Note:ใน HTML ระบบ JavaScript หลอดโดยเครื่องนอนเว็บบราวเซอร์。

คำสั่ง JavaScript

คำสั่ง JavaScript ประกอบด้วยสิ่งต่อไปนี้:

ค่า、ปฏิบัติการ、สัญญาณทาง matematical และสัญาณทางอื่นๆ และหมายคำทางภาษา

คำสั่งนี้บอกให้เบราเซอร์หลุดข้อความ "Hello Kitty." ในองค์ประกอบ HTML ที่มี id="demo"

ตัวอย่าง

document.getElementById("demo").innerHTML = "Hello Kitty.";

ลองด้วยตัวเอง

โค้ด JavaScript ส่วนใหญ่จะมีคำสั่ง JavaScript หลายหลายราย

คำสั่งเหล่านี้จะทำงานตามลำดับที่ถูกเขียน

Note:คำสั่ง JavaScript (และคำสั่ง JavaScript) มักเรียกว่า โค้ด JavaScript

สัญาณจุดสุดท้าย ;

สัญาณจุดสุดท้ายของ JavaScript สำหรับการแยกคำสั่ง

โปรดใส่สัญาณจุดสุดท้ายหลังจากทุกคำสั่งที่สามารถปฏิบัติได้

a = 5;
b = 6;
c = a + b;

ลองด้วยตัวเอง

ถ้ามีสัญาณจุดสุดท้ายแยกคำสั่ง จะอนุญาตให้เขียนคำสั่งหลายคำในแถวเดียว

a = 5; b = 6; c = a + b;

ลองด้วยตัวเอง

คุณอาจจะเห็นตัวอย่างที่ไม่มีสัญาณจุดสุดท้ายบนอินเทอร์เน็ต

คำเตือน:การสิ้นสุดคำสั่งด้วยสัญาณจุดสุดท้ายไม่จำเป็น แต่เรายังแนะนำอย่างแข็งขันที่จะทำนั้น

JavaScript หลักฐานความว่าง

JavaScript จะละเลยช่องว่างหลายหลายครั้ง

บรรทัดที่สองแถวนี้เป็นเดียวกัน

var person = "Bill";
var person="Bill"; 

ใช้ช่องว่างด้านข้างปฏิบัติการทาง matematical ( = + - * / ) ว่าเป็นขวัญของที่ดี

var x = y + z;

ความยาวของบรรทัดและการหยุดวางบรรทัด JavaScript

เพื่อให้ความสะอาดในการอ่าน แพร์โฟรมเมอร์ทั่วไปมักชอบจะควบคุมความยาวของบรรทัดโค้ดในระดับ 80 อักษร

ถ้าคำสั่ง JavaScript ยาวเกินไป การหยุดวางบรรทัดที่มีสัญาณการปฏิบัติทาง matematical หรือปฏิบัติทางอื่นๆ จะเป็นทางดีที่สุด

ตัวอย่าง

document.getElementById("demo").innerHTML =
 "Hello Kitty.";

ลองด้วยตัวเอง

บล็อคโค้ด JavaScript

คำสั่ง JavaScript สามารถใช้หลวงตัวกลม ({...}ตัวอย่างที่จะนำมาใช้

บทความหลายรายในชุดเพื่อระบุคำสั่งที่จะทำงานด้วยกัน

คุณจะเห็นถึงการประกอบคำสั่งในรูปแบบชุดความหมายใน JavaScript

ตัวอย่าง

function myFunction() {
    document.getElementById("demo").innerHTML = "Hello Kitty.";
    document.getElementById("myDIV").innerHTML = "How are you?";
}

ลองด้วยตัวเอง

Note:ในบทความนี้เราใช้เปลือกสีเทา 4 หลายของช่องว่างเพื่อย่อรอยโค้ด

คำเตือน:You will learn more about functions later in this tutorial.

JavaScript Keywords

JavaScript statements are often identified by a keyword to indicate the JavaScript action to be executed.

The following table lists some of the keywords that will be learned in this tutorial:

Keyword Description
break Terminate the switch or loop.
continue Exit the loop and start at the top.
debugger Stop executing JavaScript and call the debugging function (if available).
do ... while Execute the statement block and repeat the code block when the condition is true.
for Mark the statement block to be executed as long as the condition is true.
function Declare a function.
if ... else Mark the statement block to be executed, depending on a certain condition.
return Exit the function.
switch Mark the statement block to be executed, depending on different situations.
try ... catch Implement error handling for statement blocks.
var Declare variables.

Note:JavaScript ؼʶָΪɱıʶ. ɱıʶʹΪ.

Supplementary Reading

Advanced JavaScript Tutorial: ECMAScript ҡ