declare

JavaScript Arrays



Sharing buttons:

sometimes in your applications you might

be dealing with a list of objects for

example the list of products in a

shopping cart or the list of colors the

user has selected in situations like

that you use an array to store that list

let me show you how so here I'm gonna

declare another variable called selected

colors note that I'm using a meaningful

name I don't have SC or some other weird

name selected colors now we can

initialize this and set it to an empty

array so these square brackets or what

we call array literal and they indicate

an empty array now we can initialize

this array and add a couple of items

like red and blue let's Lock this on the

console so console the long selected

colors save the changes so here's our

array with two elements we can expand

that note that each element has an index

and that determines the position of that

element in the array so the index of the

first element is zero and the index of

the second element is one so if you want

to access an element in an array we use

this index here's how for example let's

say you want to display the first

element in this array you use the square

brackets and then specify the index save

the changes and now we have red now

earlier I told you that JavaScript is a

dynamic language so the type of

variables can change at runtime the same

principle applies to our arrays so the

lengths of arrays as well as the type of

objects we have in an array are dynamic

they can change so aligned - we

initialize this array with two elements

right now on line 3 we can add another

element to this array so the array will

expand so let's say selected colors of 2

that means the third item in this array

is going to be green

now let's display this array on the

console

so we have an array with three elements

so the length is dynamic it can change

also the type of objects we have in this

array is dynamic so unlike other

programming languages where every item

or every object in the array should have

the same type in JavaScript we can store

different types in an array so we can

make the last element a number save the

changes now we have two strings and a

number so the objects in the array as

well as the size of the array are

dynamic now technically an array is an

object so just like the personal object

we defined in the last lecture it has a

bunch of key value pairs or properties

that we can access using the dot

notation let me prove that to you so

here on the console let's look at the

type of selected colors so the type of

this array is an object so an array is

an object in JavaScript so here on line

4 we can look at the properties of this

array or this object using the dot

notation look these are all the

properties defined in arrays in

JavaScript so every time we declare an

array using square brackets that array

will automatically receive these

properties we didn't explicitly define

them they're just somehow magically

inherited from somewhere else we're

going to learn about that later when we

talk about prototypes now in this

lecture we're going to look at one of

these properties that is the length

property this property returns the

number of items or elements in an array

so save the changes you can see we have

three elements in this array now later

in the course we have a comprehensive

section about arrays you'll learn about

all kinds of operations we can perform

on arrays for now all I want you to take

away is that an array is a data

structure that we use to represent a

list of items

hi guys thank you for watching my

javascript tutorial this tutorial is

part of my JavaScript course where you

learn all the essential JavaScript

features that every web and mobile

application developer must know if

you're an absolute beginner or have some

experience in JavaScript and are looking

for a fun and in-depth course that

teaches you the fundamentals of

JavaScript this course is for you this

course is also packed with tons of

exercises that help you master what you

learned in the course in fact many of

these exercises are questions that come

up in technical programming interviews

so if you're pursuing a job as a

front-end or a back-end developer or if

you simply want to have a more in-depth

understanding of JavaScript I highly

encourage you to enroll in the course

for a limited time you can get this

course with a discount using the link in

the video description click the link to

find out more about the course and

enroll