Vue.js Generate random number


To generate random number in vue.js we can use native JavaScript Math.random() function to generate random number. Here in this tutorial, we are going to explain how you can use Math.random() function in vuejs. You can also use our online editor to edit and run the code online.


Vue.js Generate random number JavaScript- VueJS Example

You can generate random number in vue.js simply as below-

Example:

<div id="app">	
	<p>Random Number = {{randomNumber}}</p>
	<button @click="myFunction()">Click Me</button>
</div>
<script>
 new Vue({
el: '#app',
  
  data: { 
	  randomNumber:''
  },
  
   methods:{
    myFunction: function () {		
     this.randomNumber = Math.random()*100; //multiply to generate random number between 0, 100
    }   
   }

});
</script>   

Try it »

Output of above example-

Vue.js Generate random number JavaScript- VueJS Example

Advertisements

Add Comment

📖 Read More