JavaScript Unit1 1 1
JavaScript Unit1 1 1
net
What is JavaScript?
- JavaScript is an open source & most popular client side scripting language supported by all
browsers.
- JavaScript is a dynamic computer programming language.
- JavaScript is a lightweight, interpreted programming language.
- It is an interpreted programming language with object-oriented capabilities.
- JavaScript is a very powerful client-side scripting language.
- JavaScript is used mainly for enhancing the interaction of a user with the webpage.
- You can make your webpage more lively and interactive, with the help of JavaScript.
- JavaScript is also being used widely in game development and Mobile application development.
Javascript History:
Features of JavaSript:
Author-Blogshub.net
Advantages of JavaScript:
1. Less server interaction − you can validate user input before sending the web page to the server.
This saves server traffic, which means less loads on your server.
2. Immediate feedback to the visitors − they don't have to wait for a page reload to see if they have
forgotten to enter something.
3. Increased interactivity − you can create interfaces that more interactive.
4. Richer interfaces − you can use JavaScript to include such items as drag-and-drop components and
sliders to give a Rich Interface to your site visitors.
Limitations of JavaScript:
1. Client-side JavaScript does not allow the reading or writing of files. This has been kept for
security reason.
2. JavaScript cannot be used for networking applications because there is no such support available.
3. JavaScript doesn't have any multi-threading or multiprocessor capabilities.
JavaScript Tag:
- JavaScript can be implemented using JavaScript statements that are placed within the <script>...
</script> HTML tags in a web page.
Author-Blogshub.net
- You can place the <script> tags, containing your JavaScript, anywhere within your web page, but
it is normally recommended that you should keep it within the <head> tags.
- The <script> tag alerts the browser program to start interpreting all the text between these tags as a
script.
- A simple syntax of your JavaScript will appear as follows.
<script ...>
JavaScript code
</script>
1. Language − this attribute specifies what scripting language you are using. Typically, its value
will be JavaScript
2. Type − this attribute is what is now recommended to indicate the scripting language in use
and its value should be set to "text/javascript".
- Example:
JavaScript code
</script>
<html>
<body>
<script language = "javascript" type = "text/javascript"> document.write("Hello
World!")
</script>
</body>
</html>
Comments in JavaScript:
Author-Blogshub.net
- There are five types of primitive data types in JavaScript. They are as follows:
Author-Blogshub.net
JavaScript Variable:
- Syntax:
var name;
- Example:
- Example
Author-Blogshub.net
<html>
<body>
<script> var no=4; //global variable
function CalcSquare()
{
var square; //local variable square=no*no;
document.writeln("Square of Number="+square);
}
CalcSquare();
</script>
</body>
</html>
- A list of all the reserved words in JavaScript is given in the following table.
- They cannot be used as JavaScript variables, functions, methods, loop labels, or any object names.
Author-Blogshub.net
1. Arithmetic Operators
2. Comparison (Relational) Operators
3. Bitwise Operators
4. Logical Operators
5. Assignment Operators
6. Conditional Operators
Arithmetic Operators:
- Example
<html>
<body>
<script type = "text/javascript"> var a = 33;
Author-Blogshub.net
a = ++a;
document.write("++a = "); result = ++a;
document.write(result); document.write(linebreak);
b = --b;
document.write("--b = "); result = --b;
document.write(result); document.write(linebreak);
</script>
</body>
</html>
Comparison Operators:
Author-Blogshub.net
- Example
<html>
<body>
<script type = "text/javascript"> var a =
10; var b = 20;
var linebreak = "<br>";
Bitwise Operators:
- Example
Author-Blogshub.net
<html>
<body>
<script type = "text/javascript"> var a = 2;
var b = 3;
var linebreak = "<br>";
Logical Operators:
- Example:
<html>
<body>
<script type = "text/javascript"> var a =
true; var b = false;
var linebreak = "<br>";
Assignment Operators:
- Example:
<html>
<body>
<script type = "text/javascript">
var a = 33; var
b = 10; var result;
</script>
</body>
</html>
Conditional Operator:
- In above syntax, if condition is true then it will execute Expression-1 otherwise Expression-2.
- Example:
<html>
<body>
<script type = "text/javascript">
result = (a > b) ? a : b;
document.write("Greatest Number="+result);
</script>
</body>
</html>
Author-Blogshub.net
If Statement:
if(condition) {
//body of if
}
- Example.
<html>
<body>
<script type = "text/javascript"> var age = 20;
17
Author-Blogshub.net
If-else Statement:
if(condition)
{
//body of if
} else
{
//body of else
}
- Example.
<html>
<body>
<script type = "text/javascript">
}
</script>
</body>
</html>
18
Author-Blogshub.net
If…else…if Statement:
if(condition-1)
{
//statement-1
}
else if(condition-2)
{
//statement-2
}
else if(condition-3)
{
//statement-3
} else
{
//statement-4
}
- Example.
19
Author-Blogshub.net
<html>
<body>
<script type = "text/javascript">
20
Author-Blogshub.net
</script>
</body>
</html>
if(condition-1)
{
if(condition-2)
{
//statements
}
else
{
//statements
}
} else
{
if(condition-3)
{
//statements
}
else
{
//statements
}
- Example.
21
Author-Blogshub.net
<html>
<body>
<script type = "text/javascript">
var a=10;
var b=20; var c=30;
if(a>b)
{
if(a>c)
{
document.write("Greatest Number="+a);
}
else
{
document.write("Greatest Number="+c);
}
}
else
{
if(b>c)
{
document.write("Greatest Number="+b);
}
else
{
document.write("Greatest Number="+c);
}
</script>
</body>
</html>
22
Author-Blogshub.net
- Switch statement is used to execute one of the statements from many blocks of statements.
- It is a selection statement.
- There are four keywords are used i.e switch, case, break, default.
- Switch case expression value compared with case value and if it is match then corresponding
block will be executed.
- If none of case values are matched then default block is executed.
- Syntax:
switch(Expression)
{
case value1:
//statement; break;
case value2:
//statement; break;
case value3:
//statement; break;
- Example.
<html>
<body>
<script type = "text/javascript"> var grade
= 'M'; switch (grade)
{
case 'A': document.write("Good job");
break;
break;
23
Author-Blogshub.net
JavaScript Loops:
24
Author-Blogshub.net
1. for loop
2. while loop
3. do-while loop
4. for-in loop
For Loop:
//block of statements
- In for loop, program controller first goes to initialize section, after that it will test the condition.
if condition is true then it will executes body of for loop and after that goes to
increment/decrement section.
- Again it will check the condition and if condition is true then it will again executes the body of
for loop. This process will continue till condition becomes false.
- Example
<html>
<body>
<script type = "text/javascript"> var i;
document.write("Output of For Loop=");
</script>
</body>
</html>
While Loop:
25
Author-Blogshub.net
while(condition)
{
//block of statements
- In while loop, program controller first test the condition. if condition is true then it will executes
body of for loop.
- Again it will check the condition and if condition is true then it will again executes the body of
while loop. This process will continue till condition becomes false.
- Example
<html>
<body>
<script type = "text/javascript"> var i=1;
while(i<=10)
{
document.write("Current value of i : " + i ); i=i+1;
}
</script>
</body>
</html>
Do-While Loop:
26
Author-Blogshub.net
- Syntax:
do
{
//block of statements
}while(condition);
- In do- while loop, program controller first executes the body of loop and then test the condition.
if condition is true then it will again executes body of loop.
- This process will continue till condition becomes false.
- In do-while loop, program controller executes body of loop at least once even if condition
becomes false very first time.
- Example
<html>
<body>
<script type = "text/javascript">
var i=1;
do
{
document.write("Current value of i : " + i ); i=i+1;
}while(i<=10);
</script>
</body>
</html>
For-in Loop:
27
Author-Blogshub.net
- In each iteration, one property from object is assigned to variablename and this loop
continues till all the properties of the object are exhausted.
- Syntax:
- Example: following example implement ‘for-in’ loop. It prints the web browser’s
Navigator object.
<html>
<body>
<script type = "text/javascript">
var aProperty;
document.write("Navigator Object Properties<br> ");
</script>
</body>
</html>
Break and continue theory is similar to our C and C++ language concept.
28