mirror of
https://github.com/alrayyes/wiki.git
synced 2024-11-29 14:36:22 +00:00
40 lines
740 B
Markdown
40 lines
740 B
Markdown
---
|
|
id: 4851138e-ddbd-4801-8178-51d990e327e5
|
|
title: JavaScript arrow functions
|
|
---
|
|
|
|
# Introduction
|
|
|
|
``` javascript
|
|
const power = (base, exponent) => {
|
|
let result = 1;
|
|
for (let count = 0; count < exponent; count++) {
|
|
result *= base;
|
|
}
|
|
return result;
|
|
};
|
|
|
|
console.log(power(2, 3))
|
|
```
|
|
|
|
If there is only one parameter name, parentheses can be omitted around
|
|
the parameter list. Same for the function body and brackets
|
|
|
|
``` javascript
|
|
const square1 = (x) => { return x * x; };
|
|
const square2 = x => x * x;
|
|
|
|
console.log(square1(5))
|
|
console.log(square1(6))
|
|
```
|
|
|
|
When an arrow function has no parameters at all, its parameter is just
|
|
an empty set of parentheses
|
|
|
|
``` javascript
|
|
const horn = () => {
|
|
console.log("Toot");
|
|
};
|
|
|
|
horn()
|
|
```
|