<script type='text/javascript'>
//<![CDATA[
for(var j = 0; j < 6;) {
document.writeln(++j);
}
//]]>
</script>
in the above code it suppose to give a new line if referring to the definition of the The writeln() method is identical to the write() method, with the addition of writing a newline character after each statement.
But in my case the output is 123456
UPDATE:
I will find the best answer to accept but with all the answers are correct and I believe next time someone will have the same question, so I thought I should just update with the answer to the top and remove the homework tag so this will benefit everyone.
The reason that is not producing the new line is that because it is actually producing the ‘\n’
The ‘\n’ to the browser if I’m not wrong it doesn’t affect how to display to the screen but if you look at the source-code of the page you will see that the output of mine would display something like this
1
2
3
4
5
6
But if we want it to display it to the page we would have to add the <br\> tag so it will produce a break-line so we will get the same output that we want.
<script type='text/javascript'>
//<![CDATA[
for(var j = 0; j < 6;) {
document.writeln(++j);
document.writeln("<br>");
}
//]]>
</script>
No, it is working correctly. The issue here is that you’re outputting it on different lines but it will not be displayed that way in the browser.
To view your actual result use “View Source” or look at the HTML in the developer tools in your browser.
If you want this to display on different lines throw in some HTML like the following.
Or use a
PRE(aka preformatted) tag (see W3 spec info on PRE here), which will preserve line breaks in the underlying source.