Javascript Arrays | Javascript Tutorial For Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
why hello hello friends look we have awesome lighting now and so excited we are back at home I can get started on making more and more videos so wooo yeah awesome alright today we are gonna cover arrays now what are arrays Oh arrays are basically variables that you can store multiple values inside of them I don't know why I'm wearing headphones because I don't need them let me take them off good so let's get started here in our FJ yes you know that we can create a variable by writing Const and give it a name like name what we did so far is equal to let's say L alright so this is the basics of variables that we cover it alright how do we create an array well to create an array since we're throwing multiple values it's recommended to give it a name that's plural okay so let's sleep let's say we're making a an app that look at that looks at your schedule English is very hard today alright so it looks at your schedule and you have things planned for that day so we're gonna create a schedule here alright and we're gonna set that equal to and what we're gonna do is we are gonna put some square brackets okay and this basically says to JavaScript that hey you want to create an array alright so what can we store in this array well we can store everything that we previously learned in and the videos so far so like strings we can store numbers we can store boolean and even objects which we didn't cover but we're gonna get to it and yeah we can even store that so let how do we do it well we do the brackets and here we can let's just do string so my schedule for today let's say it's wake up like so and when you want to add another value all you have to do is add a comma and then you can just add another string like normal wake up eat film video and then watch things on Netflix Netflix just like so and we closed it and booked you created your first very variable nice all right so I know when I learned JavaScript it was kind of hard to figure out hey why do I even need this thing why do I need this variable this array or even the variable why do I need these things well you can imagine them like this so again if maybe I wanna store things I want to do for the day like we did schedule because what we want to do eventually is take this information and output it on the screen so we can do something like this so we can have the schedule and we can have a lot of strings in here that represent yeah what we're gonna do for the day you can also store other things like numbers so here we can create something like phone numbers or something set it equal to and we can do I don't know what that's right correct phone numbers but here we go that's an example right there so you can store these and what eventually you want to do is let's take this schedule example back and let's say we want to add values to this or how do we access things from this so we're gonna add our console lock here so we can display something in our console and let me open up this real quick because I forgot hey there we go I'm gonna drag it right here so we can see press f12 and get a bunch of errors but that's cool we don't need that clear and let me zoom in here so we can see everything nicely okay good so right now if we console.log schedule and there we go so as you can see we have our array so if we get all the values now what if we want to access each individual value how do I access these well arrays are indexed from 0 all the way to whatever how many items you have so the index is basically represents the number of the empty value that you have here so the thing is though that an arrays they start from zero so index of zero of this array is wake up this is 1 this is 2 this is 3 so basically you count from 0 I know why who knows so that's a bit tricky to get used to but that's all you have to remember you count from 0 so if I want to access the first item all you have to do is basically write schedule add the brackets okay and this is the way you can you can look up the index of a specific item in here so what you're writing here is basically the number of the value that it represents so if I write 0 that's gonna access this one so the first one so if we copy and paste it in here we are gonna get wake up in here so it would be 1 like so and what would be watch things on Netflix well that would be 1 and 0 I did it 0 1 2 3 so that would be the third one so if we write 3 here that's gonna get us watch things on Netflix good so we learn how to access things awesome how do we add things maybe I want to add more things to my schedule in my app so maybe I want to add more things so how do I add more things more things more things more things okay so the way we do that is we write schedule you know how we talked about functions there's a method on on the array that we can call and basically what the method is we're gonna look more in-depth when we talk about objects but you can imagine that javascript comes with pre-built functions that we can use on things like strings so the way we had the two upper case on the string so that's like built-in function or method that we can use well arrays also come with a lot of built-in functions that we can use so things like to add an element to an array to remove an element to access a specific index so I'm gonna show you all the basic methods that we can use on arrays so we can use something called push so the way we access it is we write schedule dot and push so what push allows us to do is just add another element to the end of this array all right so the way we do that is we just add a string here and we say hey a new thing got added and if we save this and we cancel out the schedule again so the whole array we are gonna get let's take a look look hey and you think God added so this is a way we can add more things so maybe you can have an input on your page boom you can push a new value you can get the value from the input and you can add more things to it so that's one thing we can do we can also remove the last value so to do that all we have to do is not write push we can write pop okay we can pop and here we don't need to add anything because we were moving okay we don't need to specify a parameter here so a value so we can delete this and we can just write pop so we just call this like so and then the last value is gonna be removed so the watch things on Netflix there's also eating called a shift and on shift which okay and don't really use them we rarely are gonna remove things from the beginning of the array but I'm gonna show you so all you have to do is write shift and then you can add a beginning so you can add things to the beginning like so and if we okay I apologize so these are the opposite actually so shift is gonna remove the the first one so as you can see we have eat right now so wake up got removed and on shift is gonna add a new one at the beginning I always confuse these two see a new one because I rarely use them but hey the pushing and popping is gonna be used a lot of times on shift yeah not so much but hey here they are so if you need them there you go another thing we can do is you can access all so let's say you want to access this by its index so so what I mean by that is you can return the number based on what you have here so to do that we can do schedule schedule dot index of and then you can write eat and what this is gonna return you if I copy paste it in here like so let me delay that is you're gonna get the number the index number of the string that you have in here so what it's gonna do is basically you write schedule dot index of and it's gonna look for eat so it's gonna be like hey is eat here nope is it here yes it's here awesome here is the index number of it one so what you can do is hey you can actually this is used quite often so you can store this if you want so we can make another variable if we want and we can do something cons dot let's name this let's write the film here so maybe you want to get this one so we can set that equal to schedule dot index of film a video and that's gonna return us the number so if we console.log this film so the variable so we can store this whole value in here and we can get to right so you can even do something like schedule now film right because this is the number all right we're just storing the number of this in here so if we say console.log now and we say schedule film then we are gonna get film a video yeah those are pretty much the basics of arrays we are gonna cover it new ways of doing things with looping and objects in the next videos so thank you again so much for watching I apologize if the setup is a bit different now but hey I love it because I have a lot of light coming in on my face so that's awesome oh god alright thank you again so much drop the subscribe and I'll see you in the next one bye bye [Music]
Info
Channel: Dev Ed
Views: 56,565
Rating: undefined out of 5
Keywords: Javascript Arrays, Javascript Tutorial For Beginners, javascript (programming language), array data structure, javascript arrays, arrays, array, javascript arrays tutorial, javascript array tutorial, free javascript, javascript tutorial for beginners, javascript tutorial beginner, javascript course, javascript lesson, web development (interest), web design (interest), web development tutorial, web design tutorial, web design lesson, web developer (job title)
Id: 8FmBEN0XZyI
Channel Id: undefined
Length: 11min 25sec (685 seconds)
Published: Tue Jan 29 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.