admin管理员组文章数量:1414908
My background is in java and right now Im getting into learning Javascript. (I'm porting this java code that I made into javascript but it behaves a bit different than I expected?? If I use "console.log()" instead of "document.write" I'm NOT getting the same result..why?
thanks 4 yr time! Some insight will be very appreciated!
var counter = 1;
function println(str) {
console.log(str);//
}
for (var i = 1; i <= 5; i++) {
for (var j = i; j < 5; j++) {
document.write("#");
// println("#");
}
for (var k = 1; k <= counter; k++) {
document.write("*");
// println("*");
}
document.write("<br/>");
//println(" "); <--- "\n" ?
counter+= 2; //
} // ends application
My background is in java and right now Im getting into learning Javascript. (I'm porting this java code that I made into javascript but it behaves a bit different than I expected?? If I use "console.log()" instead of "document.write" I'm NOT getting the same result..why?
thanks 4 yr time! Some insight will be very appreciated!
var counter = 1;
function println(str) {
console.log(str);//
}
for (var i = 1; i <= 5; i++) {
for (var j = i; j < 5; j++) {
document.write("#");
// println("#");
}
for (var k = 1; k <= counter; k++) {
document.write("*");
// println("*");
}
document.write("<br/>");
//println(" "); <--- "\n" ?
counter+= 2; //
} // ends application
Share
Improve this question
asked May 9, 2012 at 18:14
YoniGeekYoniGeek
4,0937 gold badges28 silver badges32 bronze badges
4
- 1 Have you checked the documentation for document.write and console.log? They're unrelated. – Madbreaks Commented May 9, 2012 at 18:15
- 1 If you want to learn JavaScript, forget ANYTHING related to Java. They are two pletely different languages. Stop this "how to make language X to work like language Y". – tereško Commented May 9, 2012 at 18:20
- "Not the same result" means what? – epascarello Commented May 9, 2012 at 19:00
- @epascarello well I found out that u dont have system.out.print in js..so u have to add everything in the same string. like string+= and then at the end of the first loop u make the println with console.log.... – YoniGeek Commented May 9, 2012 at 19:37
2 Answers
Reset to default 0document.write()
is for printing content to the page of your document, while console.log()
is used predominantly for diagnostic/debug information to be emitted in the console of your web browser. Specifically, document.write()
is intended to be consumed by the viewer of your page, while console.log()
is generally not.
Console.log logs stuff into browser console. Install Firebug (getfirebug.) and you will see your logs.
Also there is nice description about how it works http://getfirebug./logging.
Also, using document.write is not really elegant, you can use it only on page load, and it's blocking whole page. You basically should not use it at all. If you try to use document.write after page is loaded, it will replace whole content of your document with your last "log".
本文标签: javascripthow does quotSystemoutprintlnquot work in JS )Stack Overflow
版权声明:本文标题:javascript - how does "System.out.println" work in JS?... :) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745168077a2645804.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论