JavaScript Lesson 4: Functions and Arrow Functions

📎 This article includes 2 downloadable practice files ↓

⏱ 1 min read

📘 JavaScript Beginner Course · Lesson 4 of 8

“`javascript
function gst(amount, rate = 0.18) {
return Math.round(amount * rate * 100) / 100;
}
const fyLabel = (y, m) => {
const s = m < 4 ? y - 1 : y; return `FY ${s}-${String(s + 1).slice(2)}`; }; gst(1000); // 180 fyLabel(2027, 2); // "FY 2026-27" ```

Arrow functions ((a, b) => …) are shorter and common when passing functions to other functions, as you’ll see with arrays next lesson. A one-line arrow can skip braces and return: const double = x => x * 2;

Functions are values

You can store them in variables, pass them as arguments (button.addEventListener("click", fn)) and return them, which is what makes array methods and event handling work.

💡 Name functions after what they return or do: gst(), fyLabel(), formatRupees(). Good names make code read like sentences.

Practice

Run the script and write a function that returns the EMI-free total for qty and rate.

📎 Practice files for this article

Free to use for learning. Files with macros (.bas) are plain text — import them with Alt+F11 → File → Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free · AI can be wrong