How to get random variables in JavaScript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to get random variables in JavaScript?

Suppose I make two variables and when I log it in Console then it should have one random variables out of those two Please Help

7th Apr 2023, 3:06 PM
ɑყυꜱɦ
ɑყυꜱɦ - avatar
3 Answers
+ 10
ɑყυꜱɦ you can use Math.random() function
7th Apr 2023, 3:13 PM
Sakshi
Sakshi - avatar
+ 5
As Sakshi said, use Math.random(); To solve your problem of "picking between two variables.", We can use partial probability of Math.random() Math.random() --> always gives 0.something We can do something like this if(Math.random() > 0.5) Use variable 1 else Use variable 2
7th Apr 2023, 3:25 PM
Morpheus
Morpheus - avatar
+ 4
Morpheus thanks for explaining it out
7th Apr 2023, 3:28 PM
ɑყυꜱɦ
ɑყυꜱɦ - avatar