Lecture ex1 - Advance JS

Lecture ex1 - Advance JS

  • Object, Array, for loop

  • Array Looping (for, while)

  • Scope

  • Basic Dom control in web js

Array

The Array object, as with arrays in other programming languages, enables storing a collection of multiple items under a single variable name, and has members for performing common array operations.

AKA a data type that stores a lot of items in the same variables (as a big box).


Why Array

Assume you have a series of data to store

1,4,67,54,32,75

And without array, you would store the values like this:

let numberOne = 1;
let numberTwo = 4;
let numberThree = 67
let numberFour = 54
// Bad method ...
  1. Instead of create a series of variables, we would store those data in a single boxes (array)

  1. We can easily sum up those data with array method.

  1. We can adjust the array in code instead of declare a new variable.


Array Rules One

  • In general, array can be store various item with same type

  • In js, you can mix up those type to store in the array (Not recommended)


Array Rules Two

To access the array, we can do like this:

Important: In general programming languages, the index of the array start with 0, means the stringArray array, tom is in stringArray[0].


Array Rules Three

To change the array content, we can do like this:


Array Rules Four

Array can store another array too. (2D array)


Common Array method

length

Get the array length (Count how many items are in the array)

push

push() add the new item to the front of the array

pop

pop() remove one front in the array


Common Array method with Loops

Loops can execute a block of code a number of times.

Assume we want to sum up the array

That's a bad pratice if the array have a lot of items. Let say 1000. Would you code those item 1000 times?

Instead of repeating, we would use loops to go through the array like this:

The for loop will loops through the numberArray, and get each items in the array.

These two codes are the same


Different Kinds of Loops

JavaScript supports different kinds of loops:

  • for - loops through a block of code a number of times

  • for/of - loops through the values of an iterable object

  • for/in - loops through the properties of an object

  • while - loops through a block of code while a specified condition is true

  • do/while - also loops through a block of code while a specified condition is true

References: https://www.w3schools.com/js/js_loop_for.asp


For Loop

The for statement creates a loop with 3 optional expressions:

Expression 1 is executed (one time) before the execution of the code block. Expression 2 defines the condition for executing the code block. Expression 3 is executed (every time) after the code block has been executed.


For loop with index

Expression 1 : let i = 0 (Initial values) Init a variable call i Expression 2 : i < 5 (Conditions) The loops will continus if this conditions is true Expression 3 : i++ (After a loop occur)


For loop with index


For loop with index (array)

Expression 1 : let i = 0 (Initial values) Expression 2 : i < arr.length (Conditions) Expression 3 : i++ (After a loop occur)


For loop with index (array)


Javascript for/of

In javascript, instead of using index, we can use for of loop to loop through elements directly.

of loop can loop the array without the index


Javascript for/in

in loop will have you to loop the array with the index, but do not need to init the index variables

in loop can get the array element with arr[i] also, but we do NOT recommended to use for in to loop the array in general situations.

The in loop are powerful in looping the object that we will cover in pages later.


Javascript while

while loop is a loop logics which mainly use on looping thats We do not know how many times will the looping occur. Means in most of the time, we will use for/of / for/in or index for to loop a array.


while loop a array

Javascript do while

It is the same as the while logics, but it will run the inside code ONCE no matter the while loops is true or false at first.

while Samples

Hello will NOT printed

do while Samples

Hello will print once even the while(false)


Continue and break

In both for and while loop, if we want to skip a certain conditions or break the loop, we may use continue and break to achieve the situations.


continue

when a continue occur, it will skip the current loop block.


break

when a break occur, it will skip WHOLE loop block.


Object

The Object type represents one of JavaScript's data types. It is used to store various keyed collections and more complex entities.

AKA we want to pack some variables together as a objects.

Assume we have a person data

Instead of split up those variables, we tend to create a object for this.

We can access the object like this:


We tend to use object for a series of variables for functions reuse.

function.js


Object array

We can fit the object into array too


Object array loop

Loop the whole object

Loop the whole object, print the name only

Loop the whole object with index

As we mentions before, we can loop the object too by for/in

Scope

Scope determines the accessibility (visibility) of variables.

JavaScript has 3 types of scope:

  • Block scope

  • Function scope

  • Global scope


Block / Local Scope


loopSamples.js


Global scope

If the variable is in global, all the blobk, function and section can be access the variable.


Basic Dom control in web js

Document Object Model (DOM)

Last, we will learn how to add interactions in web

Let's assume we have the html like this

index.html


Get the DOM elements

In general, we have these functions to get the regarding elements. Both functions are built in for web.

Those function can help us to get the regarding elements.


Add interactions to boxes

In general, we can add events to the DOM elements:


index.html


Advance control

You can control the message of the elements by innerHTML too.



counter.html

Last updated