Jump to content
Sign in to follow this  
j.silver

JS Basic Question

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.


Share this post


Link to post
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.

Share this post


Link to post
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>

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...