Introduction to JavaScript
JavaScript DataTypes
Numbers: e.g. 23, 4.3, -230, 4.4e-24 etc
Booleans: e.g. True, False
Strings: e.g. “hello”, “what is the time?”
Undefined
Declaring Variables
var height
var name, address, phoneNumber
Conditional Statements
Conditional statements are used to perform different actions based on different conditions.
Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this.In JavaScript we have the following conditional statements:
- if statement - use this statement to execute some code only if a specified condition is true
- if...else statement - use this statement to execute some code if the condition is true and another code if the condition is false
- if...else if....else statement - use this statement to select one of many blocks of code to be executed
- switch statement - use this statement to select one of many blocks of code to be executed
If Statement
Use the if statement to execute some code only if a specified condition is true.Syntax
| if (condition) { code to be executed if condition is true } |
Example
|
If...else Statement
Use the if....else statement to execute some code if a condition is true and another code if the condition is not true.Syntax
| if (condition) { code to be executed if condition is true } else { code to be executed if condition is not true } |
Example
|
If...else if...else Statement
Use the if....else if...else statement to select one of several blocks of code to be executed.Syntax
| if (condition1) { code to be executed if condition1 is true } else if (condition2) { code to be executed if condition2 is true } else { code to be executed if condition1 and condition2 are not true } |
Example
|
Switch Statement
| | |
Conditional statements are used to perform different actions based on different conditions.
The JavaScript Switch Statement
Use the switch statement to select one of many blocks of code to be executed.Syntax
| switch(n) { case 1: execute code block 1 break; case 2: execute code block 2 break; default: code to be executed if n is different from case 1 and 2 } |
This is how it works: First we have a single expression n (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically.
Example
|
JavaScript
For Loop
| | |
Loops execute a block of code a specified number of times, or while a specified condition is true.
JavaScript Loops
Often when you write code, you want the same block of code to run over and over again in a row. Instead of adding several almost equal lines in a script we can use loops to perform a task like this.
In JavaScript, there are two different kinds of loops:
- for - loops through a block of code a specified number of times
- while - loops through a block of code while a specified condition is true
The for Loop
The for loop is used when you know in advance how many times the script should run.
Syntax
| for (variable=startvalue;variable<=endvalue;variable=variable+increment) { code to be executed } |
Example
The example below defines a loop that starts with i=0. The loop will continue to run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs.
Note: The increment parameter could also be negative, and the <= could be any comparing statement.
| Example
|
While Loop
| | |
Loops execute a block of code a specified number of times, or while a specified condition is true.
The while Loop
The while loop loops through a block of code while a specified condition is true.Syntax
| while (variable<=endvalue) { code to be executed } |
Example
The example below defines a loop that starts with i=0. The loop will continue to run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs: Example
|
The do...while Loop
The do...while loop is a variant of the while loop. This loop will execute the block of code ONCE, and then it will repeat the loop as long as the specified condition is true.Syntax
| do { code to be executed } while (variable<=endvalue); |
Example
The example below uses a do...while loop. The do...while loop will always be executed at least once, even if the condition is false, because the statements are executed before the condition is tested: Example
|
JavaScript Functions
| | |
A function will be executed by an event or by a call to the function.
JavaScript Functions
To keep the browser from executing a script when the page loads, you can put your script into a function.
A function contains code that will be executed by an event or by a call to the function.
The return statement is used to specify the value that is returned from the function.So, functions that are going to return a value must use the return statement.
Things that are important
· Name of the function
· The number and type of parameters the function expects
Functions can be defined both in the <head> and in the <body> section of a document. However, to assure that a function is read/loaded by the browser before it is called, it could be wise to put functions in the <head> section.
How to Define a Function
Syntax
| function functionname(var1,var2,...,varX) { some code } |
The parameters var1, var2, etc. are variables or values passed into the function. The { and the } defines the start and end of the function.
Note: A function with no parameters must include the parentheses () after the function name.
How to Call a Function
Syntax
| functionname() or functionname(arg1,arg2,...,argN) { some code } |
A function with no parameters must include the parentheses () after the function name.
The parameters or arguments or the values those which are passed to the function. The { and the } defines the start and end of the function.
What is an Event ?
JavaScript's interaction with HTML is handled through events that occur when the user or browser manipulates a page.
When the page loads, that is an event. When the user clicks a button, that click, too, is an event. Another example of events are like pressing any key, closing window, resizing window etc.
Onclick Event
JavaScript Function Example
| Example
|
If the line: alert("Hello world!!") in the example above had not been put within a function, it would have been executed as soon as the page was loaded. Now, the script is not executed before a user hits the input button. The function displaymessage() will be executed if the input button is clicked.
onmouseover and onmouseout Event
These two event types will help you to create nice effects with images or even with text as well. The onmouseover event occurs when you bring your mouse over any element and the onmouseout occurs when you take your mouse out from that element.
Example:
Following example shows how a division reacts when we bring our mouse in that division:
| Example
|
JavaScript Popup Boxes
| | |
JavaScript has three kind of popup boxes: Alert box, Confirm box, and Prompt box.
Alert Box
An alert box is often used if you want to make sure information comes through to the user.
When an alert box pops up, the user will have to click "OK" to proceed.
Syntax
| alert("sometext"); |
| Example
|
MessageBox Example
| Example
|
Confirm Box
A confirm box is often used if you want the user to verify or accept something.
When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed.
If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false.
Syntax
| confirm("sometext"); |
| Example
|
Prompt Box or Inputbox
A prompt box or inputbox is often used if you want the user to input a value before entering a page.
When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value.
If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null.
Syntax
| prompt("sometext","defaultvalue"); |
| Example
|
Example
|
Example
|
The Lifetime of JavaScript Variables
If you declare a variable, using "var", within a function, the variable can only be accessed within that function. When you exit the function, the variable is destroyed. These variables are called local variables. You can have local variables with the same name in different functions, because each is recognized only by the function in which it is declared.If you declare a variable outside a function, all the functions on your page can access it. The lifetime of these variables starts when they are declared, and ends when the page is closed