metaCode Louisville

a website about a class about making websites

JavaScript

Variables: variables are containers that you can store values in.

Parameters: a list of identifiers that work as local variables for the body of the function.

Arguments: values that are passed to a functions parameters.

Expressions: a phrase of JavaScript that a JavaScript iterpreter can evaluate to produce a value.**

Functions: a way of packaging functionality that you wish to reuse. This is a type of object, they can be assigned to variables and passed to other functions. Once a function is defined, it can be executed or invoked any number of times.**

Statements: if expressions are a phrase of JavaScript, then a statement is a sentance or command. There are several different kinds of JavaScript statements like conditional (if and while), Loops (while and for) and Jumps (break, return and throw). A program is made of up a sequence of JavaScript statements.

Objects: everything in JavaScript is an object, and can be stored in a variable.

Methods: when a function is assigned to the property of an object. object.method = function;

Loops: a statement that allows you to use them same section of code several times in succession to accomplish as task. There are 4 kinds of JavaScript loops, while, do/while, for and for/in.

Events: code which listen for things happening in browser, running code in response. Eventlisteners, clickhandlers...etc.

Conditionals: code structures which allow you to test if an expression returns true or not, running alternative code revealed by its result (if then)

All definitions came from or are paraphrased from MDN documentation except the ones maked **, they are from JavaScript the Definitive Guide by David Flanagan.

Loops

While Loops

The first 3 lines are a function that uses a random number generator. randomNumber() gives a value between 0 and 1, but never including 1 (between 0 and .99999). The floor makes it round down and the + 1 ensure that you never get a 0 back. the upper is the top number that the function will return.

The loop starts by declairing the variable counter and setting it to 0. Line 6 starts the loop with while and a test condition in parentheses which will run untill it tests false. In this case it is checking the counter var to see if it is less than 10. 0 < 10 so the loop will continue. line 7 creates a new variable named rand and sets the function equal to it. the 9 is the upper limit. line 8 does the purpose of the loop, in this case it writes the random number generated to the document. Finally line 9 adds the the counter variable and starts the loop over again. At this point the loop tests the condition again ( now 1 < 10 ) and continues again until the test condition is false (on the 10th pass 10 !< 10) so the loop is finished.

If this loops test condition is false, it will never run at all, and if it always tests right it is a infinite loop.

while loop with a random number generator function while loop with a boolean condition statement

Do {} while () Loop

These loops are very similar to while loops, the main difference is that this loop always runs the first time because the loop is ran before the test condition is tested. This loop is useful to prompt a user to give you information and make sure that they give you it to you by repeatedly prompting them. Usually use a true false variable as a flag to see if the condition was met.

This example starts by setting up the variables, and the standard RandomNumber generator. The formatting of the loop is different...The loop is presented first, and then the test condition is afterward in the while elemenet. In this case the test condition is that the correctGuess is wrong. So until you guess correctly, the game will continue, but it always runs atleast once.

Sample Do While Loop

For Loop

The entire first time is the counter info, line 2 is the loop itself and the 3rd line is the close. It’s common to set the counter variable as a single letter (i).

This example shows a couple of important ideas of loops. The for loop is very similar to while and do while, but it is more condensed. And this particular loops shows how you can use it to create html, and insert things into a website. This example produces 25 randomly colored boxes.

Sample For Loop
Examples from Treehouse JavaScript Videos

JavaScript Examples

Links

General JavaScript Documentation: Mozilla Developer Network Documentation (MDN) Standard Objects: Mozilla Developer Network Documentation (MDN) Grammar and Types: Mozilla Developer Network Documentation (MDN) MDN Repository of facts: Mozilla Developer Network Documentation (MDN) JavaScript Event Reference: Mozilla Developer Network Documentation (MDN) jQuery Documentation: a lightweight JavaScript library.