Перейти до основного вмісту
← ⚡ JavaScript & the browser·Модуль C5 · Урок 5
ЗавданняWrite a JSDoc'd function clamp(value, lo, hi) that clamps value into [lo, hi]. Include @param and @returns tags. Call it with clamp(5, 0, 10), clamp(-3, 0, 10), clamp(99, 0, 10) and log the results.

JSDoc @param and @returns: IDE hints without TS

150 XP10 min
Теорія

What JSDoc gives you

A JSDoc block comment above a function makes editors treat it almost like TypeScript:

  • hover the function name → see params and return type
  • call it with wrong types → red squiggle in VS Code
  • arg auto-complete shows expected shapes

No tsconfig. No build step. Just comments your editor already understands.

/**
 * Format a price with currency.
 * @param {number} cents - price in minor units
 * @param {string} [currency="USD"] - ISO currency code
 * @returns {string} formatted price like "$12.34"
 */
function formatPrice(cents, currency = "USD") {
  return new Intl.NumberFormat("en", {
    style: "currency",
    currency,
  }).format(cents / 100);
}

Key tags

  • @param {type} name - description — one per parameter
  • @param {type} [name=default] - description — optional param
  • @returns {type} description — return value
  • @throws {Error} description — what errors can fire

When this is enough

90% of small/medium JS projects need exactly this. The other 10% (large teams, complex generics) reach for TypeScript. Lesson C5-09 has the honest "when to use TS" call.

🔒

Зареєструйся щоб кодувати

Теорія відкрита всім. Інтерактивний редактор, живий перегляд і перевірка відкриваються з 7-денним безкоштовним trial — потрібна картка, скасування у будь-який час.

Реєстрація — безкоштовний trial →

Перші 10 уроків у кожному треку — безкоштовні. Картка не потрібна.

← НазадНаступний урок →

Отримуй одну пораду з Python чи вебу — щодня поштою

Коротко, без води, без спаму. Відписка — в один клік.