Java Script
Java Script
Introduction
Features of JavaScript
JavaScript is an open source scripting language.
It is lightweight.
It creates network-centric applications.
It is platform independent.
It validates form data.
It reduces server load.
Advantages of JavaScript
Disadvantages of JavaScript
What is a script?
Script is a small, embedded program.
The most popular scripting languages on the web are, JavaScript or VBScript.
HTML does not have scripting capability, you need to use <script> tag.
The <script> tag is used to generate a script.
The </script> tag indicates the end of the script or program.
Example : Type attribute
<script type = “text/javascript”>
document.write(“TutorialRide”);
</script>
The 'type' attribute indicates which script language you are using with the type attribute.
Types of Scripts
1. Client-Side Scripting
2. Server-Side Scripting
1. Client-Side Scripting
<html>
<head>
<script type = "text/javascript">
function abc()
{
document.write("CareerRide Info");
}
</script>
</head>
<body>
Click the button
<input type=button onclick="abc()" value="Click">
</body>
</html>
Output:
CareerRide Info
<html>
<body>
<script type="text/javascript">
document.write("CareerRide Info");
</script>
</body>
</html>
Output:
CareerRide Info
3. External File
<html>
<body>
<script type="text/javascript" src="abc.js">
</script>
</body>
</html>
Output:
CareerRide Info
Example:
var a;
var b;
Example: var a, b;
Variable initialization
Variable Scope
1. Global Variable
Output:
2. Local Variable
Inside Count: 7
Inside A: 6
Outside Count: 0
Outside A: 3
Arithmetic Operators
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (Reminder)
++ Increment
-- Decrement
Comparison Operators
Operator Description
== Equal To
=== Exactly Equal To
!= Not Equal To
< Less Than
> Greater Than
<= Less Than or Equal To
>= Greater Than or Equal To
Assignment Operators
Operator Description
= Simple Assignment
+= Add and Assignment
-= Subtract and Assignment
*= Multiply and Assignment
/= Divide and Assignment
%= Modulus and Assignment
Logical Operators
Operator Description
&& Logical AND
|| Logical OR
! Logical NOT
Bitwise Operators
Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
˜ Bitwise NOT
<< Left Shift
>> Right Shift
>>> Right Shift with Zero
Special Operators
Operator Description
NEW Creates an instance of an object type.
DELETE Deletes property of an object.
DOT(.) Specifies the property or method.
VOID Does not return any value. Used to return a URL with no value.
1. If Statement
Output:
2. If – Else Statement
Output:
3. Switch Statement
Output:
4. For Loop
Syntax
for(initialization; test-condition; increment/decrement)
{
//Statements;
}
Example : Palindrome Program using For Loop
<html>
<body>
<script type="text/javascript">
function palindrome()
{
var revstr = " ";
var strr = document.getElementById("strr").value;
var i = strr.length;
for(var j=i; j>=0; j--)
{
revstr = revstr+strr.charAt(j);
}
if(strr == revstr)
{
alert(strr+" - is Palindrome");
}
else
{
alert(strr+" - is not a Palindrome");
}
}
</script>
<form>
Enter a String or Number: <input type="text" id="strr" name="checkpalindrome"><br>
<input type="submit" value="Check" onclick="palindrome();">
</form>
</body>
</html>
Output:
5. For-in Loop
6. While Loop
Syntax
while (condition)
{
//Statements;
}
Output:
7. Do-While Loop
Syntax
do
{
//Statements;
}
while(condition);
Output:
0
1
2
3
4
5
In while loop, first it checks the In Do – While loop, first it executes the
condition and then executes the program. program and then checks the condition.
The condition will come before the body. The condition will come after the body.
If the condition is false, then it It runs at least once, even though the
terminates the loop. conditional is false.
8. Break Statement
9. Continue Statement
Continue statement causes the loop to continue with the next iteration.
It skips the remaining code block.
Flow Diagram of Continue Statement
Syntax:
continue;
Built-in Objects
Built-in objects are not related to any Window or DOM object model.
These objects are used for simple data processing in the JavaScript.
1. Math Object
PI Returns Π value.
Math Methods
Methods Description
Output
Output:
E Value is :2.718281828459045
LN2 Value is :0.6931471805599453
LN10 Value is :2.302585092994046
PI Value is :3.141592653589793
2. Date Object
Example:
var current_date = new Date();
Date Methods
Methods Description
getTimezoneOffset() Returns the timezone offset in minutes for the current locale.
Output:
3. String Object
Example:
var s = new String(string);
String Properties
Properties Description
constructor It returns the reference to the String function that created the object.
String Methods
Methods Description
charCodeAt() It returns the ASCII code of the character at the specified position.
concat() It combines the text of two strings and returns a new string.
split() It splits a string object into an array of strings by separating the string
into the substrings.
Output:
1. DOM Object
DOM Properties
Properties Description
documentMode It returns the mode used by the browser to render the document.
Domain It returns the domain name of the server that loaded the document.
Referrer It returns the URL of the document that loaded the current document.
DOM Methods
Methods Description
Output:
What is form validation?
Form validation is the process of checking the forms that have been filled in correctly before
they are processed.
It provides a method to check the user entered information on client-side before the data is
submitted to the server-side.
It includes two methods for validating forms:
1. Server-Side (ASP, PHP)
2. Client-Side (JavaScript)
<html>
<body>
<script>
function validateemail()
{
var a = document.myform.email.value;
var atposition = a.indexOf("@");
var dotposition = a.lastIndexOf(".");
if (atposition<1 || dotposition<atposition+2 || dotposition+2>=a.length)
{
alert("Please Enter a valid E-mail Id");
return false;
}
}
</script>
</body>
<body>
<form name="myform" method="post" action="validpage.html" onsubmit="return
validateemail();">
Enter Your Email Id: <input type="text" name="email"><br/>
<input type="submit" value="Submit">
</form>
</body>
</html>
<html>
<body>
<script type="text/javascript">
alert("You are a Valid User !!!");
</script>
</body>
</html>
Output: