Introducing JavaScript
Introducing JavaScript
com
Engaging Peers, Inspiring Careers!
• Why JavaScript?
• Statements
• Blocks
• Comments
• Datatypes
• Variables
• Expressions
• Flow control
• Arrays
• Functions
What is javascript?
• JavaScript is a client – side scripting language for the world wide web, that
is similar to the syntax of the Java programming language.
• The decrease in traffic from constant interaction with the server can also
improve a server's performance.
• Because the local machine is doing the script processing, the user can
view web pages much faster
Introducing JavaScript Syntax
• A simple JavaScript program:
<html>
<head>
</head>
<body>
<script language=“JavaScript”>
</script>
</body>
</html>
Statements
• It is simply a line of code that contains some kind of instructions.
• Example
Blocks
}
Comments
• A single line comment is denoted with two slashes ( // )
/*
some text
*/
Data Types
• There are 5 basic data types in JavaScript: string, number, Boolean, object
and function.
Variables
• Variables are "containers" for storing information.
Rules for JavaScript variable names:
• Variable names are case sensitive (y and Y are two different variables)
• Variable names must begin with a letter or the underscore character
Declaring (Creating) JavaScript Variables
• Creating variables in JavaScript is most often referred to as "declaring"
variables.
• You can declare JavaScript variables with the var statement:
var x;
var carname;
• After the declaration shown above, the variables are empty (they have no
values yet).
• However, you can also assign values to the variables when you declare
them:
var x=5;
var carname="Volvo";
Expressions
• The methods that can be employed to manipulate the data are called
expressions.
Numerical expressions
• Logical Operators
&& - And
|| - Or
! - Not
== - Equal
!= - Not Equal
> - Greater than
>=
<
<=
If Statement
• Use the if statement to execute some code only if a specified condition is
true.
Syntax
if (condition)
{
code to be executed if condition is true
}
Example
<script type="text/javascript">
//Write a "Good morning" greeting if
//the time is less than 10
var d=new Date();
var time=d.getHours();
if (time<10)
{
document.write("<b>Good morning</b>");
}
</script>
If … Else Statement
• Use the if....else statement to execute some code if a condition is true and
another code if the condition is not true.
• Syntax
if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is not true
}
If...else Statement
• Example
• <script type="text/javascript">
//If the time is less than 10, you will get a "Good morning" greeting.
//Otherwise you will get a "Good day" greeting.
Shorter form:
• You can access an array element by referring to the name of the array and
the element's index number.
document.write(Exforsys[1])
Functions
• A function is simply a block of code with a name, which allows the block of
code to be called by other components in the scripts to perform certain
tasks.
• Functions can also accept parameters that they use complete their task.
…………………………………………
//Block of Code
…………………………………………
}
Creating Custom Functions
Calling functions
• There are two common ways to call a function: From an event handler and
from another function.
• Calling a function is simple. You have to specify its name followed by the
pair of parenthesis.
<SCRIPT TYPE="TEXT/JAVASCRIPT">
name_of_function(argument1,argument2,…arguments)
</SCRIPT>
Creating Custom Functions
<html> <head> <title>Henley's Department Store</title>
<Script Language="JavaScript">
function welcomeMessage()
{ document.write("Welcome to Henley's Department Store!"); }
</Script>
</head> <body>
<h1>Henley's Department Store</h1>
<h3>Customers Order Processing</h3>
<Script Language="JavaScript">
welcomeMessage();
</Script>
</body> </html>
FaaDoOEngineers.com
Engaging Peers, Inspiring Careers!