JavaScript is the scripting language and is used for the web. It is used webpages for validating forms,adding different functionalities , for detecting different browsers and creating cookies. One can also improve the design of web page using JavaScript. With the help of JavaScript one can work on any browser.Before working with JavaScript it necessary to know HTML and also XHTML. JavaScript is an interpreted language and so is directly used in HTML pages.
Let us start working with JavaScript. For example in the code below we have start with JavaScript.
The output is Hello.
When we write <script type="text/javascript"> it means we are using JavaScript in the program. To start with JavaScript you have to insert this tag and to end you need to write </script> document.write is for giving the out put.You need to write this command in between this tags <script> and </script> so that browser will accept it as a JavaScript command and then execute it.
When one places a script in head section it gets loaded and then gets executed when it is called.In following way it is written to place in head section
When one has to place Javascript in body section it can be written this way
If has to be placed in both then it is written this way
Sometimes one has to write same code on many pages so it becomes difficult to write same thing on many pages so to avoid one can create an external file.
You have to write that code program in external file and save it as .js extension.You can place it in following way
You can place it where you actually want to write the script.In the above example abc is the name of the file. Javascript statements are the commands to the browser and they are case sensitive.It is optional to write semicolon after each statement in Javascript but writing semicolon helps programmer to write multiple statements on one line.
Statements in Javascript are written in following way
Javascript statements are grouped together in blocks. If one wants to execute the statements in specific sequence then can be written in blocks.In Javascript blocks are written in curly braces { }. Let us see how they are written
Now let us see how to write comments in Javascript.Single line comments start with //.
For example
You can declare JavaScript variables using var statement. For example var a=10; Now let us see JavaScript If...Else Statements.
First let us start with if statement.
if statement is executed when condition is true.
The syntax is as follows
Let us see an example
Second example
In both the examples when condition is true then only if statement will be executed.
In If...else Statement ( if statement is executed when condition is true and else will be executed when condition is not true.)
The syntax is as follows
Let us see an example
In the above example if an employee is having salary more than 100000 then employee is having Senior post else Junior post.
Now let us discuss about loops in Javascript
1. For loop
2. While loop
1. For loop :- This loop is used when you know how many times the script has to be executed.
Syntax
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 10. i will increase by 1 each time the loop runs.
While loop
The while loop is used when you want the loop to execute and continue executing while the specified condition is true.
Syntax
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 10. i will increase by 1 each time the loop runs.
Let us start working with JavaScript. For example in the code below we have start with JavaScript.
HTML Code:
<html> <body> <script type="text/javascript"> document.write("Hello"); </script> </body> </html>
When we write <script type="text/javascript"> it means we are using JavaScript in the program. To start with JavaScript you have to insert this tag and to end you need to write </script> document.write is for giving the out put.You need to write this command in between this tags <script> and </script> so that browser will accept it as a JavaScript command and then execute it.
When one places a script in head section it gets loaded and then gets executed when it is called.In following way it is written to place in head section
HTML Code:
<html> <head> <script type ="text/javascript> .... </script> </head>
HTML Code:
<html> <head> </head> <body> <script type="text/javascript"> .... </script> </body>
HTML Code:
<html> <head> <script type="text/javascript"> .... </script> </head> <body> <script type="text/javascript"> .... </script> </body>
You have to write that code program in external file and save it as .js extension.You can place it in following way
HTML Code:
<script src="abc.js"> </script>
Statements in Javascript are written in following way
HTML Code:
<script type="text/javascript"> document.write("<h1>This is a header</h1>"); document.write("<p>This is a first paragraph</p>"); document.write("<p>This is a second paragraph</p>"); </script>
HTML Code:
<script type="text/javascript"> { document.write("<h1>This is a header</h1>"); document.write("<p>This is a first paragraph</p>"); document.write("<p>This is a second paragraph</p>"); } </script>
For example
HTML Code:
// This statement is for header:
document.write("<h1>This is a header</h1>");
// This statement is for first paragraph:
document.write("<p>This is for first paragraph</p>");
For Multi line comments start with /* and end with */.
/*
The code given below will write
one header and one paragraph
*/
document.write("<h1>This is a header</h1>");
document.write("<p>This is a first paragraph</p>");
First let us start with if statement.
if statement is executed when condition is true.
The syntax is as follows
if (condition)
{
code to be executed if condition is true
}
Let us see an example
HTML Code:
<script type="text/javascript"> if (time<10) { document.write("<b>Good morning</b>"); } </script>
HTML Code:
<script type="text/javascript"> if (time==1) { document.write("<b>Lunch-time!</b>"); } </script>
In If...else Statement ( if statement is executed when condition is true and else will be executed when condition is not true.)
The syntax is as follows
if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is not true
}
Let us see an example
HTML Code:
if (salary < 100000)
{
document.write("Senior");
}
else
{
document.write("Junior");
}
</script>
Now let us discuss about loops in Javascript
1. For loop
2. While loop
1. For loop :- This loop is used when you know how many times the script has to be executed.
Syntax
for (var=startvalue;var<=endvalue;var=var+increment)
{
code to be executed
}
Example
HTML Code:
<html> <body> <script type="text/javascript"> var i=0; for (i=0;i<=10;i++) { document.write("The number is " + i); document.write("<br />"); } </script> </body> </html>
While loop
The while loop is used when you want the loop to execute and continue executing while the specified condition is true.
Syntax
while (var<=endvalue)
{
code to be executed
} Example
HTML Code:
<html> <body> <script type="text/javascript"> var i=0; while (i<=10) { document.write("The number is " + i); document.write("<br />"); i=i+1; } </script> </body> </html>




