JavaScript For Beginners

coderzone's Avatar author of JavaScript For Beginners
This is an article on JavaScript For Beginners in JavaScript and AJAX.
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.
HTML Code:
<html>
<body>

<script type="text/javascript">
document.write("Hello");
</script>

</body>

</html>
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

HTML Code:
<html>
<head>
<script type ="text/javascript>
....
</script>
</head>
When one has to place Javascript in body section it can be written this way

HTML Code:
<html>
<head>
</head>
<body>
<script type="text/javascript">
....
</script>
</body>
If has to be placed in both then it is written this way

HTML Code:
<html>
<head>
<script type="text/javascript">
....
</script>
</head>
<body>
<script type="text/javascript">
....
</script>
</body>
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

HTML Code:
<script src="abc.js"> </script>
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

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>
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

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>
Now let us see how to write comments in Javascript.Single line comments start with //.

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>");
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

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>
Second example

HTML Code:
<script type="text/javascript">

if (time==1) 
{
	document.write("<b>Lunch-time!</b>");
}
</script>
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

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>
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

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>
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

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>
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.
Nishant24 like this
0
neo_vi's Avatar, Join Date: Feb 2008
Invasive contributor
nice info. wats the difference b/w placing the <script> Part in <head> section and <body> section. What happens? can we see any difference while executing?
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by neo_vi View Post
nice info. wats the difference b/w placing the <script> Part in <head> section and <body> section. What happens? can we see any difference while executing?
I don't think so.
0
neo_vi's Avatar, Join Date: Feb 2008
Invasive contributor
Then why do we use it in different sections. which is the script's default standard.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
At time you cannot and should not link in the head.

Say for example on some pages I need to include some script I put in the body but for all pages inclusion I put it in header. This is what I follow here and may not be actually true.
0
growingboy's Avatar
Go4Expert Member
http://www.quirksmode.org/js/placejs.html
0
skp819's Avatar
Contributor
Thanks for share it. Some place I not able to understand.
0
neo_vi's Avatar, Join Date: Feb 2008
Invasive contributor
Quote:
Originally Posted by skp819 View Post
Thanks for share it. Some place I not able to understand.
can u tell me in which place u can't understand
0
Xintruder's Avatar, Join Date: Dec 2008
Go4Expert Member
Great basic stuff. Why not continue this tutorial.
I suggest moving onto simple site browsing effects such as: mouse over effects.

1 question though:
Quote:
for (i=0;i<=10;i++)
Does this mean:

For i = 0 to 10 step 1

as in increment in ones?
Would the loop excecute correctly if I don't enter the "i++" part?

Thank you
0
neo_vi's Avatar, Join Date: Feb 2008
Invasive contributor
Quote:
Originally Posted by Xintruder View Post
Great basic stuff. Why not continue this tutorial.
I suggest moving onto simple site browsing effects such as: mouse over effects.

1 question though:

Does this mean:

For i = 0 to 10 step 1

as in increment in ones?
Would the loop excecute correctly if I don't enter the "i++" part?

Thank you
No. It wont execute correctly. Since it is in a loop it must contain an increment/decrement part. If its not there it'll be an infinite loop. ur browser(firefox) opens a dialog box saying there is an error, and will give u the options "stop script" and "continue"..