Professional Documents
Culture Documents
JavaScript
• What is JavaScript?
JavaScript is a high level, interpreted, programming language used to make web pages
more interactive.
• Client-side validation
• Displaying popup windows and dialog boxes (like alert dialog box, confirm dialog box
and prompt dialog box)
Uses of JavaScript
• Client-side validation
• Validate user input in an HTML form before sending the data to a server.
• Change the appearance of HTML documents and dynamically write HTML into separate
Windows.
• Manipulate HTML "layers" including hiding, moving, and allowing the user to drag them
around a browser window.
Limitations of JavaScript
• It cannot be used for networking applications because there is no such support available.
Feature of JavaScript
• It is light weighted.
• Most of the javascript control statements syntax is same as syntax of control statements in
C language.
• An important part of JavaScript is the ability to create new functions within scripts.
Declare a function in JavaScript using function keyword.
<!DOCTYPE HTML>
<html>
<body>
<script>
</script>
</body>
</html>
• In this example, x, y, and z, are variables, declared with the var keyword:
• var x = 5;
var y = 6;
var z = x + y;
• Local scope
• Global scope
• Local variables have Function scope: They can only be accessed from within the
function.
• function myFunction() {
var carName = "Volvo";
• A global variable has global scope: All scripts and functions on a web page can access it.
function myFunction() {
• Boolean
• Number
• String
• Null
• Undefined
• Symbol
A boolean data type represents only one of two values either true or false.
Example:
Example:
• Strings are used for storing text. Strings must be inside of either double or single quotes.
Example:
• It is explicitly nothing.
Example:
Example:
var testVar;
console.log(testVar); // undefined
• It is explicitly nothing.
Example:
Example:
var testVar;
console.log(testVar); // undefined
• Object
• Date
• Array
Operator Description
+ Addition
- Subtraction
* Multiplication
** Exponentiation
/ Division
++ Increment
-- Decrement
= x=y x=y
+= x += y x=x+y
-= x -= y x=x–y
*= x *= y x=x*y
/= x /= y x=x/y
%= x %= y x=x%y
**= x **= y x = x ** y
e.g
var x = 10;
x += 5;
O/P:- 15
Example
var txt1 = "John";
var txt2 = "Doe";
var txt3 = txt1 + " " + txt2;
John Doe
Operator Description
== equal to
!= not equal
!== not equal value or not equal type
? ternary operator
Operator Description
|| logical or
! logical not
Any numeric operand in the operation is converted into a 32 bit number. The result is converted
back to a JavaScript number.
Operator Description Example Same as Result Decimal
JavaScript Functions
• JavaScript a function allows user to define a block of code, give it a name and then
execute it as many times as user want.
• A function can be defined using “function” keyword and can be executed using ()
operator.
• All the functions can access arguments object by default instead of parameter names.
• JavaScript allows you to create anonymous functions that must be assigned to a variable.
Function Syntax
//defining a function
function <function-name>()
// code to be executed
};
//calling a function
<function-name>();
Function Example
<html>
<body>
<script>
function MyMessage()
alert("Hello World!");
MyMessage();
</script>
</body>
</html>
<html>
<body>
<script>
ShowMessage(“Narendra", “Modi");
ShowMessage("Bill", "Gates");
ShowMessage(100, 200);
</script>
</body>
</html>
<html>
<body>
<script>
ShowMessage("Bill");
ShowMessage();
</script>
</body>
</html>
Return Values
<html>
<body>
<script>
};
alert("sum="+addition(10,20));
</script>
</body>
</html>
What is an Event ?
JavaScript's interaction with HTML is handled through events that occur when the user or the
browser manipulates a page.
When the page loads, it is called an event. When the user clicks a button, that click too is an
event. Other examples include events like pressing any key, closing a window, resizing a
window, etc.
Developers can use these events to execute JavaScript coded responses, which cause buttons to
close windows, messages to be displayed to users, data to be validated, and virtually any other
type of response imaginable.
Events are a part of the Document Object Model (DOM) Level 3 and every HTML element
contains a set of events which can trigger JavaScript Code.
This is the most frequently used event type which occurs when a user clicks the left button of
his mouse. You can put your validation, warning etc., against this event type.
Example
<body>
<p>Click the following button and see result</p>
<form>
<input type = "button" onclick = "sayHello()" value = "Say Hello" />
</form>
</body>
</html>
onsubmit is an event that occurs when you try to submit a form. You can put your form
validation against this event type.
Example
The following example shows how to use onsubmit. Here we are calling a validate() function
before submitting a form data to the webserver. If validate() function returns true, the form will
be submitted, otherwise it will not submit the data.
Try the following example.
<html>
<head>
<script type = "text/javascript">
<!--
function validation() {
alert(“Good Morning”);
}
//-->
</script>
</head>
<body>
<form method = "POST" action = " " onsubmit = "return validation()">
These two event types will help you create nice effects with images or even with text as well.
The onmouseover event triggers when you bring your mouse over any element and
the onmouseout triggers when you move your mouse out from that element. Try the following
example.
<html>
<head>
<script type = "text/javascript">
<!--
function over() {
document.write ("Mouse Over");
}
function out() {
document.write ("Mouse Out");
}
//-->
</script>
</head>
<body>
<p>Bring your mouse inside the division to see the result:</p>
<div onmouseover = "over()" onmouseout = "out()">
<h2> This is inside the division </h2>
</div>
</body>
</html>
browser. request.
this keyword
Example.
<script>
var website="Javatpoint";
function web()
document.write(this.website);
web();
</script>
<html>
<body>
</body>
</html>
• With the HTML DOM, JavaScript can access and change all the elements of an HTML
document.
• With the object model, JavaScript gets all the power it needs to create dynamic HTML:
• When a web page is loaded, the browser creates a Document Object Model of the page.
If you want to access any element in an HTML page, you always start with accessing the
document object.
Below are some examples of how you can use the document object to access and manipulate
HTML.
e.g1.
<html>
<body>
<script>
</script>
</body>
</html>
e.g.2
<h1>This is a Heading</h1>
<h1>This is a Heading</h1>
<script>
</script>
If a form field (fname) is empty, this function alerts a message, and returns false, to prevent the
form from being submitted:
Data Validation
Data validation is the process of ensuring that user input is clean, correct, and useful.
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.
e.g.
<html>
<body bgcolor="pink">
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<script>
function validation(){
var name=document.getElementById('uname').value;
var pass=document.getElementById('pwd').value;
var cpass=document.getElementById('cpwd').value;
var mobile=document.getElementById('mob').value;
var email=document.getElementById('email').value;
if(name=="")
alert("enter ur name");
if(!isNaN(name))
if(pass=="")
alert("enter ur name");
if(cpass!=pass){
if(isNaN(mobile)){
if(mobile.length!=10){
if(email.indexOf('@')<=0){
else
</script>
</body>
</html>
O/P:-