How to make rock paper scissors in javascript?

AffiliatePal is reader-supported. When you buy through links on our site, we may earn an affiliate commission.



Rock Paper Scissors is a classic hand game that many people enjoy playing to make quick decisions or settle disputes. In this article, we will explore how to create a Rock Paper Scissors game using JavaScript. By the end of this tutorial, you will have a fully functional game that can be played in any web browser.

Setting up the HTML Structure

To begin, we need to create the basic HTML structure for our game. Open your preferred text editor and create a new HTML file. Inside the file, add the following code:


Rock Paper Scissors Game

Rock Paper Scissors


In the code above, we have a simple HTML structure with three buttons representing the choices of Rock, Paper, and Scissors. We also have a div with the id “result” where we will display the outcome of the game.

Implementing the JavaScript Logic

Now that we have the HTML structure in place, let’s move on to implementing the JavaScript logic for our game. Add the following script tag just before the closing `` tag:



In the JavaScript code above, we first select all the buttons using `document.querySelectorAll(‘button’)` and store them in the `buttons` variable. We also select the result div using `document.getElementById(‘result’)` and store it in the `resultDiv` variable.

Next, we add an event listener to each button using the `forEach` method. When a button is clicked, the `playGame` function is called. Inside the `playGame` function, we get the player’s selection from the `id` of the clicked button and generate a random computer selection using the `computerPlay` function. We then determine the result of the game using the `getResult` function and display it in the result div.

The `computerPlay` function generates a random selection by choosing a random index from the `choices` array.

The `getResult` function compares the player’s selection with the computer’s selection and returns the outcome of the game.

Testing the Game

Save the HTML file and open it in your web browser. You should now see the Rock Paper Scissors game interface. Click on any of the buttons to make your selection and see the outcome displayed in the result div.

You can play the game multiple times by clicking on different buttons.


In this article, we have learned how to create a Rock Paper Scissors game using JavaScript. We started by setting up the HTML structure and then implemented the JavaScript logic to handle the game’s functionality. By following the steps outlined in this tutorial, you now have a fully functional Rock Paper Scissors game that can be played in any web browser.


– MDN Web Docs: [querySelectorAll](
– MDN Web Docs: [getElementById](
– MDN Web Docs: [addEventListener](
– MDN Web Docs: [Math.random](