+ 1
Need help on variables in javascript
anytime i write a code with javascript under the scripts tag in html, it always shows me that x is undefined. code is below <script> var x=abc; document.write(x); </script>
3 Respostas
+ 6
In my opinion, it should give you error abc is not defined. You are using abc as a variable which you have not defined.
I think you want to achieve this,
var x = 'abc';
Now you are assigning a string value to x. And will work fine.
+ 1
Doesn't have to be within a pair of single quotes, it can also be within a pair of double quotes. i.e. var x = "abc";
+ 1
Now console suggest that your abc is variable, and it is looking where abc is defined and set that value to your var x. You need to set var x = "abc"; if you want to alert the string.
In this case you can set var abc = "abc"; var x = abc; And check the resoult ;)