Programming basics for beginnersพื้นฐานการเขียนโปรแกรมสำหรับมือใหม่
Logic & Loops
How programs decide and repeatโปรแกรมตัดสินใจและทำงานซ้ำได้อย่างไร
Learn how a program makes decisions and repeats work: if/else, switch, for and while loops, and the everyday methods you'll use with them. Every example on this page runs. Change the code, press Run, and see what happens.
เรียนรู้ว่าโปรแกรมตัดสินใจและทำงานซ้ำได้อย่างไร ตั้งแต่ if/else, switch, ลูป for และ while ไปจนถึงเมธอดที่ใช้บ่อยในงานประจำวัน ทุกตัวอย่างในหน้านี้รันได้จริง ลองแก้โค้ด กด Run แล้วดูว่าเกิดอะไรขึ้น
Shortcut: pressคีย์ลัด: กด Ctrl+Enter (⌘+Enter Mac) inside a code box to run it.ในกล่องโค้ดเพื่อรันทันที
Sequenceลำดับ (Sequence)
Lines run from top to bottom, one after another.โค้ดทำงานจากบนลงล่าง ทีละบรรทัดตามลำดับ
Lesson 1 →บทที่ 1 →Selectionการเลือก (Selection)
The program chooses a path based on a condition.โปรแกรมเลือกเส้นทางตามเงื่อนไข
Lessons 2–3 →บทที่ 2–3 →Repetitionการวนซ้ำ (Repetition)
The program repeats a block of code many times.โปรแกรมทำโค้ดชุดเดิมซ้ำหลายรอบ
Lessons 4–6 →บทที่ 4–6 →Lesson 1 · The basicsบทที่ 1 · พื้นฐาน
Values & comparisonsค่าและการเปรียบเทียบ
A variable is a named box that holds a value. Use let when the value will change later, and const when it won't.
ตัวแปร (variable) คือกล่องที่มีชื่อสำหรับเก็บค่า ใช้ let เมื่อค่าจะเปลี่ยนในภายหลัง และใช้ const เมื่อค่าจะไม่เปลี่ยน
Every decision in code starts with a question whose answer is true or false. These yes/no values are called booleans. You make them with comparison and logical operators.
การตัดสินใจทุกครั้งในโค้ดเริ่มจากคำถามที่ตอบได้ว่า true (จริง) หรือ false (เท็จ) ค่าแบบนี้เรียกว่า บูลีน (boolean) ซึ่งสร้างได้ด้วยตัวดำเนินการเปรียบเทียบและตัวดำเนินการทางตรรกะ
| Operatorตัวดำเนินการ | Meaningความหมาย | Exampleตัวอย่าง | Resultผลลัพธ์ |
|---|---|---|---|
=== | equal toเท่ากับ | 5 === 5 | true |
!== | not equal toไม่เท่ากับ | 5 !== 3 | true |
> | greater thanมากกว่า | 7 > 10 | false |
< | less thanน้อยกว่า | 7 < 10 | true |
>= | greater than or equal toมากกว่าหรือเท่ากับ | 10 >= 10 | true |
<= | less than or equal toน้อยกว่าหรือเท่ากับ | 3 <= 2 | false |
&& | and: both sides must be trueและ: ต้องจริงทั้งสองฝั่ง | true && false | false |
|| | or: at least one side is trueหรือ: จริงอย่างน้อยหนึ่งฝั่ง | true || false | true |
! | not: flips the valueไม่ (นิเสธ): กลับค่าจริงเป็นเท็จ | !true | false |
Use === (three equals signs) to compare. A single = puts a value into a variable; it doesn't compare anything. JavaScript also has ==, but it converts types before comparing ("5" == 5 is true), which surprises people. Stick to ===.
ใช้ === (เครื่องหมายเท่ากับสามตัว) ในการเปรียบเทียบ ส่วน = ตัวเดียวคือการใส่ค่าลงในตัวแปร ไม่ได้เปรียบเทียบอะไรเลย JavaScript ยังมี == ด้วย แต่จะแปลงชนิดข้อมูลก่อนเทียบ ("5" == 5 ได้ true) ซึ่งมักทำให้สับสน จึงควรใช้ === เสมอ
Lesson 2 · Selectionบทที่ 2 · การเลือก
if / elseเงื่อนไข if / else
if runs a block of code only when its condition is true. Add else for what should happen otherwise. To check several conditions, chain them with else if. JavaScript checks them from top to bottom, runs the first one that is true, and skips the rest.
if จะรันโค้ดในบล็อกก็ต่อเมื่อเงื่อนไขเป็น true เพิ่ม else สำหรับสิ่งที่ต้องทำเมื่อเงื่อนไขไม่เป็นจริง ถ้าต้องตรวจหลายเงื่อนไข ให้ต่อกันด้วย else if โดย JavaScript จะตรวจจากบนลงล่าง ทำงานในเงื่อนไขแรกที่เป็นจริง แล้วข้ามส่วนที่เหลือทั้งหมด
true answer wins. The highlighted path is what happens when score is 72.
ลำดับการตรวจของตัวอย่างเกรดด้านล่าง รูปข้าวหลามตัดแต่ละอันคือคำถาม และคำตอบแรกที่เป็น true จะถูกเลือก เส้นที่ไฮไลต์คือเส้นทางเมื่อ score เท่ากับ 72
To pick between two values, the ternary operator is a one-line if/else: condition ? valueIfTrue : valueIfFalse.
ถ้าต้องการเลือกค่าระหว่างสองทาง ใช้ ternary operator ซึ่งเป็น if/else แบบบรรทัดเดียว: เงื่อนไข ? ค่าเมื่อจริง : ค่าเมื่อเท็จ
Lesson 3 · Selectionบทที่ 3 · การเลือก
switchคำสั่ง switch
switch compares one value against a list of cases. It reads more cleanly than a long else-if chain when you check the same variable for exact matches. End each case with break, or the code falls through into the next case. default runs when no case matches.
switch นำค่าหนึ่งค่าไปเทียบกับรายการ case ต่างๆ อ่านง่ายกว่าการเขียน else if ยาวๆ เมื่อเราตรวจตัวแปรเดียวกันว่าตรงกับค่าใดพอดี ปิดท้ายแต่ละ case ด้วย break ไม่เช่นนั้นโค้ดจะไหลต่อ (fall through) ไปยัง case ถัดไป และ default จะทำงานเมื่อไม่มี case ใดตรงเลย
Delete the break under the weekday message, set day to "fri", and run again. Without break, the code falls through into the next case and prints both messages.
ลบ break ใต้ข้อความวันธรรมดาออก ตั้งค่า day เป็น "fri" แล้วรันอีกครั้ง เมื่อไม่มี break โค้ดจะไหลต่อไปยัง case ถัดไป และแสดงข้อความทั้งสองอัน
if / else or switch?ใช้ if / else หรือ switch ดี?
- Use if / else for ranges or conditions that mix variables:
score >= 80,age < 13 && isMember.ใช้ if / else เมื่อเงื่อนไขเป็นช่วงค่า หรือใช้หลายตัวแปรร่วมกัน เช่นscore >= 80,age < 13 && isMember - Use switch to compare one value against several exact options: menu choices, status codes, days of the week.ใช้ switch เมื่อเทียบค่าเดียวกับตัวเลือกที่ต้องตรงกันพอดีหลายค่า เช่น เมนู รหัสสถานะ หรือชื่อวัน
switchcompares with===, so the number1does not match the text"1".switchเปรียบเทียบด้วย===ดังนั้นตัวเลข1จะไม่ตรงกับข้อความ"1"
Lesson 4 · Repetitionบทที่ 4 · การวนซ้ำ
The for loopลูป for
A for loop repeats code a set number of times. Everything that controls the loop sits in its header, in three parts separated by semicolons.
ลูป for ใช้ทำโค้ดซ้ำตามจำนวนรอบที่กำหนด ทุกอย่างที่ควบคุมลูปอยู่ในส่วนหัว ซึ่งแบ่งเป็น 3 ส่วน คั่นด้วยเครื่องหมายอัฒภาค (;)
for (let i = 0; i < 5; i++) {
- 1Start.
let i = 0runs once, before the first round. It creates the counter.จุดเริ่มต้นlet i = 0ทำงานครั้งเดียวก่อนรอบแรก เพื่อสร้างตัวนับ - 2Condition.
i < 5is checked before every round. When it becomesfalse, the loop ends.เงื่อนไขi < 5ถูกตรวจก่อนทุกรอบ เมื่อกลายเป็นfalseลูปจะจบ - 3Update.
i++runs after every round. It adds 1 toi.การอัปเดตi++ทำงานหลังจบแต่ละรอบ โดยเพิ่มค่าiทีละ 1
Trace it by handไล่โค้ดทีละรอบ
Tracing means writing down what each variable holds in each round. Here is for (let i = 0; i < 3; i++) console.log(i);การไล่โค้ด (trace) คือการจดว่าตัวแปรมีค่าเท่าไรในแต่ละรอบ นี่คือการไล่โค้ด for (let i = 0; i < 3; i++) console.log(i);
| Roundรอบ | i | i < 3 | Outputผลลัพธ์ |
|---|---|---|---|
| 1 | 0 | true | 0 |
| 2 | 1 | true | 1 |
| 3 | 2 | true | 2 |
| – | 3 | false | loop endsจบลูป |
Other ways to countวิธีนับแบบอื่นๆ
Programmers usually start counting at 0, because positions in a list start at 0 (lesson 6). i is short for index; any name works.
โปรแกรมเมอร์มักเริ่มนับจาก 0 เพราะตำแหน่งในรายการเริ่มที่ 0 (บทที่ 6) ส่วน i ย่อมาจาก index (ดัชนี) จะตั้งชื่ออื่นก็ได้
Lesson 5 · Repetitionบทที่ 5 · การวนซ้ำ
while & do…whilewhile และ do…while
while repeats as long as its condition stays true. Use it when you don't know in advance how many rounds you need.
while จะวนซ้ำไปเรื่อยๆ ตราบใดที่เงื่อนไขยังเป็น true เหมาะกับกรณีที่ไม่รู้ล่วงหน้าว่าต้องวนกี่รอบ
do…while checks the condition after the body, so the body always runs at least once. That suits things like "ask once, then ask again if the answer was wrong".
do…while ตรวจเงื่อนไขหลังทำงานในบล็อก จึงทำงานอย่างน้อยหนึ่งรอบเสมอ เหมาะกับงานแบบ "ถามหนึ่งครั้ง แล้วถามใหม่ถ้าคำตอบผิด"
If the condition never becomes false, the loop never ends. This is an infinite loop. Make sure something inside the loop moves it toward stopping, like month++ above. This page stops code that runs longer than 2 seconds, so feel free to experiment.
ถ้าเงื่อนไขไม่มีวันเป็น false ลูปจะไม่มีวันจบ เรียกว่า ลูปไม่รู้จบ (infinite loop) ต้องแน่ใจว่ามีบางอย่างในลูปที่ทำให้เข้าใกล้จุดหยุด เช่น month++ ด้านบน หน้านี้จะหยุดโค้ดที่ทำงานนานเกิน 2 วินาทีให้อัตโนมัติ จึงทดลองได้อย่างสบายใจ
- Use for when you know how many rounds you need, or have a list to go through.ใช้ for เมื่อรู้จำนวนรอบ หรือมีรายการให้วนผ่าน
- Use while when you repeat until something happens.ใช้ while เมื่อต้องทำซ้ำจนกว่าจะเกิดเหตุการณ์บางอย่าง
Lesson 6 · Repetitionบทที่ 6 · การวนซ้ำ
Looping through listsวนลูปผ่านรายการ (array)
An array is an ordered list of values, written in square brackets. Each item has a position called its index, which starts at 0. Loops and arrays go together: you will often loop over a list to read, count or change each item.
อาร์เรย์ (array) คือรายการของค่าที่เรียงตามลำดับ เขียนไว้ในวงเล็บเหลี่ยม [ ] แต่ละค่ามีตำแหน่งเรียกว่า index (ดัชนี) ซึ่งเริ่มจาก 0 ลูปกับอาร์เรย์มักใช้คู่กัน เรามักวนลูปผ่านรายการเพื่ออ่าน นับ หรือแก้ไขแต่ละค่า
break and continuebreak และ continue
breakstops the loop completely and jumps to the code after it.breakหยุดลูปทันที แล้วข้ามไปทำโค้ดที่อยู่ถัดจากลูปcontinueskips the rest of this round and moves on to the next one.continueข้ามโค้ดที่เหลือในรอบนี้ แล้วไปเริ่มรอบถัดไป
Lesson 7 · Reuseบทที่ 7 · การนำกลับมาใช้ซ้ำ
Functions & methodsฟังก์ชันและเมธอด
A function is a named block of code you can run again and again. You give it inputs called parameters, it does its work, and it can hand back a result with return.
ฟังก์ชัน (function) คือบล็อกโค้ดที่มีชื่อและเรียกใช้ซ้ำได้เรื่อยๆ เราส่งข้อมูลขาเข้าที่เรียกว่า พารามิเตอร์ (parameter) ให้ ฟังก์ชันจะทำงาน แล้วส่งผลลัพธ์กลับมาด้วยคำสั่ง return
getGrade needs no else. return leaves the function immediately, so each later line only runs if every earlier condition was false.
getGrade ไม่ต้องใช้ else เพราะ return จะออกจากฟังก์ชันทันที บรรทัดถัดไปจึงทำงานเฉพาะเมื่อเงื่อนไขก่อนหน้าทั้งหมดเป็นเท็จ
Function or method?ฟังก์ชันกับเมธอดต่างกันอย่างไร?
A method is a function that belongs to a value or object. You call it with a dot: name.toUpperCase(), list.push(4), Math.round(2.5). The next lesson lists the ones you'll use most.
เมธอด (method) ก็คือฟังก์ชันที่เป็นของค่าหรือออบเจกต์หนึ่งๆ เรียกใช้ด้วยจุด เช่น name.toUpperCase(), list.push(4), Math.round(2.5) บทถัดไปรวมเมธอดที่ใช้บ่อยที่สุดไว้ให้
Lesson 8 · Toolboxบทที่ 8 · กล่องเครื่องมือ
Everyday built-in methodsเมธอดสำเร็จรูปที่ใช้บ่อย
JavaScript comes with ready-made methods for text, lists and numbers. You don't need to memorize them all. Learn these, and look up the rest when you need them. JavaScript มีเมธอดสำเร็จรูปสำหรับข้อความ รายการ และตัวเลขให้ใช้ ไม่จำเป็นต้องจำทั้งหมด เรียนรู้ชุดนี้ก่อน แล้วค่อยค้นหาตัวอื่นเมื่อต้องใช้
| Methodเมธอด | What it doesหน้าที่ | Exampleตัวอย่าง | Resultผลลัพธ์ |
|---|---|---|---|
.length | Number of characters (a property, so no brackets)จำนวนตัวอักษร (เป็น property จึงไม่มีวงเล็บ) | "hello".length | 5 |
.toUpperCase().toLowerCase() | Change to capital / small lettersเปลี่ยนเป็นตัวพิมพ์ใหญ่ / ตัวพิมพ์เล็ก | "Hello".toUpperCase() | "HELLO" |
.trim() | Remove spaces at the start and endตัดช่องว่างที่หัวและท้ายข้อความ | " hi ".trim() | "hi" |
.includes(text) | Does it contain this text?มีข้อความนี้อยู่หรือไม่ | "banana".includes("nan") | true |
.startsWith(text) | Does it begin with this text?ขึ้นต้นด้วยข้อความนี้หรือไม่ | "0812345678".startsWith("08") | true |
.indexOf(text) | Position of the text, or -1 if not foundตำแหน่งของข้อความ หรือ -1 ถ้าไม่พบ | "hello".indexOf("l") | 2 |
.slice(start, end) | Copy part of the text (end is not included)ตัดเอาบางส่วนของข้อความ (ไม่รวมตำแหน่ง end) | "JavaScript".slice(0, 4) | "Java" |
.split(separator) | Cut the text into an arrayแยกข้อความออกเป็นอาร์เรย์ | "a,b,c".split(",") | ["a", "b", "c"] |
.replace(old, new) | Replace the first matchแทนที่ข้อความที่พบครั้งแรก | "I like cats".replace("cats", "dogs") | "I like dogs" |
| Methodเมธอด | What it doesหน้าที่ | Exampleตัวอย่าง | Resultผลลัพธ์ |
|---|---|---|---|
.length | Number of itemsจำนวนสมาชิก | [10, 20, 30].length | 3 |
.push(item) | Add an item to the end (changes the array)เพิ่มสมาชิกต่อท้าย (แก้ไขอาร์เรย์เดิม) | a = [1, 2]; a.push(3) | a → [1, 2, 3] |
.pop() | Remove and return the last itemลบและคืนค่าสมาชิกตัวสุดท้าย | [1, 2, 3].pop() | 3 |
.includes(item) | Is this item in the list?มีค่านี้อยู่ในรายการหรือไม่ | ["a", "b"].includes("b") | true |
.indexOf(item) | Position of the item, or -1ตำแหน่งของค่า หรือ -1 ถ้าไม่พบ | ["a", "b", "c"].indexOf("c") | 2 |
.join(separator) | Glue all items into one stringรวมสมาชิกทั้งหมดเป็นข้อความเดียว | ["a", "b", "c"].join("-") | "a-b-c" |
.forEach(fn) | Run a function for each itemเรียกฟังก์ชันกับสมาชิกทุกตัว | [1, 2].forEach(x => console.log(x)) | prints 1, then 2แสดง 1 แล้วแสดง 2 |
.map(fn) | New array with every item changedสร้างอาร์เรย์ใหม่ที่แปลงค่าทุกตัว | [1, 2, 3].map(x => x * 2) | [2, 4, 6] |
.filter(fn) | New array with only the items that pass a testสร้างอาร์เรย์ใหม่ที่มีเฉพาะค่าที่ผ่านเงื่อนไข | [5, 12, 8, 3].filter(x => x > 6) | [12, 8] |
.find(fn) | First item that passes the testค่าแรกที่ผ่านเงื่อนไข | [5, 12, 8].find(x => x > 6) | 12 |
.sort(fn) | Sort the array in place. For numbers, pass (a, b) => a - bเรียงลำดับอาร์เรย์เดิม ถ้าเป็นตัวเลขให้ส่ง (a, b) => a - b | [10, 1, 5].sort((a, b) => a - b) | [1, 5, 10] |
| Methodเมธอด | What it doesหน้าที่ | Exampleตัวอย่าง | Resultผลลัพธ์ |
|---|---|---|---|
Math.round(x) | Round to the nearest whole numberปัดเศษเป็นจำนวนเต็มที่ใกล้ที่สุด | Math.round(4.6) | 5 |
Math.floor(x) | Round downปัดลง | Math.floor(4.9) | 4 |
Math.ceil(x) | Round upปัดขึ้น | Math.ceil(4.1) | 5 |
Math.max(...)Math.min(...) | Largest / smallest of the valuesค่ามากที่สุด / น้อยที่สุด | Math.max(3, 9, 2) | 9 |
Math.abs(x) | Distance from zero (drops the minus sign)ค่าสัมบูรณ์ (ตัดเครื่องหมายลบออก) | Math.abs(-7) | 7 |
Math.random() | Random number from 0 up to, but not including, 1สุ่มตัวเลขตั้งแต่ 0 แต่ไม่ถึง 1 | Math.random() | e.g. 0.417… (changes every time)เช่น 0.417… (เปลี่ยนทุกครั้ง) |
| Roll a die: random whole number 1–6ทอยลูกเต๋า: สุ่มจำนวนเต็ม 1–6 | Math.floor(Math.random() * 6) + 1 | 1…6 | |
Number(text) | Turn text into a numberแปลงข้อความเป็นตัวเลข | Number("42") | 42 |
parseInt(text) | Read a whole number from the start of the textอ่านจำนวนเต็มจากต้นข้อความ | parseInt("42px") | 42 |
.toFixed(digits) | Format with a fixed number of decimals (gives text)จัดรูปแบบทศนิยมตามจำนวนหลัก (ได้ผลเป็นข้อความ) | (3.14159).toFixed(2) | "3.14" |
String(x) | Turn any value into textแปลงค่าใดๆ เป็นข้อความ | String(99) | "99" |
Put them togetherลองใช้ร่วมกัน
- Chaining:
input.trim().toLowerCase()trims first, then lowercases the result.การเรียกต่อกัน (chaining):input.trim().toLowerCase()จะตัดช่องว่างก่อน แล้วจึงแปลงผลลัพธ์เป็นตัวพิมพ์เล็ก - Changes vs copies: some methods change the original array (
push,pop,sort). Others return something new and leave the original alone (map,filter,slice, and every string method).แก้ของเดิม หรือสร้างใหม่: บางเมธอดแก้ไขอาร์เรย์เดิมโดยตรง (push,pop,sort) ส่วนบางเมธอดคืนค่าใหม่โดยไม่แตะของเดิม (map,filter,sliceและเมธอดของ string ทุกตัว)
Lesson 9 · Your turnบทที่ 9 · ถึงตาคุณแล้ว
Practiceแบบฝึกหัด
Five exercises, from easy to harder. Write your answer in the editor and press Run. Open the solution only after you have tried. แบบฝึกหัด 5 ข้อ เรียงจากง่ายไปยาก เขียนคำตอบในตัวแก้โค้ดแล้วกด Run ลองทำเองก่อน แล้วค่อยเปิดดูเฉลย
Exercise 1 · Even totalแบบฝึกหัด 1 · ผลรวมเลขคู่
Add up all the even numbers from 1 to 50 and print the total. The correct answer is 650.หาผลรวมของเลขคู่ทั้งหมดตั้งแต่ 1 ถึง 50 แล้วแสดงผลรวม คำตอบที่ถูกต้องคือ 650
Uses:ใช้: for if % +=
Exercise 2 · Calculatorแบบฝึกหัด 2 · เครื่องคิดเลข
Use switch on op to print the result of a and b. Support +, -, * and /. For any other operator, print "Unknown operator".ใช้ switch กับตัวแปร op เพื่อแสดงผลลัพธ์ของ a กับ b รองรับ +, -, * และ / ถ้าเป็นเครื่องหมายอื่นให้แสดง "ไม่รู้จักเครื่องหมายนี้"
Uses:ใช้: switch case break default
Exercise 3 · FizzBuzzแบบฝึกหัด 3 · FizzBuzz
Print the numbers 1 to 20. For multiples of 3 print "Fizz" instead, for multiples of 5 print "Buzz", and for multiples of both print "FizzBuzz". This is a classic job-interview warm-up.แสดงตัวเลข 1 ถึง 20 ถ้าเป็นพหุคูณของ 3 ให้แสดง "Fizz" แทน ถ้าเป็นพหุคูณของ 5 ให้แสดง "Buzz" และถ้าเป็นพหุคูณของทั้งสองให้แสดง "FizzBuzz" โจทย์นี้เป็นโจทย์อุ่นเครื่องยอดนิยมในการสัมภาษณ์งาน
Uses:ใช้: for if else if % &&
Exercise 4 · Ticket priceแบบฝึกหัด 4 · ราคาตั๋ว
Write a function ticketPrice(age) that returns the price in baht: under 4 is free (0), ages 4 to 12 pay 100, 60 and over pay 120, and everyone else pays 200.เขียนฟังก์ชัน ticketPrice(age) ที่คืนค่าราคาตั๋วเป็นบาท: อายุต่ำกว่า 4 ปีเข้าฟรี (0) อายุ 4–12 ปีจ่าย 100 อายุ 60 ปีขึ้นไปจ่าย 120 และคนอื่นๆ จ่าย 200
Uses:ใช้: function if else if return
Exercise 5 · Longest wordแบบฝึกหัด 5 · คำที่ยาวที่สุด
Find the longest word in the list and print it. Then change the list and check that your code still works.หาคำที่ยาวที่สุดในรายการแล้วแสดงผล จากนั้นลองเปลี่ยนรายการ แล้วตรวจว่าโค้ดยังทำงานถูกต้อง
Uses:ใช้: for…of if .length
Lesson 10 · Debuggingบทที่ 10 · การแก้บั๊ก
Common mistakesข้อผิดพลาดที่พบบ่อย
Everyone makes these. Knowing them by sight will save you hours. ทุกคนเคยพลาดแบบนี้ ถ้าจำรูปแบบได้ จะช่วยประหยัดเวลาแก้บั๊กไปได้หลายชั่วโมง
= instead of ===ใช้ = แทน ===
A single = assigns, so this condition is always true, and it also changes score to 100.= ตัวเดียวคือการกำหนดค่า เงื่อนไขนี้จึงเป็นจริงเสมอ และยังเปลี่ยนค่า score เป็น 100 ด้วย
Forgetting break in switchลืม break ใน switch
Without break, a matching case keeps running into the cases below it.ถ้าไม่มี break เมื่อ case ใดตรงแล้ว โค้ดจะทำงานต่อลงไปยัง case ด้านล่างด้วย
Off by oneเกินมาหนึ่งรอบ (off by one)
Indexes go from 0 to length - 1. Using <= reads one item past the end, which is undefined.index มีค่าตั้งแต่ 0 ถึง length - 1 ถ้าใช้ <= จะอ่านเกินท้ายรายการไปหนึ่งตัว ซึ่งได้ค่า undefined
A loop that never endsลูปที่ไม่มีวันจบ
i never changes, so i < 5 stays true forever.i ไม่เคยเปลี่ยนค่า เงื่อนไข i < 5 จึงเป็นจริงตลอดไป
Text that looks like a numberข้อความที่ดูเหมือนตัวเลข
+ also joins text. Values from forms and inputs arrive as text, so convert them first.+ ใช้ต่อข้อความได้ด้วย ค่าที่มาจากฟอร์มหรือช่องกรอกข้อมูลเป็นข้อความ จึงต้องแปลงเป็นตัวเลขก่อน
Forgetting returnลืม return
A function without return gives back undefined, even if it calculated the right value.ฟังก์ชันที่ไม่มี return จะคืนค่า undefined แม้จะคำนวณค่าที่ถูกต้องไว้แล้วก็ตาม
Lesson 11 · Beyond JavaScriptบทที่ 11 · นอกเหนือจาก JavaScript
Same logic in PHP & Pythonตรรกะเดียวกันใน PHP และ Python
Once the logic makes sense, a new language is mostly new spelling. PHP puts $ in front of variables. Python uses a colon and indentation instead of curly braces, and has no do…while.
เมื่อเข้าใจตรรกะแล้ว การเรียนภาษาใหม่ส่วนใหญ่ก็แค่เปลี่ยนวิธีเขียน PHP ใส่ $ หน้าชื่อตัวแปร ส่วน Python ใช้เครื่องหมาย : และการย่อหน้าแทนวงเล็บปีกกา และไม่มี do…while
| Conceptแนวคิด | JavaScript | PHP | Python |
|---|---|---|---|
| if / elseif / else | if (x > 0) {
...
} else if (x === 0) {
...
} else {
...
} |
if ($x > 0) {
...
} elseif ($x === 0) {
...
} else {
...
} |
if x > 0:
...
elif x == 0:
...
else:
... |
| switchswitch | switch (day) {
case "sat":
...
break;
default:
...
} |
switch ($day) {
case "sat":
...
break;
default:
...
} |
match day: # 3.10+
case "sat":
...
case _:
... |
| for (counting)for (นับรอบ) | for (let i = 0; i < 5; i++) {
...
} |
for ($i = 0; $i < 5; $i++) {
...
} |
for i in range(5):
... |
| Loop over a listวนผ่านรายการ | for (const x of list) {
...
} |
foreach ($list as $x) {
...
} |
for x in list:
... |
| whilewhile | while (n > 0) {
n--;
} |
while ($n > 0) {
$n--;
} |
while n > 0:
n -= 1 |
| Functionฟังก์ชัน | function add(a, b) {
return a + b;
} |
function add($a, $b) {
return $a + $b;
} |
def add(a, b):
return a + b |
| Printแสดงผล | console.log(x); |
echo $x; |
print(x) |
| and / or / notและ / หรือ / ไม่ | && || ! |
&& || ! |
and or not |
| Lengthความยาว | list.length text.length |
count($list) strlen($text) |
len(list) len(text) |
Lesson 12 · Referenceบทที่ 12 · อ้างอิง
Glossaryคำศัพท์
The English terms are what you'll see in documentation, error messages and search results, so it pays to know both. คำศัพท์ภาษาอังกฤษคือสิ่งที่จะเจอในเอกสาร ข้อความแจ้งข้อผิดพลาด และผลการค้นหา จึงควรรู้ทั้งสองภาษา
| English | ไทย | Meaningความหมาย |
|---|---|---|
| variable | ตัวแปร | A named box that stores a valueกล่องที่มีชื่อ ใช้เก็บค่า |
| value | ค่า | One piece of data, like 42, "hi" or trueข้อมูลหนึ่งชิ้น เช่น 42, "hi" หรือ true |
| boolean | บูลีน | A value that is either true or falseค่าที่เป็นได้แค่ true (จริง) หรือ false (เท็จ) |
| string | สตริง / ข้อความ | Text, written inside quotesข้อความ เขียนอยู่ในเครื่องหมายคำพูด |
| condition | เงื่อนไข | An expression that is true or false, used by if and loopsนิพจน์ที่ให้ผลเป็นจริงหรือเท็จ ใช้กับ if และลูป |
| operator | ตัวดำเนินการ | A symbol that works on values, like +, ===, &&สัญลักษณ์ที่ใช้กระทำกับค่า เช่น +, ===, && |
| block | บล็อก | Code grouped between { and }กลุ่มโค้ดที่อยู่ระหว่าง { และ } |
| loop | ลูป / การวนซ้ำ | Code that repeatsโค้ดที่ทำงานซ้ำ |
| iteration | รอบการวนซ้ำ | One round of a loopการทำงานหนึ่งรอบของลูป |
| infinite loop | ลูปไม่รู้จบ | A loop whose condition never becomes falseลูปที่เงื่อนไขไม่มีวันเป็นเท็จ |
| array | อาร์เรย์ | An ordered list of valuesรายการของค่าที่เรียงตามลำดับ |
| index | ดัชนี / ตำแหน่ง | An item's position in an array, starting at 0ตำแหน่งของสมาชิกในอาร์เรย์ เริ่มจาก 0 |
| function | ฟังก์ชัน | A named, reusable block of codeบล็อกโค้ดที่มีชื่อและเรียกใช้ซ้ำได้ |
| method | เมธอด | A function that belongs to a value or objectฟังก์ชันที่เป็นของค่าหรือออบเจกต์ |
| parameter | พารามิเตอร์ | The input name in a function's definitionชื่อข้อมูลขาเข้าที่ประกาศไว้ในฟังก์ชัน |
| argument | อาร์กิวเมนต์ | The actual value you pass when calling a functionค่าจริงที่ส่งเข้าไปตอนเรียกใช้ฟังก์ชัน |
| return value | ค่าที่ส่งกลับ | The result a function hands backผลลัพธ์ที่ฟังก์ชันส่งกลับมา |
| bug | บั๊ก | A mistake that makes code behave wronglyข้อผิดพลาดที่ทำให้โค้ดทำงานผิด |