Javascrpt_control statement__array_function_string
Javascrpt_control statement__array_function_string
Javascrpt_control statement__array_function_string
Operators,String, Array,Function
Narendra Mohan
Assistant Professor
Department of Computer Engineering and Applications
GLA University Mathura
What is JavaScript ?
• Introduced by Brendan Eich, member of Netscape
company in 1995
• cross-platform object based scripting language
for client and server applications
• Javascript is a dynamic computer programming
language.
• It is lightweight and most commonly used as a
part of web pages, whose implementations allow
client-side script to interact with the user and
make dynamic pages.
• It is an interpreted programming language with
object-oriented capabilities.
Advantages of JavaScript
document.write('This is my first
JavaScript Page');
</script>
</body>
</html>
JavaScript Statements
<html>
<head><title>My Page</title></head>
<body>
<script language=“JavaScript">
document.write('<h1>This is my first
JavaScript Page</h1>');
HTML written
</script> inside JavaScript
</body>
</html>
JavaScript Statements
<html>
<head><title>My Page</title></head>
<body>
<p>
<a href="myfile.html">My Page</a>
<br />
<a href="myfile.html"
onMouseover="window.alert('Hello');">
My Page</A>
</p>
JavaScript written
</body> An Event
inside HTML
</html>
Example Statements
<script language="JavaScript">
window.prompt('Enter your
name:',''); Another event
</script>
<form>
<input type="button" Value="Press"
onClick="window.alert('Hello');">
Note quotes: " and '
</form>
External JavaScript
External scripts are practical when the same code is
used in many different web pages.
JavaScript files have the file extension .js.
<html> <head>
<script type="text/javascript">
function getValue(){
var retVal = prompt("Enter your name : ", "your name here");
document.write("You have entered : " + retVal);
} </script> </head>
<body>
<p>Click the following button to see the result: </p>
<form>
<input type="button" value="Click Me" onclick="getValue();" />
</form>
</body></html>
Data Types:
• Primitive Types: The three primitive types, Boolean,
Number and String, are the simple building blocks of all
data in JavaScript. Each of the three primitive types has a
corresponding object that provides additional functionality.
• Boolean: has only two values(True and False), case
sensitive. (Note that null, undefined, or an empty string, is
interpreted as false and any other value is considered true.)
• Number: contain both floating-point numbers and integers
In fact, all numbers in JavaScript are stored as floating point
numbers even though we may treat them as integers.
• String: A string is a row (not an array) of single characters.
There are a few special characters, known as escape
sequences
Escape Sequence
• Escape Sequence Produces
\b Backspace
\t Horizontal tab
\n Line feed (new line)
\f Form feed
\” Double quote
\’ Single quote
\\ Backslash
Other Core Data Type:
• JavaScript also defines a number of other core types, Null, Undefined and
Object
Null and Undefined:
• JavaScript’s null value means that there is no data or known value, and it is
used as a placeholder in a variable to let us know there’s nothing useful in
there.
• Undefined is the value held by a declared variable that has not been
initialized to a value, or not yet set to hold any data. Although a newly
declared variable holds no data, it does not hold a null value, but instead
undefined.
Object:
• The real functionality of JavaScript comes from its native objects. While
the core of the language so far has allowed us to store data and change
the flow of code, native objects allow us to not just store a string, but also
manipulate, search and change it.
Variables:
• can be assigned to any data type without the need to first declare the
type, declare it with the word var. must start with either a letter or the
underscore character. Variable names are usually case sensitive in most
browsers.
Keywords in javascript
• Write 20 keywords
Variable
• Can be assigned to any data type without the
need to first declare the type
• Declared with var keyword
• Start with either a letter or the underscore
character
operators
Flow control statements
If else statements: 3. If (condition)
{
1. If (condition) statements
{ }
statements else if (another condition)
} {
statements
2. If (condition) }
{ else
statements {
} statements
else }
{
statements
}
While Loop
The while statement repeats a statement or block based on a condition. It syntax is:
while (condition)
{
statements or block
}
Eg
num=0;
while(true)
{
document.write(++num +’ ‘);
if (num>20)
{
break;
}
}
Do while
• the statement or block is always executed at least once
syntax
do
{
statements
}while (condition)
Example
num=0;
do{
document.write(++num +’ ‘);
if (num>20)
{
break;
}
} while(true)
For loop
for (initialize; condition; change)
{
statements
}
Switch statement
Break and Continue
Switch()
{
Case ‘add’:
var sum;
sum= a+b;
alert(sum);
Break;
//document.body.style.backgroundColor="red";
}
function sub()
{
var a =prompt(“Enter the first number”);
var sub1;
a = parseInt(a);
sub1= a-b;
alert(sub1);
//document.body.style.backgroundColor="yellow";
}
</script></head>
<body> <form>
<input type="button" onclick="()" value=“add">
<input type="button" onclick="stp()" value="sub">
</form></body></html>
Blinking of text
<html><head>
<script type = "text/javascript">
function blink()
{
document.getElementById("t1").innerHTML= "";
setTimeout("appear()",500);
}
function appear()
{
document.getElementById("t1").innerHTML= "Hello World";
setTimeout("blink()",500);
}
</script></head>
<body onload="blink()">
<p id= "t1"> Hello World </p>
</body></html>
Example – Text hiding and showing
<html><head>
<script type="text/javascript">
function demoHide()
{
document.getElementById("p2").style.visibility="hidden";
document.body.style.backgroundColor = "red";
}
function demoShow()
{
document.getElementById("p2").style.visibility="visible";
document.body.style.backgroundColor = "yellow";