- HTML Form Validation – to validate input elements for email, phone number etc.
- These tags can be placed in head section as well as in body section.XHTML1<script>---</script>
- Variables in any language are the placeholders that can store different datatype values in it.
- There are rules to define a variable.
- Variables should start with a letter.
- Variables are case sensitive
- Declaration of an empty variable x is shown below:
- Here var is a keyword used to declare a variable.
- Declaration of variables with assignment of a value is shown below:
- Here, two variable declaration statements are shown above.
- The two statements are one and the same, i.e. they will do exactly the same work of declaring a variable y with value 4 assigned to it.
- From this you might have understood that var keyword is not compulsory. We can declare variable without var keyword also.
- Array is just a variable that holds more than one values.
- Variables have a property to hold a single value. But if you want to store more values under a single variable, you can definitely use arrays.
- Declaration of arrays is shown below:
- If you want to store values in this array, you can store it as follows:
- Array index starts from zero(0).
- You can assign values as shown above at every index.
- Next way of declaring array along with the assignment of values is shown below:
- This creates an array of cars with values assigned to it as parameters.
- Loops are used to run the same code over and over until a certain condition is met.
- For Loop: It loops through a block of code a number of times. You usually know the number of times.
- While Loop: It loops through a block of code until a condition is true. You usually don’t know the number of times.
- For In Loop: It loops through array object values.
- Do—While Loop: It is same as While loop, only it checks the condition at the last. It is not used frequently.
- Here, we will see syntaxes of for loop and while loop to get an idea of the loops.
Let’s see an example.
var cars=new Arrays(); // this is an empty array having no values stored in it.
var cars=new Array(“Honda”,”Toyota”,”Ford”);
- For Loop:
- In for loop, the initialization, condition check and increment/decrement is done in a single statement.
- document.write(); is a command that is used to print the output on the screen.
- The loop will repeat 5 times, since the condition will remain true until i=4.
The output of both the above loops will be:
- A function is a block of code that runs when it is called.
- It is mostly used to reduce the length and complexity of the program.
- If a program requires same code to be written in most of the places, we can write function for it once and call it wherever required, thus reducing the programmer’s work.
- Syntax for creating a function:
- The function is defined with a keyword function followed by a function name and paranthesis.
- Whatever statements present inside the opening and closing curly braces form the body of the function.
- The function is not executed unless it is called.
- Syntax for calling a function:
- Calling a function is very simple. Just write the function name with paranthesis.
- A function can have parameters also.
document.write(“This is some function”);
- Some built in objects are: String, Date, Array, Function, etc.
- Objects have properties or characteristics.
- An example is shown below:
- Here, x is equal to 5 as it is equal to the length property of the “greeting” variable.