Creating Array and Fetching Elements in JavaScript

Telusko
3 Jul 202107:14

Summary

TLDRIn this JavaScript tutorial, David Reddy introduces the concept of arrays, explaining their necessity when dealing with multiple values without keys. He demonstrates two methods of creating arrays: using the 'new Array' constructor and direct assignment within square brackets. Reddy then shows how to assign values to an array, check its length, and use the 'push' method to add elements dynamically. Additionally, he clarifies the zero-based indexing system in arrays and the potential issue of accessing an undefined element beyond the array's length, suggesting a precautionary check before fetching values.

Takeaways

  • πŸ“š The video is part of a series on JavaScript, continuing from previous discussions on objects, primitive types, and the concept that non-primitive data types are objects.
  • πŸ—ƒ Arrays are introduced as a data structure for when you need to store multiple values without assigning keys to each value, unlike objects.
  • πŸ”‘ Arrays can be created using the `new Array()` constructor function or by using square brackets `[]` to assign values directly.
  • πŸ“¦ Two methods for creating an array are shown: one that initializes an empty array and another that pre-populates the array with values.
  • πŸ” The `length` property of an array is used to determine the number of elements it contains, which is demonstrated in the script.
  • πŸ“ˆ The `push` method is explained as a way to add elements to an array after its creation, with examples of how it works.
  • πŸ”‘ Accessing array elements is done using index numbers, starting with zero as the index for the first element.
  • 🚫 Attempting to access an element at an index that does not exist in the array results in `undefined`, which is a potential source of errors.
  • πŸ”„ The script suggests checking the array's `length` before trying to access an element to avoid errors related to out-of-bounds indices.
  • πŸ” The video promises to cover how to add elements to an array using index values in a future video.
  • πŸ‘‹ The presenter invites viewers to comment, subscribe, and engage with the content, indicating an interactive and educational approach.

Q & A

  • What is the main topic of the video?

    -The main topic of the video is about arrays in JavaScript.

  • What are the two ways mentioned in the video to create an array in JavaScript?

    -The two ways to create an array in JavaScript mentioned in the video are using the 'new Array' constructor function and directly assigning values within square brackets.

  • What is the purpose of arrays in JavaScript when objects can also store collections of data?

    -Arrays are used for storing collections of values without the need for keys, unlike objects, which require key-value pairs.

  • How does the video demonstrate creating an empty array in JavaScript?

    -The video demonstrates creating an empty array by using 'new Array' and by using an empty square bracket notation, both of which result in an empty array.

  • What is the property used to check the length of an array in JavaScript?

    -The 'length' property is used to check the number of elements in an array in JavaScript.

  • How can you add values to an array that was initially empty?

    -You can add values to an initially empty array using the 'push' method in JavaScript.

  • What happens if you try to access an index that is beyond the length of the array?

    -If you try to access an index beyond the length of the array, JavaScript will return 'undefined'.

  • How does the video explain the concept of array indexing?

    -The video explains array indexing by stating that it starts with zero, and each subsequent element increases the index by one.

  • What is the method used in the video to add a single value to an array?

    -The 'push' method is used in the video to add a single value to an array.

  • Why does the video suggest checking the length of an array before accessing its elements by index?

    -The video suggests checking the length of an array before accessing its elements by index to avoid accessing undefined values or going out of bounds.

  • How does the video address the issue with 'undefined' when accessing an out-of-bounds index in an array?

    -The video mentions that it's not ideal to get 'undefined' and suggests that it would be better to get an error or a message indicating the index is out of bounds, similar to Java's 'ArrayIndexOutOfBoundsException'.

Outlines

00:00

πŸ“š Introduction to JavaScript Arrays

In this segment, the host, David Reddy, introduces the concept of arrays in JavaScript, building upon the previous discussions about objects and primitive types. He clarifies the need for arrays as a way to handle multiple values without the need for keys, as opposed to objects. The host explains two methods of creating arrays: using the 'new Array' constructor function or by directly assigning values within square brackets. He also demonstrates how to declare an empty array and how to check if it's empty by printing its values. The segment concludes with a practical example of assigning values to an array and printing them to verify their presence.

05:00

πŸ” Understanding Array Length and Indexing

This paragraph delves into the mechanics of determining the length of an array and accessing individual elements through indexing. The host explains the use of the 'length' property to find out how many elements an array contains and demonstrates this with a code example. He then discusses the concept of array indexing, highlighting that computer indexing starts at zero, which can lead to confusion when trying to access the 'first' element (which is actually at index 0). The host provides a step-by-step guide on how to correctly access elements by their index numbers and warns about the potential for 'undefined' results when attempting to access an index that exceeds the array's length. The segment ends with an invitation for viewers to share their thoughts in the comments and a reminder to subscribe for more content.

Mindmap

Keywords

πŸ’‘JavaScript

JavaScript is a high-level, interpreted programming language commonly used for enhancing web pages with interactive elements. In the video, it is the main programming language being discussed, with a focus on arrays, which are a fundamental data structure in JavaScript for storing collections of values.

πŸ’‘Arrays

An array in JavaScript is an object that can hold multiple values at a time. It is used when you have a collection of values and you do not need to assign a unique key to each value. The script explains how to create and manipulate arrays, emphasizing their use for handling multiple values without keys.

πŸ’‘Objects

In JavaScript, an object is a collection of properties, where each property is a key-value pair. The video script contrasts arrays with objects, noting that while objects use keys to access values, arrays use numerical indices. The concept of objects is foundational to understanding the need for arrays when keys are not necessary.

πŸ’‘Primitive Types

Primitive types in JavaScript refer to the basic data types like number, string, boolean, etc. The script mentions that everything that is not a primitive type is considered an object, which includes arrays. Understanding primitive types helps in distinguishing between them and complex data structures like arrays.

πŸ’‘Constructor Function

A constructor function in JavaScript is a special type of function used with the 'new' keyword to create new objects. The script demonstrates creating an array using the 'new Array()' syntax, illustrating the concept of constructor functions in the context of array creation.

πŸ’‘Index

Index refers to the position of an element within an array. The script explains that in JavaScript, array indices start at zero, which is a common point of confusion for beginners. The concept of index is crucial for accessing and manipulating elements within an array.

πŸ’‘Push Method

The push method is an array function in JavaScript that adds one or more elements to the end of an array and returns the new length of the array. The script uses the push method to demonstrate how to add elements to an array after its creation.

πŸ’‘Length Property

The length property of an array in JavaScript returns the number of elements in the array. The script mentions using the length property to find out how many elements are in an array, which is essential for understanding the array's size and capacity.

πŸ’‘Undefined

In JavaScript, 'undefined' is a value representing the absence of a value or when a variable has not been initialized. The script discusses what happens when you try to access an array element beyond its length, which results in 'undefined', highlighting a common error when dealing with arrays.

πŸ’‘Index Out of Bounds

Index out of bounds is a common error that occurs when trying to access an array element with an index that is outside the valid range of the array's indices. The script touches on this concept when explaining that attempting to access an element with an index higher than the length of the array results in 'undefined'.

Highlights

Introduction to JavaScript arrays as a concept distinct from objects, emphasizing their use for collections of values without keys.

Explanation of the difference between objects and arrays, with objects having key-value pairs and arrays containing only values.

Demonstration of two methods to create an array in JavaScript: using the 'new Array' constructor and direct assignment with square brackets.

Illustration of creating an empty array and the difference between using 'new Array' and an empty square bracket notation.

How to assign values to an array, showcasing the flexibility to include numbers, strings, objects, and functions.

Introduction of the 'length' property to determine the number of elements in an array.

Explanation of the 'push' method for adding elements to an array after its creation.

Demonstration of using the 'push' method to add multiple values to an initially empty array.

Clarification on how to access specific elements in an array using index numbers, starting with zero.

Example of accessing the first, second, and third elements of an array using their respective index numbers.

Discussion on the implications of accessing an index out of the array's bounds, resulting in 'undefined'.

Suggestion to check the array's length before attempting to access elements to avoid 'undefined' results.

Teaser for the next video, which will cover adding elements to an array using index values.

Invitation for viewers to share their thoughts in the comment section and a call to action for subscriptions.

The video's conclusion with a reminder of the key concepts covered and an encouragement for viewers to continue learning about JavaScript arrays.

Transcripts

play00:00

[Music]

play00:05

welcome back aliens my name is david

play00:07

reddy and let's continue with this

play00:08

series on javascript

play00:10

in this video we'll talk about arrays

play00:12

see in the earlier video we have talked

play00:13

about

play00:14

objects right and then before that we

play00:16

have also talked about primitive types

play00:18

and then we have also discussed that

play00:20

whatever data is not a type of primitive

play00:22

it will be an object

play00:23

now this can be the object which we

play00:25

created then we have functions which are

play00:27

objects

play00:28

and then we have areas which are objects

play00:30

but then when you have an object

play00:32

why you need another concept like arrays

play00:34

so if you look at the

play00:35

object we have a key and a value right

play00:38

example when you talked about

play00:39

aliens or laptop it had a key and a

play00:42

value pair

play00:43

example for laptop it can be cpu which

play00:45

is your key and then i9

play00:47

i7 those are your values maybe you can

play00:49

go with another property which is let's

play00:51

say ram

play00:51

which is your key and then let's say 16

play00:54

gb is the value but in this case let's

play00:56

say you want to have multiple values and

play00:58

you don't need to have a key

play01:00

so let's say you have a lot of values

play01:02

and then you don't want to assign a key

play01:03

to it

play01:04

maybe you want to work with the index

play01:06

values can we do it now if you're coming

play01:08

from other languages like cc plus with

play01:09

java most of the languages has this

play01:11

concept but if you're new don't worry

play01:13

whenever you have a collection of values

play01:15

which you don't want to assign a key to

play01:17

it maybe you can

play01:18

use numbers to represent them we can use

play01:20

a concept like arrays how do we create

play01:22

an array here

play01:23

so let's say i want to create a array of

play01:25

values so what we can do is to create an

play01:27

array we can say new

play01:29

edit this is one of the syntax which we

play01:30

can follow so we can select

play01:32

values equal to new array and now you

play01:34

can guess this is a constructor function

play01:36

right because we are using a new keyword

play01:38

so basically we are trying to create an

play01:40

object okay there is one way of creating

play01:41

an array the other way is you can

play01:43

directly assign a value

play01:45

here itself maybe you can give a square

play01:46

bracket and you can assign the values

play01:48

okay so this works in the same way the

play01:50

other method works so we'll go with that

play01:52

only

play01:52

but again you can replace that with the

play01:54

new array keyboard here so let's use

play01:57

the empty square package this is another

play01:59

way of getting an array now this

play02:00

is your empty array so what we have done

play02:03

earlier with the help of new array is

play02:05

same as this one

play02:06

both will give you an empty array okay

play02:08

so when you empty it will not have any

play02:10

values right how do we check that so

play02:12

it's very simple you can just print the

play02:13

values to verify right

play02:14

so we can simply come back here and we

play02:16

can say values and when you run this

play02:18

code of course it will not print any

play02:20

values because it's blank so yes it is

play02:21

printing it's an array but we don't have

play02:23

any values now how do we assign a value

play02:26

of course there are multiple ways of

play02:27

doing it so what if you know the values

play02:29

already so in that case let's say the

play02:30

values are 5 7 and 8 that's how you

play02:32

assign the values so you can say 5 comma

play02:34

7 comma 8

play02:35

and you got an array with these three

play02:36

values of course there is no compulsion

play02:38

that they should only have numbers you

play02:40

can also have string

play02:41

or you can have other objects you can

play02:42

have a function your choice

play02:44

let's print this and you can see we got

play02:46

an array with three values

play02:47

that sounds cool right okay can we just

play02:49

print the length so maybe i want to know

play02:51

the length of this array so that's

play02:53

simple you can simply come back here and

play02:55

say length so values.length and it will

play02:58

print the length of the array and the

play03:00

length is three

play03:01

okay so you have three values in this

play03:03

array quite simple right so this is how

play03:04

you create an array the same thing you

play03:06

can do with new ad

play03:07

it's just that new array will give you

play03:08

empty array and you can assign the

play03:10

values later

play03:11

but how how do we assign the values

play03:13

later so let's say i want

play03:14

empty adder here and i want to assign

play03:17

values

play03:18

in that case you can use certain

play03:20

functions maybe you can use

play03:22

a function like push or you can say

play03:23

methods because this thing actually

play03:25

belongs to an object right

play03:26

so i can say values dot i can say a

play03:28

method which is push

play03:30

now what this push will do is whatever

play03:32

value you will assign here let's say

play03:33

5 or this value 5 will be assigned

play03:36

in this array okay so when you say push

play03:38

it will add the element

play03:39

let's see if that works uh let me just

play03:41

remove this length i want to print the

play03:43

actual values and let's see what happens

play03:45

ah so you can see we got only one value

play03:47

now which is five so initially it was an

play03:48

empty array

play03:49

but then you said hey i want to add one

play03:51

value it's doing that

play03:53

what if you repeat this what if you just

play03:54

copy this and

play03:56

add another value let's say seven and

play03:58

what will happen now

play03:59

so it will have five and seven and

play04:01

likewise if you add some more values it

play04:03

will

play04:03

add that as well okay uh so that's how

play04:06

you can push the values

play04:07

but again i will see this later let me

play04:10

just go with the predefined values here

play04:12

which is five seven and eight okay so

play04:14

when you print

play04:15

the values it's quite simple right it

play04:17

will simply print all the values but

play04:19

what if

play04:20

you want to fetch one particular value

play04:22

let's say i want to

play04:23

print the first value the second value

play04:25

third value how do we do that

play04:26

now we have seen this in objects right

play04:29

in object what we do is if you want to

play04:31

fetch a particular value

play04:32

you ask for the key right so you

play04:34

mentioned hey we have this value

play04:36

i want to fetch that with the help of a

play04:37

key but in this case we don't have a key

play04:39

right we all have values here but then

play04:41

as i mentioned before

play04:42

maybe you can fetch this with the help

play04:44

of numbers something like index numbers

play04:47

so this 5 will have a number this 7 will

play04:48

have some index number

play04:50

8 will have some index number example

play04:51

you know when you create a table

play04:53

you have a column which is the first

play04:55

column right serial numbers

play04:56

right uh example the number of the line

play04:59

number here so these are the index

play05:00

numbers you can imagine

play05:01

uh but for this value of phi what should

play05:03

be the index value so what i will do is

play05:05

uh first of all how do you measure index

play05:07

value

play05:07

uh the same way you whenever you mention

play05:10

array it always represents with the help

play05:12

of

play05:12

a square bracket so you are saying hey i

play05:15

want to fetch values

play05:16

and i don't want to fetch all the values

play05:18

i want to fetch the values

play05:19

with the index number so i can say hey i

play05:22

want to fetch the first one

play05:23

so i will say one and let's see what

play05:25

happens so when you mention

play05:27

one and let's run this code oh oh we got

play05:30

seven we wanted five but we got seven

play05:32

now what went wrong here is

play05:34

uh in computers basically whenever you

play05:36

talk about index values

play05:38

it always starts with zero so this five

play05:40

here it

play05:41

should be zero the index number is zero

play05:43

here okay so we should start with zero

play05:45

so when you say five is zero then seven

play05:48

will be one

play05:48

and eight as is at location two okay

play05:51

let's run this and you can see we got

play05:52

five

play05:53

okay uh how about if i say one uh it's

play05:56

quite simple now you know it should be

play05:58

seven we have done that before and then

play05:59

when you say two it will print eight

play06:01

let's try two as well

play06:03

and let's run this code and you can see

play06:04

we got eight but what will happen if i

play06:07

go for three because we have only three

play06:10

values

play06:11

and when you say i want to fetch the

play06:13

element at index number three that means

play06:15

we are trying to fetch the fourth value

play06:17

we don't have a fourth value here what

play06:19

will happen you will get undefined so

play06:21

luckily you don't get others

play06:22

in fact i am not a big fan of undefined

play06:25

i believe we should get either that

play06:27

there is no element outside three or

play06:30

maybe you can you should get

play06:31

something like in java you get other

play06:34

index sort of bound but here we don't

play06:35

have that you know

play06:36

it's printing undefined uh but dad you

play06:38

have to remember this whenever you have

play06:40

the add a if you want to fetch a value

play06:42

maybe you can

play06:44

first check what's the length of the

play06:45

array and then you can fetch the value

play06:47

yeah so that's how you can create an

play06:48

array and that's how you can fetch a

play06:50

particular value

play06:51

how do we add elements with the help of

play06:53

index value that will say in the

play06:55

next video so i hope you are enjoying

play06:58

this video let me in the comment section

play06:59

and do subscribe for the videos bye

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
JavaScriptArraysProgrammingData StructuresCoding TutorialIndexingPush MethodObject ComparisonPrimitive TypesFunction Objects