1 Java Script
1 Java Script
var varname;
<body>
<script>
var x = 5;
var y = 6;
var z = x + y;
document.write("value of z is" +z);
</script>
</body> </html>
FUNCTIONS
• A function is some code that is executed when an event fires
or a call to that function is made.
• Typically a function contains several lines of code.
• Functions are written in the <head> element.
Syntax
function function-name(parameter1, parameter2, parameter3)
{
code to be executed
}
<html><head><script>
function add(){
var a,b,c;
a=Number(document.getElementById("first").value);
b=Number(document.getElementById("second").value);
c= a + b;
document.getElementById("answer").value= c;
}
</script></head><body>
Enter First Value:<input type="text" id="first"><br>
Enter second Value:<input type="text" id="second"><br>
Answer:<input id="answer"><br>
<button onclick="add()">Answer</button>
</body></html>
BUILT-IN OBJECTS IN JAVASCRIPT
concat() Returns a new array comprised of this array joined with other array(s)
and/or value(s).
pop() Removes the last element from an array and returns that element.
push() Adds one or more elements to the end of an array and returns the new
length of the array.
reverse() Reverses the order of the elements of an array -- the first becomes the
last, and the last becomes the first.
shift() Removes the first element from an array and returns that element.
slice() Extracts a section of an array and returns a new array.
sort() Sorts the elements of an array
unshift() Adds one or more elements to the front of an array and returns the new
length of the array.
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.write("<h1> Before Array operations:</h1>"+fruits);
fruits.pop();
document.write("<h1> After POP():</h1>"+fruits);
fruits.push("Kiwi");
document.write("<h1> After Push():</h1>"+fruits);
fruits.shift();
document.write("<h1> After shift():</h1>"+fruits);
fruits.unshift("Sapota");
document.write("<h1> After unshift():</h1>"+fruits);
var num=["2","1","3"]
var my= num.concat(fruits);
document.write("<h1> After concat():</h1>"+my);
document.write("<h1> After reverse:</h1>"+my.reverse());
document.write("<h1> After sort:</h1>"+my.sort());
</script>
String Object
•The JavaScript string is an object that represents a
sequence of characters.
•There are 2 ways to create string in JavaScript
•By string literal
•By string object (using new keyword)
1.string literal
The string literal is created using double quotes.
syntax:
var stringname="string value";
Ex:
<script>
var str="This is string literal";
document.write(str);
</script>
2)string object (using new keyword)
var stringname=new String("string literal");
•Here, new keyword is used to create
instance of string.
Ex:
<script>
var str=new String("hello javascript string");
document.write(stringname);
</script>
Methods
Method Description
charAt() Returns the character at the specified index.
concat() Combines the text of two strings and returns a new string.
replace() Used to find a match between a regular expression and a
string, and to replace the matched substring with a new
substring.
slice() Extracts a section of a string and returns a new string.
substr() Returns the characters in a string beginning at the specified
location through the specified number of characters.
toLowerCase() Returns the calling string value converted to lower case.
Method Description
alert() displays the alert box containing message
with ok button.
confirm() displays the confirm dialog box containing
message with ok and cancel button.
<html><head>
<script>
function msg(){
alert("Hello Alert Box");
}
</script></head> <body>
<html><head><script>
function msg(){
var v= prompt("Who are you?");
alert("I am "+v);
}
</script> </head> <body>
<input type="button" value="click" onclick="msg()">
</body></html>
DOCUMENT OBJECT
Method Description
Event Description
onclick The user clicks an HTML element
•This is the most frequently used event type which occurs when a
user clicks the html element like button.
Syntax:
onclick= function()
Ex:
<input type="button" onclick="printvalue()" value="print name"/>
onsubmit Event type
•onsubmit is an event that occurs when you try to submit a form. You can
put your form validation against this event type.
Ex:
<form name="myform" method="post" onsubmit="validateform()" >
DATA VALIDATION
Data validation is the process of ensuring that user input is clean, correct, and
useful.
• Typical validation tasks are:
1. has the user filled in all required fields?
2. has the user entered a valid date?
3. has the user entered text in a numeric field?
• Most often, the purpose of data validation is to ensure correct user input.
• Validation can be defined by many different methods, and deployed in many
different ways.
Server side validation is performed by a web server, after input has been sent to
the server.
Client side validation is performed by a web browser, before input is sent to a web
server.
JavaScript Form Validation