คำสั่งใน JavaScript

Jan 24 2023
คำสั่ง JavaScript เป็นบล็อกของรหัสที่ดำเนินการหรืองานบางอย่างให้เสร็จสมบูรณ์ ตามค่าเริ่มต้น ตัวแปล JavaScript จะดำเนินการคำสั่งเหล่านี้ตามลำดับที่เขียน

คำสั่ง JavaScript เป็นบล็อกของรหัสที่ดำเนินการหรืองานบางอย่างให้เสร็จสมบูรณ์ ตามค่าเริ่มต้น ตัวแปล JavaScript จะดำเนินการคำสั่งเหล่านี้ตามลำดับที่เขียน

เรามาทำความเข้าใจเกี่ยวกับข้อความประเภทต่างๆ ใน ​​JS ด้านล่างกัน

1. ข้อความนิพจน์

นิพจน์ที่มีผลข้างเคียงถือเป็นคำสั่งนิพจน์เช่น คำสั่งมอบหมาย, เรียกใช้ฟังก์ชัน, ตัวดำเนินการเพิ่มและลด, ++ และ — ที่เกี่ยวข้องกับคำสั่งมอบหมาย และตัวดำเนินการลบ

let greeting = “Hello “ + name; //assignment statement
counter ++; // increment operator which is changing a variable value
delete o.x; // deleting a property of an object O
cx = Math.cos(x);; // function invocation and assign the value to a variable

คำสั่งเดียวสามารถมีมากกว่าหนึ่งคำสั่งย่อยจะถือเป็นคำสั่งผสม หรือบล็อกคำสั่งใน JS บรรทัดต่อไปนี้ทำหน้าที่เป็นคำสั่งเดียว

{ 
 x = Math.PI; 
 cx = Math.cos(x);
 console.log(“cos(π) = “ + cx);
}

3. คำชี้แจงเงื่อนไข

คำสั่งที่ทำให้ล่าม JS ดำเนินการหรือข้ามคำสั่งอื่นขึ้นอยู่กับค่าของนิพจน์เช่นคำสั่งifและswitch

3.1 if Statement
เป็นคำสั่งควบคุมพื้นฐานที่อนุญาตให้ JS ตัดสินใจและดำเนินการตามคำสั่งอย่างมีเงื่อนไข

//syntax
If (expression) 
 Statement1
else 
 Statement 2

//example
if (n === 1) { //curly braces are optional in case of single statement
 console.log("You have 1 new message.");
} else {
 console.log(`You have ${n} new messages.`);
}

3.2 คำสั่ง Switch
เมื่อค่าของนิพจน์เดียวกันสามารถใช้ในการตัดสินใจระหว่างตัวเลือกมากกว่า 2 ตัวเลือกสำหรับกรณีดังกล่าว JS จัดเตรียมคำสั่ง switch

//Syntax
switch(expression) {
 case label1: statement1; // if expression value === label1 execute statement1
 case label2: statement2; // if expression value === label2 execute statement2
 … 
 default: default_statement; // optional
}

//Example:
switch(n) {
  case 1:                        
   // Start here if n === 1.(sameness is determined by the === operator)
   // Execute code block #1.
   break;                         // stop here
  case 2:                         
    // Start here if n === 2
    // Execute code block #2.
   break;                         // Stop here
  case 3:                         
    // Start here if n === 3
    // Execute code block #3;
   break;                         // Stop here
  default:                        
    // If no case match the value of n
    // Execute code block #4.
}

4. คำสั่งลูป

คำสั่งที่ดำเนินการคำสั่งอื่น ๆ ซ้ำ ๆ เช่นwhileและfor

4.1 ลูป while
ล่ามจะประเมินนิพจน์ก่อน หากค่าของนิพจน์เป็นเท็จ ตัวแปลจะข้ามคำสั่งที่ทำหน้าที่เป็นตัวลูปและไปยังคำสั่งถัดไปในโปรแกรม ในทางกลับกัน หากการแสดงออกเป็นความจริง ล่ามจะดำเนินการตามคำสั่งและพูดซ้ำ

//Syntax
while (expression) { //curly braces are optional in case of single statement
  //statment or statement block
}

//Example
let count = 0;
while(count < 10) {
 console.log(count);
 count++;
}

//Syntax
do { //curly braces are optional in case of a single statement
 //statement / statement block
} 
while (expression);

//Example
let count = 0;
do {
 console.log(count);
 count++;
}
while (count < 10);

//syntax
for(initialize ; test ; increment) { //curly braces are optional in case of single statement
 //statement / statement block
}
//Example:
for(let count = 0; count < 10; count++) {
 console.log(count);
}

//Syntax with example
let data = [1, 2, 3, 4, 5, 6, 7, 8, 9], sum = 0;
for(let element of data) {
 sum += element;
}

// for of with map
let m = new Map([[1, "one"]]);
for(let [key, value] of m) {
 key    // => 1
 value  // => "one"
}

//syntax
for (variable in object) {
 //statement / statement block
}

//Example:
for(let p in o) { // Assign property names of o to variable p
 console.log(o[p]); // Print the value of each property
}

คำสั่งที่ทำให้ล่ามข้ามไปยังส่วนอื่นของโปรแกรม เช่นbreak , returnและThrow

5.1 ติดป้าย
ข้อความใด ๆ อาจมีป้ายกำกับโดยนำหน้าด้วยตัวระบุและเครื่องหมายทวิภาค: โดยการติดป้ายข้อความ แสดงว่าคุณให้ชื่อที่คุณสามารถใช้อ้างถึงที่อื่นในโปรแกรมของคุณ

//syntax
identifier: statement

//example
mainloop: while(token !== null) {
 // Code omitted…
 continue mainloop; // Jump to the next iteration of the named loop(mainloop)
 // More code omitted…
}

5.2 ดำเนินการต่อ
เมื่อดำเนินการคำสั่งดำเนินการต่อ การวนซ้ำปัจจุบันของลูปล้อมรอบจะสิ้นสุดลง และการวนซ้ำครั้งต่อไปจะเริ่มต้นขึ้น

//example
for(let i = 0; i < data.length; i++) {
 if (!data[i]) continue; // jump to the next iteration of the loop
 total += data[i];
}

//syntax
return expression;

//Example
function square(x) { return x*x; } // A function that has a return statement
square(2) // => 4

// A generator function that yields a range of integers
function* range(from, to) {
  for(let i = from; i <= to; i++) {
    yield i;
  }
}

คำสั่ง try/catch/final
คำสั่ง try/catch/final เป็นกลไกการจัดการข้อยกเว้นของ JavaScript

//syntax
try {
 //Normally, this code runs from the top of the block to the bottom
 //without problems. But it can sometimes throw an exception,
 //either directly or with a throw statement.
} catch(e) {
 // The statements in this block are executed if, and only if, the try
 // block throws an exception. These statements can use the local variable
 // e to refer to the Error object or other value that was thrown.
} finally {
 // This block contains statements that are always executed, regardless of
 // what happens in the try block. They are executed whether the try
 // block terminates:
}

//example
try {
  // Ask the user to enter a number
  let n = Number(prompt("Please enter a positive integer", ""));
  // Compute the factorial of the number, assuming the input is valid
  let f = factorial(n);
  // Display the result
  alert(n + "! = " + f);
} catch(e) {     // If the user's input was not valid, we end up here
  alert(e);  // Tell the user what the error is
}

โพสต์ต่อไปนี้จะเป็นส่วนหนึ่งของซีรีส์ที่พูดถึงหัวข้อพื้นฐานและขั้นสูงเกี่ยวกับ JavaScript

หากคุณชอบเรื่องราวนี้ โปรดคลิกปุ่ม และแบ่งปัน เพื่อให้คนอื่นสามารถค้นพบมันได้เช่นกัน! นอกจากนี้อย่าลังเลที่จะแสดงความคิดเห็นด้านล่าง

Groww Engineeringเผยแพร่เกร็ดเล็กเกร็ดน้อยทางเทคนิค เทคโนโลยีล่าสุด และวิธีที่ดีกว่าในการจัดการกับปัญหาการเขียนโปรแกรมทั่วไป คุณสามารถสมัครรับข่าวสารได้ที่นี่เพื่อรับข่าวสารล่าสุด

เรากำลังจ้างงาน ดูตำแหน่งงานว่างที่นี่