user31782
user31782

Reputation: 7587

Can local javascript variables be read by user console?

I am trying to create an online lottery system in which user has to guess a number between 1 to 10. If the number matches with the javascript generate random number he will win the lottery. The method I used is:

!function(){
  var x = Math.floor((Math.random() * 10));
  var btn = document.getElementById("btn");
  btn.addEventListener("click", function(){
    var user_input = document.getElementById("text").value;
    if( user_input == x) {
      alert("Won Lottery", user_input, x);
    }
    else {
      alert(user_input + x + "sorry");
    }
  });
}();
<h2>Guess a number between 1 and 10 </h2>  
<input id="text" type="text">
<button id="btn" type="submit">Submit</button>

My question is, can a hacker open the js console and somehow find the value of local x variable?

Upvotes: 0

Views: 178

Answers (1)

jbell
jbell

Reputation: 141

enter image description here

At this point in the code execution, x is within scope. You can see in the right column that x = 0 so it's just a case of changing user_input to 0 and then finishing the execution.

Upvotes: 2

Related Questions