eldan88 Posted November 16, 2013 Share Posted November 16, 2013 Hey guys. I come from a PHP programming background. In php, to create arrays, its simple as doing the following $array = array1("dog", cat); echo $array[0]; But for java its not that simple. My first question is what is the difference between a literal array, condensed array, and regular array. My second question is how can i display a value of an array using the example below? For example how do I display dog in the browser? <script> var array1 = ["dog", "cat"]; array1[0]; </script> Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/283951-arrays/ Share on other sites More sharing options...
.josh Posted November 16, 2013 Share Posted November 16, 2013 See this SO answer they explain it a lot better. At least read the top 2 answers, though there's good info in a lot of the rest. As far as displaying the value.. well that depends on what you mean by "display". This will output the value of the first element to the javascript console: console.log(array1[0]); This will give you a popup alert: alert(array1[0]); Quote Link to comment https://forums.phpfreaks.com/topic/283951-arrays/#findComment-1458486 Share on other sites More sharing options...
dalecosp Posted November 16, 2013 Share Posted November 16, 2013 (edited) You'd do well to get a book or some web tutorials on Java*script*. Remember this is NOT JAVA. An array in Javascript is just as simple as PHP, or perhaps moreso, and can be initialized like this: myArray = ['dog','cat']; Putting it "in the browser", though, is a whole 'nother can of worms known as "manipulating the DOM" (Document Object Model).One of Javascript's biggest proponents calls it (the DOM) nothing less than the "worst API ever invented."Here's one way it could be done, but I would never recommend it for general use: myArray = ['dog','cat']; body = document.getElementsByTagName("body"); body[0].innerHTML = myArray[0]; Edited November 16, 2013 by dalecosp Quote Link to comment https://forums.phpfreaks.com/topic/283951-arrays/#findComment-1458487 Share on other sites More sharing options...
.josh Posted November 16, 2013 Share Posted November 16, 2013 meh.. DOM isn't so bad.. it's the inconsistency between browsers that kills you. Quote Link to comment https://forums.phpfreaks.com/topic/283951-arrays/#findComment-1458489 Share on other sites More sharing options...
eldan88 Posted November 16, 2013 Author Share Posted November 16, 2013 See this SO answer they explain it a lot better. At least read the top 2 answers, though there's good info in a lot of the rest. As far as displaying the value.. well that depends on what you mean by "display". This will output the value of the first element to the javascript console: console.log(array1[0]); This will give you a popup alert: alert(array1[0]); .josh. I mean just echoing it out... I guess javascript doesn't work like that does it? It needs to be triggered by an event? Quote Link to comment https://forums.phpfreaks.com/topic/283951-arrays/#findComment-1458494 Share on other sites More sharing options...
eldan88 Posted November 16, 2013 Author Share Posted November 16, 2013 dalecosp. Why are you saying its the worst API ever implemented? Quote Link to comment https://forums.phpfreaks.com/topic/283951-arrays/#findComment-1458495 Share on other sites More sharing options...
dalecosp Posted November 16, 2013 Share Posted November 16, 2013 The API is the browser, and the quote is from Douglas Crockford, a renowned speaker on Javascript.Start here: and you'll learn quite a bit about Javascript. There are 3 parts to this original teaching, and he's gone on from there to "world famous" on the subject. Quote Link to comment https://forums.phpfreaks.com/topic/283951-arrays/#findComment-1458500 Share on other sites More sharing options...
Solution dalecosp Posted November 16, 2013 Solution Share Posted November 16, 2013 .josh. I mean just echoing it out... I guess javascript doesn't work like that does it? It needs to be triggered by an event? Well, yes, triggered by something, typically an event. Javascript is the internal language of the browser, and the browser is displaying what's given to it by the server. In the context of the loaded "page", it will then execute. So you use Javascript most often to manipulate the document (page) when it's already loaded in the browser window, which means you're manipulating the browser's view of the code (the DOM) after it's already been parsed and rendered, more or less. Quote Link to comment https://forums.phpfreaks.com/topic/283951-arrays/#findComment-1458501 Share on other sites More sharing options...
eldan88 Posted November 16, 2013 Author Share Posted November 16, 2013 Okay got it, and thanks for showing me the youtube video. Quote Link to comment https://forums.phpfreaks.com/topic/283951-arrays/#findComment-1458502 Share on other sites More sharing options...
eldan88 Posted November 16, 2013 Author Share Posted November 16, 2013 Okay got it, and thanks for showing me the youtube video. Quote Link to comment https://forums.phpfreaks.com/topic/283951-arrays/#findComment-1458503 Share on other sites More sharing options...
eldan88 Posted November 16, 2013 Author Share Posted November 16, 2013 Video Looks very interesting. I am going to watch the whole thing tomorrow!! Quote Link to comment https://forums.phpfreaks.com/topic/283951-arrays/#findComment-1458505 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.