Facebook – https://www.facebook.com/TheNewBoston-464114846956315/
GitHub – https://github.com/buckyroberts
Google+ – https://plus.google.com/+BuckyRoberts
LinkedIn – https://www.linkedin.com/in/buckyroberts
reddit – https://www.reddit.com/r/thenewboston/
Support – https://www.patreon.com/thenewboston
thenewboston – https://thenewboston.com/
Twitter – https://twitter.com/bucky_roberts
Original source
43 responses to “Beginner JavaScript Tutorial – 34 – Cool Technique to Print Array Elements”
Thanks
Wait, is this Bucky from "Bucky's C++ tutorails"?
Wow, Bucky, as always, comes to the rescue 😀
Here is an even better way to do this:
<!doctype html>
<html>
<head>
<title>For-Each Loop</title>
</head>
<body>
<script type="application/javascript">
var stuff = ["apples", "pears", "bacon", "tuna", "ham"].sort();
stuff.forEach(
function(element) {
window.document.write(element + "<br />");
}
);
</script>
</body>
</html>
thank you!
my favourite tutorial
Bucky, finished your Html & Css tutorials, 3 days back. Now trying to friend-zone javascript. Thanks Bucky
2017
why is it so easy? it looks like Java and C/C++
When he wrote : *BACON*, I predicted tuna and ham LOL!
so how can we take from user from prompt if we use this technique ?
one simply does not erase bacon from the list.
Ever.
<script type="text/javascript">
var door=new Array("steel","wood","chilli","dip");
door.sort();
for(i=0;i<door.length;i++){
document.write(door[i]);
}
</script>
Very useful! thanks for the tutorials Bucky 🙂 they're a great help! I've done your java ones, now I'm busy with these and they're actually helping me understand java better due to the in depth explanation of certain things. Thanks again!
Why did you use for loop when you could directly print it out using document.write?
Thanks a lot 😀
Wouldn't it be easier to convert it to a string?
Single Line Solution:
for (x in stuff) {document.write(stuff[x]+"<br>")}
Thanks for this 🙂
stuff = new Array();
stuff = ("THIS", "IS", "BETTER");
for (x in stuff) {document.write[x]+"<br />";}
Thank you for the tutorial.
GOD bless you.
check this out…..
<script type="text/javascript">
var bp=new Array(3);
for(i=0;bp.length;i++){
bp[i]=prompt("enter your data","");
}
document.write(bp[i]+"</br>");
</script>
you can just put document.write(stuff);
the i is not needed and it still prints five times.
Your tutorials are educational + entertaining(funny) 🙂
I dont get it..
y are we using "loop" here?
when we can sort it and print it without that loop.
and what difference does it makes? we aint looping here anything?
actually not cool !!
You can use a console.log(); instead of a document.write();
Or you could just do it like this==> document.write(stuff.join("<br/>"));
How do we pass multiple arrays to another function? i.e I have one function for input and have another to output
Mr. Roberts, you provide a valuable service. Thank you very much.
some tuna (bacon) company is paying bucky for using "tuna" ("bacon") in every tutorial… last edited (27.3.2014) By the way Bucky is lost, no new videos…:(
Thanks for the tutorial !
this tutorial is making me hungry….. but i just had breakfast 😀
they were his ex's 😛
But using stuff.length thingy would take an ignorant little more time to load perhaps ..
Now there are 3 dislikes. Thanks for reminding the haters
not even single dislike thumbs up
br is break but html tags dont work
isn't document.write(stuff.join("br")); faster?
Is the letter "i" a preset property or can it be swapped out for a value of ones choosing?
Finally no idiots that dislike Bucky's videos
I wrote "Yes" with a capital "Y" 😛 I suck
You are so predictable ;D
"Let's just put 5 random words in this array:"
Me: probably apples
Bucky: "apples"
Me: yeah! Now what? :S
Bucky: "pears"
Me: "ah okay, now bacon"
Bucky: "bacon…"
Me: "Hmm, tuna?"
Bucky: "tuna…"
Me: "Last but not least: ham"
Bucky: "and ham."
You never disappoint me!
Make sure you spell that wrong!!! XD!!