declare

Create an array and populate it with values in JavaScript



Sharing buttons:

there are many ways to define an array

for example I can define my array that

I'll name empty simply by putting in

square brackets and now I have an empty

array that I can add items to of course

that does change the meaning of the name

of the variable empty but nonetheless I

can say for example empty offset 0

equals 1 and empty offset 1 equals 2 and

now empty is not so empty and if I say

alert empty and we run that application

we get 1 comma 2 the contents of the

formerly empty array another way to

define an array is to define the members

when you create it and so I can fill in

my Prime's array debating whether 1 is a

prime and then I can at any time extract

from my array using offset notation so

the third prime is going to be at offset

and I'm going to leave that as a

question of our moment and the way you

figure that out is 1 2 3 is the third

prime and that's at offset zero 1 to

remember that the third prime will be at

3 minus 1 because we have 0 offset and

we can see that as well let's comment

out the earlier alert and run this

program and there's our third prime

arrays do not need to have uniform types

in them so I can create an array and set

it to have the value 1 and hello and

false

and that's a perfectly valid array and I

can extract any of those values using

the offset so here I'm going to extract

the final value and set that as truth

put in an alert and let's run the

application and sure enough it comes up

false because that was the third member

of my array at offset to remember that

the length of an array tells you how

many members are in the array not the

highest index of the array here I've set

my array length to my array dot length

getting the length property from the

array and I've put that value into an

alert and sure enough it comes up and

says 3 which is the length of that array

you can add two arrays after they're

created and let's create a small

function that uses that capability

we'll call our function range it will

take one parameter which is the max

that's the number that we want the range

to go up to and in the function we're

going to have a return value which is an

empty array will then add a for loop to

populate that array let's take a quick

look at the for loop it's going to

create a counter called I which it will

initialize to 0 you will count until I

is less while I is less than max

incrementing I each time through the for

loop and it will set the eighth offset

so first 0 then 1 then 2 equal to I

times 2 it will then return that value

notice by the way that the for loop

uses the counter variable I using I and

then J and then K as counter variables

is an old programmer tradition going all

the way back to Fortran where those were

the required identifiers for counters

and for small loops it is very much the

tradition to continue to use I and if

you have a nested for loop then J and K

as your counter variables this is very

much akin to why the roads in Boston are

so twisty they were built on old cow

paths old traditions die hard we can see

the result of this method by calling

alert and passing in range and giving it

a value let's say five for its maximum

and then let's run this application and

sure enough we get five values