Árlega Greiddu árlega og fáðu 2 mánuði ókeypis á öllum greiddum áskriftum. Skoða áskriftir
Skrá inn Byrjaðu ókeypis

Kóðaþýðandi

Inntak
Kóði
function add(a, b) { return a + b; }
Dæmi um úttak Gert 27 október 2026

export function add(a: number, b: number): number { return a + b; }

Notes:

  • The TypeScript version explicitly accepts and returns number.
  • Unlike the JavaScript original, calls with strings or other non-number values are rejected at compile time.
  • TypeScript types are erased at runtime, so JavaScript callers can still pass incompatible values unless runtime validation is added.
  • number includes floating-point values, NaN, and Infinity.
  • JavaScript’s usual numeric behavior still applies, including floating-point precision issues and coercion at runtime.
Nota þetta Kóðaþýðandi Eyðublaðið opnast með inntaki þessa dæmis; breyttu hverju sem er.