Variables, Operators, Comparisons and Events in JavaScript
Variables are very useful in any programing language, they allow you to call the value you set to it. A variable can be any word, a sentence, numbers or a combination of numbers and sentences. You can also name a variable anything you want, as long as it does not have start with a digit, have any spaces, punctuation in it. The variable name mySite is correct where as 1site is incorrect. JavaScript variables are also case sensitive, that is to say mySite and mysite, are not the same variable. To call a variable in JavaScript can be done multiple ways. The most basic way is by telling the JavaScript client you are creating a variable with var. Here are some examples of variables in JavaScript.
<script language="JavaScript" type="text/javascript"> var mySite = "http://www.code-sucks.com"; var myNumb = 14; var myVar = 'iEntry2008'; var myOp = 3+5; </script>
If the variable is more then one character long, you have to either put double or single quotes around it. When variables are longer then For numbers, you do not not have to put any type of quotes around it. There are some reserved words in JavaScript that you can not use for a variable name, which are shown in the table below. These are words that are used in JavaScript, like if, or are reserved for future releases of JavaScript.
| abstaract | as | boolean |
| break | byte | case |
| catch | char | class |
| continue | const | debugger |
| default | delete | do |
| double | else | enum |
| export | extends | false |
| final | finally | float |
| for | function | goto |
| if | implements | import |
| in | instanceof | int |
| interface | is | long |
| namespace | native | new |
| null | package | private |
| protected | public | return |
| short | static | super |
| switch | synchronized | this |
| throw | throws | transient |
| true | try | typeof |
| use | var | void |
| volatile | while | with |
Another useful part of any programming language are operators. Operators allow you to do math functions, and at strings together. Most of them are like your basic math operators, but some of them are a little different.
| Operator | Action | Examples |
|---|---|---|
| x+y | Adds the two variables together. | 3+5 will make 8. 3+Five will make 3Five |
| x-y | Subtracts the second variable from the first. | 5-3 will make 2 |
| x*y | Multiplies the two variables | 3*5 will result in 15 |
| x/y | Divides the variables. | 6/3 will return 2 |
| x%y | Returns the modulus (remainder) of x divided by y | 5%2 returns 5 |
x++, ++x | Adds one to the variable | x=3, x++ now x equals 4 |
x--, --x | Subtracts one from x | x=3, x-- now x equals 2 |
| -x | Changes the sign of x | x=3, -x now x is equal to -3 |
Just remember to be careful when using the ++ and -- operators, they may do the same when used independently, but when using them to assign a value to a variable, they act differently. If you use y=x++, that would set y to equal what the value of x was, and x now equals x+1. Wwhere as y=++x will make both x and y equal to x+1. The minus (--) works the same way.
Another group of operators are called comparison operators. Theses allow you to compare two variables values, or stings with each other. They are mostly used in functions to tell the function what you would like to do based on the outcome of the comparison.
| Operator | Action |
|---|---|
| x==y | Compares x and y together to make for sure they are the same, if so it returns true |
| x!=y | Checks to see if x and y are not equal to each other. If the values are not equal it returns true. |
| x>y | Verifies that x is larger then y, if x is larger then the statement returns true. |
| x<y | Compares x and y, and if x is smaller then a true value is returned. |
| x>=y | Checks to see if x is either greater then or equal to y. If the x is larger or equal to y then it returns true. |
| x<=y | Compares x to y, and if x is less then or equal to y it returns true. |
| x&&y | If both x and y are true then the statement is true |
| x||y | If either x or y are true then the statement is true |
| !x | If x is false, then the statement is true |
JavaScript events, are what allow JavaScript to interact with the user, and allow things scripts to be ran when users do certain actions. Events are used to create alerts, rollovers, and interactive elements on your site.
| Event | Action |
|---|---|
| onabort | Used to run a script if an image stops loading |
| onblur | When an element is no longer the focus of the users |
| onchange | When the element's value changes, an action is performed. |
| onclick | If you want to run code when an element is clicked, then this element is what you need. |
| ondblclick | Same as click, but the user has to double click the element. |
| onerror | This will run code if there is an error when loading the web page |
| onfocus | When an element is the focus of the user, it triggers this event |
| onkeydown | This event is triggered when a key is pressed on the keyboard. |
| onkeyup | If the key chosen is released, then the code is ran. |
| onkeypress | This is the same as keydown, but also is triggered if the key is held down. |
| onload | Actives a script once an image or the page loads. |
| onmousedown | Event is triggered when a mouse button is pressed down. |
| onmouseover | When the mouse moves over the HTML element, it activates the event. |
| onmouseout | If the mouse is no longer on the element this trigger is activated. |
| onmousemove | Similar to onmouseover, when the mouse touches the element it is activated. |
| onmouseup | This is triggered after clicking an element. |
| onrest | Used to reset a form |
| onresize | When the window has is resized, this event is triggered. |
| onselect | If text is selected, then the event has been triggered. |
| onsubmit | Similar to onreset. The event is triggered when the submit but is pressed. |
| onunload | When the user moves from the page, the event is triggered. |
iEntry 10th Anniversary
CSS Snippets
JavaScript Snippets