Docsity

Arrays, Arrays in JavaScript - Introduction to Computing - Lecture Slides, Slides of Introduction to Computing

Partial preview of the text

Download Arrays, Arrays in JavaScript - Introduction to Computing - Lecture Slides and more Slides Introduction to Computing in PDF only on Docsity!

CS101 Introduction to Computing

Lecture 26

Arrays

(Web Development Lecture 9)

During the last lecture we had a discussion on Flow Control & Loops

  • We discussed the concept of flow control using the “if” and “switch” structures
  • And also the concept behind the “while” and “for” looping structures
  • We also solved simple problems using flow control and loop structures

Compound Statements

  • At times, we need to put multiple statements at places where JavaScript expects only one
  • For those situations, JavaScript provides a way of grouping a number of statements into a single statement, called a “statement block”
  • This is done simply by enclosing any number of statements within curly braces, { }
  • NOTE: Although the statements within the block end in semicolons, the block itself doesn’t (^) Docsity.com^4

for: Example 1

x = 1 ;

while ( x < 6000 ) {

document.write ( x ) ;

x = x + 1 ;

5

for ( x = 1 ; x < 6000 ; x = x + 1 ) {

document.write ( x ) ;

Initial count Condition Operation

‘for’ loops become especially useful when used in conjunction with arrays

We’ll find out about arrays today, and we’ll probe their usefulness as part of ‘for’ loop structures

Today’s Topic: Arrays

  • We will find out why we need arrays
  • We will become able to use arrays for solving simple problems

Array

An indexed list of elements

We said that a variable is a container that holds a value.

Similarly, an Array can be considered a container as well, but this one can hold multiple values

Array

An indexed list of elements

Example: There are many ways of assigning identifiers to the following fruit

11

strawberry fruit fruit[ 0 ]

orange fruit fruit[ 1 ]

apple fruit fruit[ 2 ]

watermelon fruit fruit[ 3 ]

Array

13

fruit[ 0 ]

Identifier Square bracket

Index

Let’s now take look at one of the advantages of using arrays

16

student = new Array( 4 ) ; //array declaration

student[ 0 ] = “Waseem” ;

student[ 1 ] = “Waqar” ;

student[ 2 ] = “Saqlain” ;

student[ 3 ] = “Daanish” ;

for ( x = 0 ; x < 4 ; x = x + 1 ) {

document.write( student[ x ] ) ;

}

Can you see the advantage of using arrays along with the ‘for’ loop?

Arrays in JavaScript

  • In JavaScript, arrays are implemented in the form of the ‘Array’ object
  • The key property of the ‘Array’ object is ‘length’, i.e the number of elements in an array
  • Two of the key ‘Array’ methods are:
    • reverse( )
    • sort( )
  • Elements of an array can be of any type; you can even have an array containing other arrays (^17)

student = new Array( 4 )

19

This is the identifier of the new instance

The ‘new’ operator creates an instance

This is the parent object (or class) of the new instance

Length of the new instance of ‘Array’

Pair of paren- theses

The ‘assignment’ operator

An Object