JavaScript is a fun and farly simple and simplistic language, but can get very complicated depending upon what the user wants to do. *Note: What is listed below is not everything that can be done in JavaScript, and more will be added to this page. Also note that all examples work, and have pages that will go into deeper detail about the code structure.
Explanation Code

JavaScript Basics

            Unlike most programming languages that allow you to define a variable as an int, double, etc JavaScript only allows for you to declare a variable as var. This means that whenever you give the variable a value, JavaScript usually decides to hold the value in a String, mean it would be considered almost a word. So if you did 2+2, you would get 22 instead of 4 since the "+" sign with two strings means to simple put them together. There is an example of this on the example page for this section. For example the assigning a value to a variable would look like this: var num1=2; But if you did this: var num1="2"; that now means that number two is being held in the variable as a string (like a letter, or word would be), the same way as, "two", would be held in another variable, but just becuase you enter a number in digit form dosn't mean that JavaScript will regonize that it is a interger or a String. What you must do to ensure that JavaScript knows what type of variable is being passed in, other than a String, is parse it This is usually done only if JavaScript will not regonize the data type, an issues that is uaually run into every so often. What this means is you want JavaScript to turn the in comming value into eahter a: Integer/Double or boolean , to name the major variables that will be used. The syntax looks like this for turning/making sure a variable is a integer number: parseInt(variable); There are examples shown on the example page for futhur clarification.

            There is also a type of variable called a Global variable. This variable is really no different then all the others, EXECPT that it is visiable to all of the functions in between the script tags (<script>), not inside of a single function. When a variable is inside of fuction, that means that the variable is only visible INSIDE of that function, thus it's Scope is only in that function. When declared outside of all functions though, the variable has a global scope.

            You should also know that just about everything you do in JavaScript is done inside of a function or multiple functions. A Function is a block of code that can be called over and over again to perform a specific task. So say you make a function to take numbers from 3 different fields and add them all together. The function would allow you to call it every time someone that the total button, and thus would add all the values from the provided fields, add them all together, and then if you wanted to, you can display the total (shown in more detail on examples page). Another very important bit of information to know is that, HTML and JavaScript work together by using EVENT HANDLERS. These event handlers, like the name suggests, handle events. An even can be something such as; a mouse click, a mouse over, when the page loads, etc. The event handlers allow you to tell the page what function to call when certain event takes place. Such as a button being pressed, going back to the previous example, it will then call the function you tell it to, and perform the task you programmed the function to do. You may also pass values to functions, all you have to do is place a variable in between the () and when you call the function in the event handler, just have the value you want passed up in between the () of the function call.

Go here see more examples and see further explanations on Basic JavaScript.

Enter anything for variable 1 (i.e ca7):

Enter anything for variable 2 (i.e 45):



Code:
Code



Link for more infomation on: Basic JavaScript

JavaScript Event Handlers

            Event Handlers are what make webpages interactive, and then can be used from anything from inside of an image tag to a table on an HTML page. When a button is clicked a Event Handler detects that it has been pushed (example right). Though there is not much more to say about even handlers this part will provide a comprehensive list of Event Handlers and some examples of how to use them.

Click me!

Double-click me!!

*Code on example page



Link for more infomation on: Event Handlers

JavaScript Document Object

            The document Object is probably one of the must used objects in JavaScript. It is used from writing to the document, to getting the attibutes of every instance of a tag on the page, thus allows basic changes to be made. Though there is really not much to say about the document object, it is good to know what it's able to do on the web page.

Here is a comprehensive list of document Document Object.



Link for more infomation on: Document Object

The "if" Statement

            Just like in any programming language you, as the programmer want to be able to make decisions about information that has been input into the program. For example you may want to check and see if a number is of a certain value so that you may print something or do something with that number once it is the value you wish. The easiest way to do this is with the 'if' statement, also known as the decision statement. In this decision statement you can check to see if a value is Equal to (==), greater than (>), less than (<), greater than or equal to(>=), less than or equal to(<=), and not equal too (!=). These symbols are called comparisons, and allow you to compare numbers to and other types of variables to each other, such as words or letters. The syntax of the 'if' statement looks like this: if(<condition>) {}. A 'if' statement condition CAN ONLY be TRUE or FALSE. The way the 'if' works is it compares variables based on what type of comparison you give the "if". So lets say you want to check if 2 is equal to 3, which would look like this: if(2==3){do something}. So the 'if' takes the 2 and 3, compares them and finds out that 2 is not equal to 3, it then sends back a FALSE, and since the result is FALSE the 'if' will not execute and will fall to the ELSE (a fall back as I call it), if there is one. You may have a ELSE IF that allows you to test for a different set of conditions without completly leaving the 'if' and having to start a new one.

           'if' statements also allow you to check multiple conditions at one time using and (&&), and or (||). With and (&&) both statements on either side of the && must be TRUE for the 'if' to execute, but with or (||) only ONE of the conditions on either side of the || must be true for the 'if' to execute.

Here is more explanation on "if" Statements in JavaScript.

Enter a number between one and ten!

Code:
Code



Link for more infomation on: "if" Statements

JS Loops

            Although "if" statements and variables are very useful, it would be very difficult to write a program with only those tools at your disposal, especially when it comes to tasks that repeat themselves many times over. That is where loops come in. Loops allow you, the programmer, to execute a section of code as many times as needed to in order to perform a certain task. For example, let's say the user wants you count to a specific number that is specified by the user. So what you have to do, say the user inputs 20, is count 1 to 20 in sequential order, displaying each number on the page till you get to twenty. Not knowing the upper limit of the numbers they could enter or what number the user is going to put in makes the program interesting, but not having a data structure that can repeat itself would make this problem nearly impossible to solve.

           That's where loops come into play, and there are 3 different kinds, the "while loop," the "for loop," and the "do loop." The first one we will talk about is the "while loop," which essentially means, continue while a certain condition is true. The syntax looks like this: while (<condition>){}. Remember that just like the "if" statement loops only need curly braces ({}) if there is MORE THAN ONE LINE of code that goes inside of that loop. A while loop in our situation could be used to take a variable that is a number and increase it from 1 till it is greater than 20. The syntax would look like this: while(someVar<20){do something}.

           Next is the "for loop," which is usually used when you, the programmer, knows how many times the loop needs to reiterate. The syntax for the "for loop" looks like this: for(declareVariable<condition>incrementDeclareedVariable){}. A "for" loop in our particular situation would be very handy since we will know how many times the loop is going to execute. In other words, even though we DO NOT know what number the user is going to put in, we do know that the loop is going to have to execute a certain number of times based on what the user enters into the program.

           Finally there is the "do while" loop, which will execute at least once, even if the condition is not met. The syntax for the "do while" loop looks like this: do {(code)}while(<condition>); Notice that the "do while" loop is the only loop that has a semicolon (;) after the condition. For our example a "do while" loop WOULD NOT BE be very useful because if the user enters zero, then the "do while" loop would still print out 1.

           As can be seen, loops are very helpful and are extremely important when it comes to programming. These loops are all very useful in different ways in different circumstances, it just takes time and practice to learn where, when, and how to use them. Also remember that in loops, you use the same compairsons as you would use inside of an "if" statement.

Here is more explanation on Loops in JavaScript.

Example: How Loops work


Input a number between 1 and 20:



Link for more infomation on: Loops

JS Arrays

            Everything thus far has been very useful and many different situations and allow you to perform many tasks on different information, but what if you had more than just 2 or 3 variables? What if you had 100? That's where Arrays come into play. Arrays allow you to keep large amounts of data in one variable. Each peace of data has it's own seat (a.k.a index), like seats on a bus. The bus (a.k.a Array) has a name and each seat on the bus has a number starting with 0. These numbers are called indexes. So for example, say you have 4 numbers that you want to put into the Array. The first number would have the index value of 0, the second number would have the index value of 1, and the last number would have the index value of 3. Even though the physical size of the Array is 4, the "index size " of the Array would be 3. This is where a lot of people get tripped up. It is crucial to remember that the physical amount of data is different from the "index size" of the Array.

            The syntax for declaring an Array looks like this: var arrayName= new Array(); After the declaration you can then assign indexes of the Array specific values, for example: arrayName[0]=2; , arrayName[1]=4; , etc. You may also declare and set values to the Array, the syntax looks like this: var arrayName= new Array(value1,value2,value3,etc);

            Once you have all those values in the Array your probably wondering, "How do I print out the entire Array if I want or need to?" That is actually very easy, and you already have the tool to use it. Loops are the most effective and efficient way to find and edit information in an Array, learning how to use a loops and Arrays to your advantage will take time, but eventually it all makes sense.

            JavaScript Arrays also have many defferent properties, such as length, and methods such as indexof(). These are discussed in more detial on the Arrays page.





       Link for more infomation on: Arrays