Automatically translated.View original post

function ( ) / JavaScript

# Web Site Writing Basics

# javascript

# Function

Function in JavaScript is a block of reusable code designed to perform a particular task or process a certain value and send back the result (optional). 💡

Function is the most basic and important component in programming with JavaScript because it keeps code organized, easy to manage, and runs efficiently.

1. Main elements of the function

Functions in JavaScript include the following key parts:

Element, meaning, example (in function greet (name) {return "Hello," + name;})

Function name (name) The name used to call the greet function.

Parameters, a variable specified in parentheses that receives external entry values

Body (Body) A block of code inside the {} brace that will be processed when the function is called return "Hello," + name;

Return Value The value that the function sends back to the executed code (without the return command, it will automatically return undefined) returns "Hello," + name;

Export to sheet

2. Main benefits of the function

Reusability: We can write a set of instructions once and run it repeatedly in a program, reducing redundant code (Don't Repeat Yourself - DRY)

Organization: Helps break large code into functional subsections, making the code easier to read and manage (Modularity)

Debugging Ease: When an error occurs, the code can be checked and corrected one function at a time, narrowing the scope of the problem.

3. Function Syntax Format

JavaScript provides 3 main ways of creating functions:

3.1. function declaration (Function Declaration)

It is the most primitive and basic form. Functions created in this way are "elevated" (Hoisting) to the top of the scope (Scope), allowing the function to be called before the line declaring the function.

JavaScript

Function add (a, b) {

return a + b;

}

Let result = add (5, 3); / / Run immediately

3.2.Function expression (Function Expression)

Is to create an anonymous function and assign it to a variable:

JavaScript

Const subtract = function (a, b) {

return a - b;

};

Let result = subtract (10, 4); / / must be run only after the variable-defined line.

3.3.Arrow Functions (Arrow Functions - ES6)

It is a very short, concise and popular form of function writing today, using the symbol = > instead of the function:

JavaScript

/ / Basic format

Const times = (a, b) = > {

return a * b;

};

/ / Implict Return

/ / For a function that has a single command and needs to be restored.

const divide = (a, b) = > a / b;

Let result 1 = multiply (2, 4); / / 8

Let result 2 = divide (10, 2); / / 5

4. Relationship between function and JavaScript

In JavaScript, the function is considered a First-Class Citizen, which means:

Functions can be assigned to variables (as seen in Function Expression).

Functions can be sent as arguments to other functions (e.g. in Callback Functions).

A function can return another function as a result (e.g. in Higher-Order Functions).

This ability makes JavaScript very flexible and capable of programming in complex ways such as functional programming.

2025/10/15 Edited to

... Read moreถ้าคุณกำลังค้นหา “JavaScript คืออะไร” หรือ “ฟังก์ชัน ภาษาไทย” แบบเข้าใจง่าย ฉันแนะนำให้เริ่มจากภาพรวมก่อนว่า JavaScript คือภาษาที่ทำให้หน้าเว็บ “โต้ตอบได้” (interactive) เช่น กดปุ่มแล้วเปลี่ยนข้อความ, คำนวณราคาอัตโนมัติ, ตรวจสอบฟอร์มก่อนส่ง หรือดึงข้อมูลจาก API มาแสดงบนหน้าเว็บ พอเข้าใจภาพนี้แล้ว “ฟังก์ชัน (Function)” จะกลายเป็นเครื่องมือหลักที่ช่วยจัดโค้ดให้เป็นชิ้น ๆ และเรียกใช้ซ้ำได้ สิ่งที่ฉันเจอบ่อยตอนเริ่มเรียนคือสับสนระหว่าง “พารามิเตอร์ (Parameters)” กับ “อาร์กิวเมนต์ (Arguments)”. จำง่าย ๆ แบบนี้: - Parameters = ตัวแปรที่ประกาศไว้ตอนสร้างฟังก์ชัน (เช่น name) - Arguments = ค่าจริงที่ส่งเข้าไปตอนเรียกใช้ (เช่น "สมชาย") ตัวอย่างสั้น ๆ ภาษาไทย: function greet(name) { return "สวัสดีครับ " + name + "!"; } console.log(greet("สมชาย")); ในตัวอย่างนี้ name คือพารามิเตอร์ ส่วน "สมชาย" คืออาร์กิวเมนต์ และ return คือคำสั่ง “ส่งค่ากลับ” ออกไป อีกจุดที่ช่วยให้เข้าใจเร็วคือ “ฟังก์ชันคืนค่า vs ไม่คืนค่า”: - ฟังก์ชันที่ return เหมาะกับงานคำนวณ/ประมวลผล เช่น บวกเลข คำนวณพื้นที่ แล้วเอาค่าไปใช้ต่อ - ฟังก์ชันที่ไม่ return มักใช้ทำ “ผลข้างเคียง” เช่น console.log, แก้ไข DOM, ส่งคำสั่งให้หน้าเว็บเปลี่ยน ฉันมักใช้ตัวอย่างคำนวณพื้นที่เพื่อฝึกเรื่อง function expression ด้วย: const calculateArea = function(width, height) { return width * height; }; const area = calculateArea(5, 10); console.log(area); // 50 และเพื่อให้โค้ดสั้นลง จะเปลี่ยนเป็น arrow function ได้แบบนี้: const calculateArea2 = (width, height) => width * height; ทิปที่ใช้งานจริงและคนค้นหาบ่อยคือ “พารามิเตอร์เริ่มต้น (Default Parameters)” เวลาไม่ได้ส่งค่ามา: function sayHello(name = "ผู้ใช้งาน") { return "Hello, " + name; } console.log(sayHello()); // Hello, ผู้ใช้งาน console.log(sayHello("Alice")); // Hello, Alice สุดท้าย ถ้าคุณยังงงว่าเมื่อไหร่ควรใช้แบบไหน ฉันสรุปแบบใช้งานจริงไว้ว่า: - Function Declaration: เหมาะกับฟังก์ชันหลัก ๆ ของไฟล์ เรียกใช้ได้ก่อนประกาศ (เพราะ hoisting) - Function Expression: เหมาะกับการกำหนดเป็นตัวแปร/ส่งเป็นค่า (เช่น callback) - Arrow Function: เหมาะกับฟังก์ชันสั้น ๆ โดยเฉพาะใน callback เช่น map/filter และงานที่ต้องการโค้ดกระชับ ลองฝึกโดยเลือกโจทย์เล็ก ๆ เช่น “รับชื่อแล้วทักทาย”, “รับคะแนนแล้วตัดเกรด”, “รับราคา+ส่วนลดแล้วคืนยอดสุทธิ” จะทำให้เข้าใจว่า JavaScript คืออะไร และฟังก์ชันช่วยจัดโค้ดให้คิดเป็นระบบขึ้นจริง ๆ

Related posts

My Programming Interest Just Skyrocketed! 🚀
I just discovered something that has made my interest in programming explode to 1000000000000000%! 🤯 🎮 Learning Programming is Like a Game Adventure! From Python, Java, JavaScript, to HTML, there are all kinds of programming languages available, along with systematic topic courses. The learning p
Valder

Valder

2173 likes

The image displays the title 'BACKEND DEVELOPMENT ROADMAP: EVERYTHING YOU NEED TO LEARN!' with a subtitle 'Master the skills to power the web!' over a background of a person looking at a computer screen with code.
This image defines backend development, explaining it handles data, logic, and server-side processing, manages databases and APIs, and works with the frontend. An illustration of a laptop with code and gear icons represents backend functions.
The image lists backend programming languages like JavaScript (Node.js), Python (Django, Flask), Java (Spring Boot), C# (.NET), and Ruby (Ruby on Rails), with their respective logos, advising to focus on one.
Backend Development Roadmap
Want to become a Backend Developer but don’t know where to start? 🤔 This step-by-step roadmap will guide you through the essential skills needed to build powerful, scalable web applications! #codingforbeginners #backenddeveloper #htmlcssforbeginners #studymotivations #javascript
Aysha

Aysha

24 likes

This is my function🖤
This is my function (aka an everything shower, clean room, dark romance book) #relatab #girlhood #girlproblems #girltherapy #aestheticvibes
lexa smith ⋆˙⟡♡

lexa smith ⋆˙⟡♡

1892 likes

free tech certifications - 3 websites
1. freeCodeCamp • Why it’s great: Offers free certifications in web development, data analysis, and machine learning. • Popular certs: Responsive Web Design, Data Analysis with Python, and JavaScript Algorithms. • Best for: Beginners starting with coding and tech basic
vedha | career tips (tech) 👩‍

vedha | career tips (tech) 👩‍

2175 likes

A laptop displays coding terminal output with a keyboard in the foreground, featuring the text "BEST AI Tutor for Coding," highlighting an AI-powered programming learning platform.
BEST AI Tutor for Coding
Best AI Tutor for Coding in 2026? This Is What Serious Learners Are Switching To Most people don’t fail at coding because it’s hard. They fail because they’re learning alone. Watching tutorials. Copying code. Getting stuck. Quitting. BootSelf changes that. BootSelf is an AI-powered coding tuto
Code with BootSelf AI

Code with BootSelf AI

17 likes

Relieve back pain and enhance kidney function
#taichi #chineseculture #exercises #healthylifestyle #backpain
Lijun kungfu

Lijun kungfu

2126 likes

How I’d Learn JavaScript If I Were Starting Again
#javascript #coding #programming #tech #codingforbeginners
Aysha

Aysha

124 likes

A woman with curly hair and glasses, holding a microphone, with text overlays 'SQL Windows Function Explained' and 'Mad Key'.
A bookshelf background with 'SQL Windows Function Explained' and a quote defining a window function as viewing a 'slice' of data while seeing the bigger picture.
A SQL code example using a window function (RANK() OVER) to rank sales, showing output with individual sales records and their ranks, maintaining detail.
SQL: Windows Function Explained
One thing I've discovered is that when working with data, Normal SQL (GROUP BY) helps summarize data but sacrifices row-level detail in the process. On the other hand, Window Functions allow you to perform calculations across rows while maintaining each row’s detail. This key difference m
NeLo

NeLo

58 likes

Learning React : Programming
Group project and honestly the guys have been doing the hard work lol . They’ve been encouraging towards my contributions. It’s always great learning with others that are already in the field and more versed in a programming language . Have you ever had to work with people that were more advanced ?
Mother

Mother

9 likes

A man with tattoos and a cap sits at a desk, coding on a large monitor in a dimly lit room. Text overlays read 'SWIPE', 'Learning JavaScript?', and 'This is for YOU!', with a thinking emoji, promoting coding education.
A man with headphones and a cap codes at a desk with a large monitor displaying code. Text overlays read 'SWIPE', 'Break vs Continue', and 'In JS loops', with a thinking emoji, highlighting JavaScript loop concepts.
A man with headphones and a cap sits at a desk, coding on a large monitor in a bright room. Text overlays read 'Let's Discuss!' with a thinking emoji, inviting interaction about coding topics.
Coding W/ JavaScript?
As a software engineer w/ ADHD, understanding the difference between certain syntax keywords is critical, such as ‘continue’ and ‘break’ within JavaScript loops.💡 It’s almost like centering a <div> — it takes practice to feel confident in your abilities. 🛠️ When my mind gets stuck in a
Michael Burbank

Michael Burbank

13 likes

UI Designer - tips for a career
1. Learn the Fundamentals of UI Design -Basic Design Principles: -Typography: Learn how to use fonts effectively (size, hierarchy, readability). -Color Theory: Understand color combinations, palettes, and contrast. -Layout and Grids: Study alignment, spacing, and grid systems.
vedha | career tips (tech) 👩‍

vedha | career tips (tech) 👩‍

27 likes

A smiling woman, Miranda, codes at her desk, surrounded by motivational tech-themed decor. She uses an Apple Pencil with a tablet, with Python, JavaScript, and SQL code on her monitor. Her red Jordans are visible, embodying her 'coding is cool' passion.
“Coding Is Cool, But Watching Your Dreams Compile Successfully Is Even Better.”
Powered by Python. Fueled by JavaScript. Organized by SQL. With an Apple Pencil in hand and determination in her heart, Miranda turns ideas into reality one line of code at a time. Surrounded by reminders to learn, build, and grow, she’s creating her future from the comfort of her coding corner.
mirandaperry05

mirandaperry05

1 like

$1099 Later… Here’s the Truth About the iPad Pro✨
$1099 might sound crazy for a tablet… but here’s why this iPad Pro became my ride-or-die as a work-from-home mom and student: ✨ It’s basically my laptop, sketchbook, planner, & notebook all in one I pair it with: — Apple Magic Keyboard (hello built-in trackpad & laptop vibes!) — Apple
Byaombe •••

Byaombe •••

8546 likes

the vibes i bring to the function
mags | booktok📖🦋🏹

mags | booktok📖🦋🏹

17 likes

🎮Try These Games When You are Free
🎮CodeCombat CodeCombat is an educational role-playing game that allows players to learn programming while having fun. In this game, players take on the role of a hero and progress through various levels by inputting code to control the hero's actions. It offers support for multiple progr
Capital&Crypto

Capital&Crypto

6 likes

A character sheet portrait of a smiling woman, a 52-year-old mom of seven, surrounded by elements reflecting her organized, resilient life. It highlights her journey balancing family, personal growth, and learning technology like Python, JavaScript, and SQL, with daily routines, goals, and affirmations for managing anxiety and ADHD.
🌹 From Python, JavaScript and SQL Books to Code Dreams: This Is Me
🌹 From Budget Books to Code Dreams: This Is Me This beautiful character-sheet portrait captures me being organized, resilient, and focused on growth. I am surrounded by planners, notebooks, coding references, roses, and inspirational reminders, I represent the perfect blend of determination and
mirandaperry05

mirandaperry05

1 like

Feeling hesitant about freelancing? I get it!
I remember those nerves about diving into something new. But trust me, the rewards are so worth pushing through the fear! If you're ready to take the leap but not sure where to start, here are some easy-to-start side hustles that will help you gain experience and build your portfolio: 👉🏻
Bria | Social Media Marketing

Bria | Social Media Marketing

28 likes

They Dared Me to Make a Game...
torque.test

torque.test

0 likes

Where tf the function?
Twitch: Midnightbre YT: brelawrence #fortnite #gaming #gamer #fyp #viral
Bre 🦋🤍

Bre 🦋🤍

41 likes

4 Popular Programming languages of 2024
1. JavaScript Prerequisites: A basic understanding of HTML and CSS is recommended for effective web development with JavaScript. Platforms: Web browsers, server-side environments with Node.js. Use Cases: - Creating interactive web elements - Building dynamic web applications - Developing sing
vedha | career tips (tech) 👩‍

vedha | career tips (tech) 👩‍

25 likes

AI taught me to code faster than any course.
💡 Tired of watching endless tutorials and still getting stuck? I used to spend hours Googling every coding error—until I found BootSelf, the AI coding mentor that actually teaches you like a human would. This isn’t a course. It’s a 24/7 AI mentor that: ✅ Explains code in real time ✅ Reviews a
Code with BootSelf AI

Code with BootSelf AI

9 likes

the vibes we bring to the function
#vibecheck #vibewithus #fitcheck #friends #fashionfriends
RockrBarbiJenni

RockrBarbiJenni

74 likes

Four smiling young women in yellow dresses pose together at a sorority date function with a "My Tie" theme. They are standing in a line at an indoor event with dim lighting, ready for a fun night out.
A young woman in an off-the-shoulder yellow dress takes a mirror selfie in her dorm room. Text overlays indicate the "My Tie" theme, a yellow, orange, and pink color palette, and an energy drink for the road.
Two smiling young women, one wearing a black tie and the other a white tie, pose closely for a selfie. Both are dressed in yellow/cream tops, embracing the "My Tie" theme at the sorority function.
come to a college sorority date function with me!
let’s recap my college sororities date function! This date function had the theme of “My Tie” which was both a play on words for Mai Tais (sunset colored drink) and an encouragement to wear your dates suit tie as an accessory. As someone who loves the color yellow, I was super happy that I got to w
presleigh green 🩵🩵

presleigh green 🩵🩵

15 likes

A young woman with long blonde hair looks upwards, with the text "Best AI Tutor for Coding" overlaid. The Lemon8 logo and username @code.with.bootsel are visible at the bottom left.
Best AI Tutor for Coding
I’ve tried YouTube tutorials, online courses, and coding bootcamps - but nothing felt personal. Every platform threw videos and notes at me… but when I got stuck, I was still alone. That changed the moment I discovered BootSelf, the best AI tutor app for coding in 2025. 🚀 This app doesn’t ju
Code with BootSelf AI

Code with BootSelf AI

1 like

The function >>>
Show: Summer House on Peacock🤗🤍
G 🧚‍♀️

G 🧚‍♀️

57 likes

The beauty about tech is there is no one path. 🌱 One of the biggest lessons throughout my journey has been that what felt like the hardest parts often involved “figuring out my next growth step”. The decision is sometimes harder than the execution. ⚙️ Overtime, I learned those phases of “going in
lindavivah

lindavivah

1 like

What Is JavaScript? (Beginner Friendly)
JavaScript is what turns a website from static to interactive. In Day 1 of this beginner-friendly JavaScript series, I explain: • what JavaScript is • why websites feel “alive” • how JavaScript works with HTML and CSS #learnjavascript #codingforbeginners #webdevelopment #codewit
Aysha

Aysha

10 likes

A young woman with long reddish-brown hair and bangs, wearing a black lace cardigan and dark floral skirt, stands with eyes closed in a sunny, grassy outdoor setting, embodying a whimsical vibe.
A woman with long reddish-brown hair, wearing a black lace cardigan and dark floral skirt, twirls playfully in a grassy outdoor setting, capturing a free-spirited and whimsical moment.
A smiling woman with long reddish-brown hair and bangs looks at the camera, wearing a black cardigan and dark floral skirt, against a natural, grassy hillside background.
The vibes I bring to the function
#appalachian #southerngothic #whimsigoth #Lemon8Diary #witchyvibes
ArasEliz

ArasEliz

8 likes

See more