Menu

Functions

“Packaging a piece of logic so you can reuse it.”

View .md

A function takes inputs, does some work and usually returns a result.

Defining a function#

math.tsts
1export function add(a: number, b: number): number {
2  return a + b;
3}
4
5add(2, 3); // 5

Pure functions#

A function is pure when the same inputs always give the same output and it changes nothing outside itself. Pure functions are easy to test.

Tip

When a function is hard to test, it’s often doing two things. Try splitting it.

Byte check

0/2

Answer these 2 questions to commit this byte to your learning path.

  1. When is a function pure?
  2. A function is hard to test. What’s a common reason?
Search posts and bytes

Optional account

Keep your progress everywhere

Everything on AminBytes is free to read, with or without an account. Sign in only if you want your commits, streak and merged topics saved, on every device.

or

We keep only your name, avatar and which bytes you finished, and you can delete it all anytime. Privacy