Changing variable types


Build "Rock, Paper, Scissors"

The code works but I'm coming from more of a Java & C# background and noticed the variable type changes unless I'm mistaken.

In the code below, 'computerChoice' starts as a float but after the if statement, the value is now a string. Is 'computerChoice' really changing or is there something I'm not aware of in javascript? Assuming I'm right, doesn't this cause problems at times? Do devs just have to be careful about what they are doing with variables as they create larger projects?


var computerChoice = Math.random(); //here is a float
if (computerChoice < 0.34) {
	computerChoice = "rock"; //here is a string
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";


simply answer yes. :slight_smile:

you can check the type
after your code just add

console.log(typeof computerChoice)


Always appreciate the simple, direct answer. :slight_smile:



This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.