Javascript สำหรับผู้เริ่มต้น ตอนที่ 2/3
หัวข้อที่คุณจะกล่าวถึงในส่วนนี้
- การดำเนินงาน
- เงื่อนไข
- ลูป
- การทำงาน
ใน JavaScript การดำเนินการคือการกระทำที่คุณดำเนินการกับค่าเพื่อสร้างค่าใหม่ได้ JavaScript มีการดำเนินการหลายประเภท ได้แก่ :
- การมอบหมาย : การดำเนินการมอบหมายกำหนดค่าให้กับตัวแปร ตัวดำเนินการกำหนดที่พบมากที่สุดคือ
=ซึ่งจะกำหนดค่าทางด้านขวาให้กับตัวแปรทางด้านซ้าย ตัวอย่างเช่น:
let x = 10; // assigns the value 10 to the variable x
let x = 10;
x += 5; // x is now 15
x *= 2; // x is now 30
let x = 10 + 5; // x is now 15
let y = 20 - 10; // y is now 10
let z = 5 * 2; // z is now 10
let a = 20 / 5; // a is now 4
let b = 7 % 3; // b is now 1
let x = 10;
let y = 5;
console.log(x == y); // false
console.log(x != y); // true
console.log(x > y); // true
console.log(x < y); // false
console.log(x >= y); // true
console.log(x <= y); // false
let x = true;
let y = false;
console.log(x && y); // false
console.log(x || y); // true
console.log(!x); // false
มีเงื่อนไขหลายประเภทใน JavaScript รวมถึง:
ifคำสั่ง:ifคำสั่งใช้เพื่อดำเนินการบล็อกของรหัสหากเงื่อนไขบางอย่างtrueคือ มีไวยากรณ์ดังต่อไปนี้:
if (condition) {
// code to be executed if condition is true
}
if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition1 is false and condition2 is true
} else {
// code to be executed if condition1 and condition2 are false
}
switch (expression) {
case value1:
// code to be executed if expression is equal to value1
break;
case value2:
// code to be executed if expression is equal to value2
break;
default:
// code to be executed if expression is not equal to any of the values
}
ใน JavaScript การวนซ้ำเป็นโครงสร้างการควบคุมที่อนุญาตให้คุณรันบล็อกโค้ดซ้ำๆ จนกว่าจะตรงตามเงื่อนไขที่กำหนด มีลูปหลายประเภทใน JavaScript รวมถึง:
forการวนซ้ำ: การforวนซ้ำใช้เพื่อรันบล็อกของโค้ดตามจำนวนครั้งที่กำหนด มีไวยากรณ์ดังต่อไปนี้:
for (initialization; condition; increment) {
// code to be executed
}
for (let i = 1; i <= 10; i++) {
console.log(i);
}
while (condition) {
// code to be executed
}
นี่คือตัวอย่างการwhileวนซ้ำที่นับ 1 ถึง 10:
let i = 1;
while (i <= 10) {
console.log(i);
i++;
}
do {
// code to be executed
} while (condition);
นี่คือตัวอย่างการdo-whileวนซ้ำที่นับ 1 ถึง 10:
let i = 1;
do {
console.log(i);
i++;
} while (i <= 10);
4. ฟังก์ชั่น
ใน JavaScript ฟังก์ชันคือบล็อกของโค้ดที่สามารถกำหนดและเรียกตามชื่อได้ ฟังก์ชันสามารถยอมรับอาร์กิวเมนต์ซึ่งเป็นค่าที่ส่งผ่านไปยังฟังก์ชันเมื่อมีการเรียกใช้ และสามารถส่งคืนค่าโดยใช้returnคำสั่ง
นี่คือตัวอย่างฟังก์ชันง่ายๆ ใน JavaScript:
function greet(name) {
return "Hello, " + name;
}
let greeting = greet("John"); // "Hello, John"
คุณยังสามารถกำหนดฟังก์ชันด้วยหลายอาร์กิวเมนต์:
function add(x, y) {
return x + y;
}
let sum = add(10, 20); // 30
ฟังก์ชันสามารถกำหนดโดยไม่ระบุตัวตนและจัดเก็บไว้ในตัวแปรได้:
let multiply = function(x, y) {
return x * y;
}
let product = multiply(10, 20); // 200
นอกจากนี้ยังสามารถกำหนดฟังก์ชันโดยใช้สัญกรณ์ลูกศร:
let divide = (x, y) => {
return x / y;
}
let quotient = divide(10, 5); // 2
Javascript สำหรับผู้เริ่มต้น ส่วนที่ 1: https:///@mdaliansari33/javascript-for-beginner-part-2–3-f86113fcc95e
Javascript สำหรับผู้เริ่มต้น ส่วนที่ 3: https://medium.com/@mdaliansari33/javascript-for-beginner-part-3-3-b655c1395cc





































![รายการที่เชื่อมโยงคืออะไร? [ส่วนที่ 1]](https://post.nghiatu.com/assets/images/m/max/724/1*Xokk6XOjWyIGCBujkJsCzQ.jpeg)