Problem with variables and document.write in JavaScript

Hello everyone, I created variables and I wanted to display data from variables with document.write. The problem is when I run the code with one of the browsers it doesn’t show me anything. I am a beginner in JavaScript and every tip and suggestion will mean a lot to me. Here is my code

<!Doctype html>

<html>

<head>

<meta charset="UTF-8">

</head>

<body>

<script type="text/javascript">

var x=r;

var y=1510;

window.document.write(x);

document.write("<hr>");

document.write(y);

</script>

</body>

</html>

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

r is undefined. If you want to write the letter “r” it should be in quotes var x = "r";

Anyway, I would not suggest using document.write.

Look into DOM manipulation and all the different ways of getting elements, creating elements, and updating elements.

1 Like

Hi @sajo

You should create a name variable that makes sense, as well strings with quotes with a specific name like:

var myString = "myLetterString";  // with quotes around to identify a string letter.
window.document.write(myString); // output  --> myLetterString

I suggest you try to do console.log() to see any output. Also like @JeremyLT it’s better not to write document.write might run into a remove existing strings. and better to do DOM manipulation.

1 Like

Thanks guys you help me a lot. :slight_smile: