---
title: "How to create an arrow function in JavaScript?"  
description: "How to create an arrow function in JavaScript?"  
author: "Utpal Vishwas"  
published: 2023-10-08  
updated: 2023-10-09  
canonical: https://www.mindstick.com/forum/160057/how-to-create-an-arrow-function-in-javascript  
category: "javascript"  
tags: ["javascript", "arrow function"]  
reading_time: 2 minutes  

---

# How to create an arrow function in JavaScript?

How to create an [arrow function](https://www.mindstick.com/forum/160054/how-to-pass-parameter-values-in-javascript-arrow-function) in [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript)?

## Replies

### Reply by Gulshan Negi

Well, here is a simple example of how to create an arrow function in JS.

> // Arrow function with no parameters\
> const greet = () => {\
> return 'Hello!';\
> };
>
> // Arrow function with one parameter\
> const square = x => {\
> return x * x;\
> };
>
> // Arrow function with multiple parameters\
> const add = (a, b) => {\
> return a + b;\
> };

Thanks

### Reply by Aryan Kumar

Creating an [arrow](https://answers.mindstick.com/qa/95602/which-country-test-flighted-arrow-3-anti-ballistic-missile-system-and-its-interceptors) [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) that returns a random number between 1 and 100 involves using the **Math.random()** method, which generates a random floating-point number between 0 (inclusive) and 1 (exclusive). You can then multiply this value by 100 to get a random number between 0 and 99, and finally, add 1 to shift the range to 1-100. Here's the explanation followed by the code:

## Explanation:

- **Math.random()**: This method generates a random floating-point number between 0 (inclusive) and 1 (exclusive).
- **Math.random() * 100**: Multiply the random value by 100 to get a random floating-point number between 0 (inclusive) and 99 (exclusive).
- **Math.floor(Math.random() * 100)**: Use **Math.floor()** to round down the random value to the nearest integer, resulting in a random integer between 0 and 99.
- **Math.floor(Math.random() * 100) + 1**: Finally, add 1 to the result to shift the range to 1-100.

Now, let's write the code:

```plaintext
const getRandomNumber = () => Math.floor(Math.random() * 100) + 1;

// Usage:
const randomNum = getRandomNumber();
console.log(randomNum); // This will print a random number between 1 and 100.
```

In this code:

- We define an arrow function called **getRandomNumber**.
- Inside the function, we use the **Math.random()** method to generate a random floating-point number between 0 and 1, and then we apply the necessary calculations to obtain a random integer between 1 and 100.
- We call the **getRandomNumber** function to get a random number and store it in the **randomNum** variable.
- Finally, we log the random number to the console.


---

Original Source: https://www.mindstick.com/forum/160057/how-to-create-an-arrow-function-in-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
