JavaScript Lesson 4: Functions and Arrow Functions

JavaScript Lesson 4: Functions and Arrow Functions featured illustration

πŸ“Ž This article includes 2 downloadable practice files ↓

⏱ 1 min read

πŸ“˜ JavaScript Beginner Course Β· Lesson 4 of 8

Advertisement
In this article
  1. Functions are values
  2. Practice
  3. Continue learning
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.

Advertisement

πŸ“Ž Practice files for this article

⬇ Download all 2 files (ZIP Β· 481 B)

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