Menu
Step 2 of 2
Functions
“Packaging a piece of logic so you can reuse it.”
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.
Answer these 2 questions to commit this byte to your learning path.
Reset. This byte isn't committed any more. Answer the questions again to commit it.
$ git commit -m "learn(basics): Functions"
[main f9a7307] learn(basics): Functions
1 byte changed, 2 insertions(+)
See it on your path Already committed . Try the questions again any time, it won't undo anything. To uncommit it and start over: