Javascript

Oliver Stigley

Date: 23 february

How does Javascript compare to HTML and CSS?

Javascript adds a layer of functionality to websites that HTML and CSS cannot. It allows web content to become dynamic. Previously I have used the analogy that HTML is the walls, roof and floor of a house, CSS is the interior design. Well, Javascript then tells us how to behave in each room. Can we throw pillows? Can we move paintings? Can we change the colour of the bed sheets? It lets you manipulate the DOM! Javascript lets you make these decisions.

Control Flow

Control flow refers to the flow of controlling instructions in a script. It is the order in which different lines of code are called: statements, instructions or functions calls. The most common control flow structure is the if, else, else if statement. These statements run conditional tests, which then trigger subsequent statements or actions, depending if the statements are true or not. Other control flow statements include for loops, while loops, switch statements and for in statements. An everyday life example of control flow would be hanging out your washing. For each item in the washing basket, hang it on the line, until there are no washing items left. This would look like: for ( i=washing.length-1; i >= 0 ; i- - ) { washing[i] . hang }

Arrays Vs Literals

Accessing data from an array requires the user to provide the location of the array item they wish to access. This is done with square bracket notation. If I want to access the 3rd item in my array, the correct statement would be myArray[2] (using 0 based indexing). Accessing data from an object containing {“property”: “value”} pairs, can be done using dot notation or square bracket notation, referencing the property of the object. Each method will return the value. myObject.property will return “value”, and so will myObject[“property”].

Functions

Functions let us pre-package code that can be used repeatedly, with different inputs and parameters. It saves a lot of re-inventing the wheel.