How can I change the values of variables in a website's JavaScript code?
I want to know because I'm building a website at the moment and, as part of the code, I have a variable whose value has important consequences for the functionality of the website. Hence my desire to know how someone can change this value (even if it only affects the website as it appears to that user).
I am particularly interested in learning how to do this in Google Chrome.
How can I change the values of variables in a website's JavaScript code?
I want to know because I'm building a website at the moment and, as part of the code, I have a variable whose value has important consequences for the functionality of the website. Hence my desire to know how someone can change this value (even if it only affects the website as it appears to that user).
I am particularly interested in learning how to do this in Google Chrome.
Share Improve this question edited Aug 5, 2016 at 22:34 user5508297 asked Aug 5, 2016 at 22:19 user5508297user5508297 8853 gold badges11 silver badges30 bronze badges 10- 1 is it your own website that you are trying to change? if not, it's unlikely that changing things in your browser will affect anyone but yourself; if you are hoping to find someone to explain how to hack a website here, you are on the wrong site. – Claies Commented Aug 5, 2016 at 22:26
- @Claies I want to learn how to change the values of variables in the JavaScript code of my own side so I can build more robust code. – user5508297 Commented Aug 5, 2016 at 22:28
- 1 Sorry, I've been unclear. I want to know because I'm building a website at the moment and, as part of the code, I have a variable whose value has important consequences for the functionality of the website. Hence my desire to know how someone can change this value (even if it only affects the website as it appears to that user). @Claies – user5508297 Commented Aug 5, 2016 at 22:32
- 2 you can't stop users from using the browser tools on their pc, so if you believe this value has important consequences, you should ensure that it is thoroughly validated on the server. If this isn't enough security, and changing the variable is a problem for your application, then perhaps you should consider something other than a web application for this sensitive information. – Claies Commented Aug 5, 2016 at 22:36
- 2 knowing how they could change the value won't give you any power to stop it from happening. – Claies Commented Aug 5, 2016 at 22:37
1 Answer
Reset to default 6If you hit F12 in chrome and click on "Console" at the top, you have a fully functional JavaScript interpreter living in the scope of the website you're looking at. This means you can enter pretty much any code in the console and execute it as javascript code in the global scope of that website. Global variables in <script>
tags are exposed in this scope, so if you have (for example) in your page:
<script>
var x = {'a': 5};
</script>
You can type in x.a = 6
in the console, press enter, and the variable will indeed be changed.
You can even use the console to change the values of builtins, like Array.map
, though this is not remended as it can change or break the functionality of website code.