Logic & Loops

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 แล้วดูว่าเกิดอะไรขึ้น

Examples are in JavaScript. The same ideas work in PHP, Python, Java and most other languages (see lesson 11). ตัวอย่างใช้ภาษา JavaScript แต่แนวคิดเดียวกันใช้ได้กับ PHP, Python, Java และภาษาอื่นๆ เกือบทั้งหมด (ดูบทที่ 11)

Shortcut: pressคีย์ลัด: กด Ctrl+Enter (⌘+Enter Mac) inside a code box to run it.ในกล่องโค้ดเพื่อรันทันที

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 === 5true
!==not equal toไม่เท่ากับ5 !== 3true
>greater thanมากกว่า7 > 10false
<less thanน้อยกว่า7 < 10true
>=greater than or equal toมากกว่าหรือเท่ากับ10 >= 10true
<=less than or equal toน้อยกว่าหรือเท่ากับ3 <= 2false
&&and: both sides must be trueและ: ต้องจริงทั้งสองฝั่งtrue && falsefalse
||or: at least one side is trueหรือ: จริงอย่างน้อยหนึ่งฝั่งtrue || falsetrue
!not: flips the valueไม่ (นิเสธ): กลับค่าจริงเป็นเท็จ!truefalse
Tipเคล็ดลับ

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 จะตรวจจากบนลงล่าง ทำงานในเงื่อนไขแรกที่เป็นจริง แล้วข้ามส่วนที่เหลือทั้งหมด

score >= 80 score >= 65 score >= 50 false false false true true true A B C F
How the grade example below is checked. Each diamond is a question, and the first true answer wins. The highlighted path is what happens when score is 72. ลำดับการตรวจของตัวอย่างเกรดด้านล่าง รูปข้าวหลามตัดแต่ละอันคือคำถาม และคำตอบแรกที่เป็น true จะถูกเลือก เส้นที่ไฮไลต์คือเส้นทางเมื่อ score เท่ากับ 72
Tipเคล็ดลับ

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 ใดตรงเลย

Try itลองดู

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 เมื่อเทียบค่าเดียวกับตัวเลือกที่ต้องตรงกันพอดีหลายค่า เช่น เมนู รหัสสถานะ หรือชื่อวัน
  • switch compares with ===, so the number 1 does 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 ส่วน คั่นด้วยเครื่องหมายอัฒภาค (;)

  1. 1Start. let i = 0 runs once, before the first round. It creates the counter.จุดเริ่มต้น let i = 0 ทำงานครั้งเดียวก่อนรอบแรก เพื่อสร้างตัวนับ
  2. 2Condition. i < 5 is checked before every round. When it becomes false, the loop ends.เงื่อนไข i < 5 ถูกตรวจก่อนทุกรอบ เมื่อกลายเป็น false ลูปจะจบ
  3. 3Update. i++ runs after every round. It adds 1 to i.การอัปเดต i++ ทำงานหลังจบแต่ละรอบ โดยเพิ่มค่า i ทีละ 1
① let i = 0 ② i < 5 true console.log(i) ③ i++ check againกลับไปตรวจเงื่อนไขอีกครั้ง false loop endsจบลูป

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รอบii < 3Outputผลลัพธ์
10true0
21true1
32true2
–3falseloop endsจบลูป

Other ways to countวิธีนับแบบอื่นๆ

Tipเคล็ดลับ

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 ตรวจเงื่อนไขหลังทำงานในบล็อก จึงทำงานอย่างน้อยหนึ่งรอบเสมอ เหมาะกับงานแบบ "ถามหนึ่งครั้ง แล้วถามใหม่ถ้าคำตอบผิด"

Watch outระวัง

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

  • break stops the loop completely and jumps to the code after it.break หยุดลูปทันที แล้วข้ามไปทำโค้ดที่อยู่ถัดจากลูป
  • continue skips 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

Noteสังเกต

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ผลลัพธ์
.lengthNumber of characters (a property, so no brackets)จำนวนตัวอักษร (เป็น property จึงไม่มีวงเล็บ)"hello".length5
.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"

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 ด้วย

✗ Wrong✗ ผิด
✓ Right✓ ถูก

Forgetting break in switchลืม break ใน switch

Without break, a matching case keeps running into the cases below it.ถ้าไม่มี break เมื่อ case ใดตรงแล้ว โค้ดจะทำงานต่อลงไปยัง case ด้านล่างด้วย

✗ Wrong✗ ผิด
✓ Right✓ ถูก

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

✗ Wrong✗ ผิด
✓ Right✓ ถูก

A loop that never endsลูปที่ไม่มีวันจบ

i never changes, so i < 5 stays true forever.i ไม่เคยเปลี่ยนค่า เงื่อนไข i < 5 จึงเป็นจริงตลอดไป

✗ Wrong✗ ผิด
✓ Right✓ ถูก

Text that looks like a numberข้อความที่ดูเหมือนตัวเลข

+ also joins text. Values from forms and inputs arrive as text, so convert them first.+ ใช้ต่อข้อความได้ด้วย ค่าที่มาจากฟอร์มหรือช่องกรอกข้อมูลเป็นข้อความ จึงต้องแปลงเป็นตัวเลขก่อน

✗ Wrong✗ ผิด
✓ Right✓ ถูก

Forgetting returnลืม return

A function without return gives back undefined, even if it calculated the right value.ฟังก์ชันที่ไม่มี return จะคืนค่า undefined แม้จะคำนวณค่าที่ถูกต้องไว้แล้วก็ตาม

✗ Wrong✗ ผิด
✓ Right✓ ถูก

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แนวคิดJavaScriptPHPPython
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ข้อผิดพลาดที่ทำให้โค้ดทำงานผิด