Javascript

Intro

What does (Client Side) JS do?

Add JavaScript to the Webpage

insert Javascript code between <script></script>

<script>
function example(){
    //MORE
}
</script>

or

<script src = "example.js"></script>

Display Something in the Webpage

Statement

var x = 5+6; // this is an example

JS statements are composed of

Data Type

Array & Object

var anArray = [a,b,c];
var anObject = {name:"Jams", id:007, weight:70};

Scope

Funciton

Closure

A closure is a function having access to the parent scope, even after the parent function has closed.

Event

Loop Control

For-in & For-of

// iterates over the values of object's properties
for (var name in person) { }

// iterating over iterable objects
for (var oneperson of person) { }

Break & Continuie & Labels

<script type="text/javascript">
    document.write("Entering the loop!<br /> ");
    outerloop: // This is the label name
 
    for (var i = 0; i < 5; i++)
    {
       document.write("Outerloop: " + i + "<br />");
       innerloop:
       for (var j = 0; j < 5; j++)
       {
          if (j > 3 ) break ; // Quit the innermost loop
          if (i == 2) break innerloop; // Do the same thing
          if (i == 4) break outerloop; // Quit the outer loop
          document.write("Innerloop: " + j + " <br />");
       }
    }
    document.write("Exiting the loop!<br /> ");
</script>

Global Reference

DOM

In the HTML DOM (Document Object Model), everything is a node

The HTML DOM is a standard for how to get, change, add, or delete HTML elements.

Three keys: Object,Method,Property;

document.getElementById("demo").innerHTML = "Hello World!";
// Object: document
// Method: getElementById()
// Property: innerHTML

Find HTML Elements

document.getElementById()
getElementsByTagName
document.getElementsByClassName()

Changing HTML Elements

element.innerHTML=
element.attribute=
element.setAttribute(attribute,value)
element.style.property=

Changing Context

Add Event Handler

document.getElementById(id).onclick=function(){code}

Topic

Fork me on GitHub