Jump to content

JS Basic Question


j.silver

Recommended Posts

<body>

hello all

<script>

document.write(5+7);

document.write(4);

</script>

</body>


Above basic js code outputs: hello all 124


There is no concatenation between "hello all" and "12", yet space is there. I'd appreciate an explanation.


Link to comment
Share on other sites

The line break between "hello all" and the opening <script> tag is rendered as a space. In HTML spaces, tabs and line breaks render as a single space.

 

Content between script tags is not rendered.

Link to comment
Share on other sites

If that was lesson 1 then lesson 2 should be to avoid using document.write(). Instead learn to use innerHTML.

<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>title</title>
</head>
<body>

hello all
<div id="div1">
</div>

<script>
   var str = '';
   var ele = document.getElementById('div1');
   str += (5+7);
   str += (4);
   ele.innerHTML = str;
</script>
</body>
</html>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...