+ 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>

21st Apr 2017, 1:13 PM
Olurode Mus'ab
Olurode Mus'ab - avatar
3 Answers
+ 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.
21st Apr 2017, 1:19 PM
Ashwani Kumar
Ashwani Kumar - avatar
+ 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";
21st Apr 2017, 1:36 PM
Daniella
Daniella - avatar
+ 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 ;)
21st Apr 2017, 2:14 PM
Milan