0% found this document useful (0 votes)
11 views48 pages

JavaScript (Day 1)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views48 pages

JavaScript (Day 1)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

Hidaya Institute of

Science &
Technology
www.histpk.org
A Division of Hidaya Trust, Pakistan

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


JAVASCRIPT

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


CONTENTS
• What You Need To Know
• JavaScript
• Introduction
• History
• What JavaScript Can And Cannot Do
• What Needed To Write A JavaScript
• How And Where To Place JavaScript
• Referencing An External File
• Whitespace And Line Breaks
• Semicolon
• Case Sensitivity
• Comments In JavaScript
• Operators
© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
WHAT YOU NEED TO KNOW?
You need to know basic html and css.
Html :Hypertext Markup Language.
• Content Structure
• What your headline
• How many divisions you have in your page
• How many paragraphs
• What are contents of paragraphs.
Css: Style Sheet For Presentation
• What’s the color and style of font
• The Width of paragraphs
• Background color of page

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


A Programming Language, Behavior and Interactivity with the
page.
What happens when you move mouse over menu.
What happens when a user types in an invalid or wrong value in
textfield.
How Long a photo Slide Show takes to move from one image to
other.

JAVASCRIPT!
© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
INTRODUCTION
• JavaScript is an interpreted programming language with object-
oriented (OO) capabilities.

• JavaScript is a scripting language you can use — in conjunction with


HTML — to create interactive Web pages.

• Syntactically, the core JavaScript language resembles C, C++, and


Java, with programming constructs such as the if statement, the
while loop, and the && operator.

• JavaScript is a loosely typed language, which means that variables


do not need to have a type specified.

• It is commonly called client-side JavaScript to emphasize that scripts


are run by the client computer rather than the web server.

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


HISTORY
• JavaScript was originally developed by Brendan Eich of Netscape
under the name Mocha.
• Later renamed to LiveScript.
• Finally to JavaScript mainly because it was more influenced by
the Java programming language.
• JavaScript very quickly gained widespread success as a client-side
scripting language for web pages.
• As a consequence, Microsoft named its implementation JScript to
avoid trademark issues.
•  JScript was included in Internet Explorer 3.0, released in August
1996.
• In November 1996, Netscape announced that it had submitted
JavaScript to Ecma International for consideration as an industry
standard, and subsequent work resulted in the standardized version
named ECMAScript.
© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
HISTORY

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


JAVASCRIPT
•JavaScript is simply a scripting language.
•Cannot write a desktop application
•JavaScript is limited
•Cannot access local files directly
•Cannot directly access database
•Cannot access hardware (As Usb, etc)
•JavaScript was designed to manipulate webpages

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


WHAT JAVASCRIPT CAN DO?
•JavaScript gives HTML designers a programming tool
•JavaScript can put dynamic text into an HTML page
•JavaScript can react to events
•JavaScript can read and write HTML elements
•JavaScript can be used to validate input data

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


WHAT NEEDED TO WRITE A JAVASCRIPT
•It Doesn't matter it be
• Pc
• Mac,
• Linux
•What ever language it be,
• Php
• ruby on rails
• Asp.net
•No Licensing is need.
•Some application may help in writing code.
•But basically will work on a simple Text Editor.

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


HOW AND WHERE TO PLACE JAVASCRIPT
•JavaScript consists of JavaScript statements that are placed within the
<script>... </script> HTML tags in a web page.
•The <script> tag alert the browser program to begin interpreting
all the text between these tags as a script.
• The script tag takes two important attributes:
•language:
• This attribute specifies what scripting language you are using.
• Typically, its value will be “javascript”.
•type:
• This attribute is what is now recommended to indicate the scripting
language in use
• its value should be set to "text/javascript".
<script language="javascript" type="text/javascript">
JavaScript code
</script>
© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
HOW AND WHERE TO PLACE JAVASCRIPT
You Can place Script tag in
head section: The browser interprets from top to bottom and head
section is read first than the body. So will do some javascript work and
then will show up content of body
body section:
Start: It will execute right after head.
Middle: It will in middle of code
End: Right at the end.
• It depends on condition that you want a page to do
processing before the content is rendered. Creating some
dynamic content, then should do in Head Section.
• There arise a need to process some elements in body then go
ahead writing to body. Beware when code increases.
• It could be the case you may load external javascript code as
in css.

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


HOW AND WHERE TO PLACE JAVASCRIPT
<html>
<head>
<script>
alert("head");
</script>
</head>
<body>
<script> alert("start"); </script>
<p>THis is text Wow</p>

<p>some what in middel 1</p>


<p>some what in middel 2</p>
<script> alert("middle"); </script>
<p>some what in midde 3</p>
<p>some what in midde4</p>
<p>finally end</p>
<script>alert("end");</script>
</body>
</html>

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


REFERENCING AN EXTERNAL FILE
Page1.html alert(“Hello World”);
<html>
<head>
<script src=“my.js”>
</script>
</head>
<body>
<p> This is Simple Text </p>
</body>
</html>
My.js
© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
WHITESPACE AND LINE BREAKS
•JavaScript ignores
• spaces,
• tabs,
• newlines.
•Because you can use spaces, tabs, and newlines freely in your
programs, you are free to format and indent your programs in a
neat and consistent way that makes the code easy to read and
understand.

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


OPTIONAL SEMILCOLONS
Simple statements in JavaScript are generally followed by
semicolons (;), just as they are in C, C++, and Java.
The semicolon serves to separate statements from each other.
In JavaScript, however, you may omit the semicolon if each of
your statements is placed on a separate line.

<script language="javascript" type="text/javascript">


statement1
statement2
</script>

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


CASE SENSITIVITY
•JavaScript is a case-sensitive language.
•This means that
• language keywords,
• variables,
• function names,
must always be typed with a consistent capitalization of
letters.
•So identifiers Test, TeSt and TEST will have different meanings
in JavaScript.
•Proper Care should be taken while writing your variable and
function names in JavaScript.

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


COMMENTS IN JAVASCRIPT
•JavaScript supports both C-style and C++ style comments
•Any text between a // and the end of a line is treated as a comment
and is ignored by JavaScript.
•Any text between the characters /* and */ is treated as a
comment. This may span multiple lines.
•JavaScript also recognizes the HTML comment opening sequence
• <!-- JavaScript treats
•This as a single-line comment, just as it does the // comment.

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


NO SCRIPT TAG
• The noscript element is the • paragraphs,
converse of script . • lists,
• A JavaScript-aware browser will • divisions,
render the con-tents of the • headings, and so on.
noscript block if scripting has • The conventional use of noscript
been turned off or if the browser is to present HTML content to
doesn’t support a scripting display in the absence of
language invoked by a script scripting support — a message
element in the document. such as, ‘‘This page would be
• A noscript block will therefore be more interesting if viewed with
rendered by all browsers when JavaScript running,’’ for
scripting is not supported. example, or an HTML hyperlink
to a page that provides content
• You can display any standard that would otherwise be inserted
HTML within the noscript tag set. by Javascript.
It is a block-level element that
can contain • <noscript> This page would be
more interesting if viewed with
JavaScript running </noscript>
© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
VARIABLES
In all programs we need to keep track of all kinds of data as
email, date of birth and etc.
We create variables for the above purpose.
Variable is a container grabbing a piece of memory and giving it
a name so we can use it in javascript.
We create varibale with the word var.
E.G
var value;
var name;

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


JAVASCRIPT VARIABLE SCOPE
•The scope of a variable is the region of your program in which it
is defined. JavaScript variable will have only two scopes.
•Global Variables: A global variable has global scope
which means it is defined everywhere in your JavaScript
code.
•Local Variables: A local variable will be visible only
within a function where it is defined. Function parameters
are always local to that function.

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


JAVASCRIPT VARIABLE NAMES
•While naming your variables in JavaScript keep following rules
in mind.
•You should not use any of the JavaScript reserved
keyword as variable name.
•For example, break or boolean variable names are not valid.
•JavaScript variable names should not start with a numeral (0-
9). They must begin with A letter or the underscore character.
• For example, 123test is an invalid variable name but _123test
is a valid one.
•JavaScript variable names are case sensitive. For example,
Name and name are two different variables.

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


JAVASCRIPT DATATYPES
•One of the most fundamental characteristics of a programming
language is the set of data types it supports.
•JavaScript allows you to work with three basic data types:
• Numbers eg. 123, 120.50 etc.
• Strings of text e.g. "This text string" etc.
• Boolean e.g. true or false.
•JavaScript also defines two trivial data types, null and undefined, each
of which defines only a single value.
•In addition to these data types, JavaScript supports a composite data
type known as object.

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


THE TYPEOF OPERATOR
•The typeof is a unary operator that is placed before its single
operand, which can be of any type. Its value is a string
indicating the data type of the operand.
•The typeof operator evaluates to "number", "string", or
"boolean" if its operand is a number, string, or boolean value
and returns true or false based on the evaluation.
Type String Returned by typeof
Number "number"
String "string"
Boolean "boolean"
Object "object"
Function "function"
Undefined "undefined"
Null "object"
© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
OPERATORS
•Using expression 4 + 5 is equal to 9.
• Here 4 and 5 are called operands and + is called operator.
•JavaScript language supports following type of operators.
•Arithmetic Operators
•Comparision Operators
•Logical (or Relational) Operators
•Assignment Operators
•Conditional (or ternary) Operators

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


The Arithmetic Operators
There are following arithmetic operators supported by JavaScript
language:
Assume variable A holds 10 and variable B holds 20 then:
Operator Description Example

+ Adds two operands A + B will give 30


- Subtracts second operand from A - B will give -10
the first
* Multiply both operands A * B will give 200
/ Divide numerator by denumerator B / A will give 2

% Modulus Operator and remainder B % A will give 0


of after an integer division
++ Increment operator, increases A++ will give 11
integer value by one
-- Decrement operator, decreases A-- will give 9
integer value by one

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


THE COMPARISON OPERATORS
There are following comparison operators supported by
JavaScript language
Assume variable A holds 10 and variable B holds 20 then:
Operator Description Example

== Checks if the value of two operands are equal or not, if (A == B) is not true.
yes then condition becomes true.
!= Checks if the value of two operands are equal or not, if (A != B) is true.
values are not equal then condition becomes true.

> Checks if the value of left operand is greater than the (A > B) is not true.
value of right operand, if yes then condition becomes true.

< Checks if the value of left operand is less than the value of (A < B) is true.
right operand, if yes then condition becomes true.

>= Checks if the value of left operand is greater than or equal (A >= B) is not true.
to the value of right operand, if yes then condition
becomes true.

<= Checks if the value of left operand is less than or equal to (A <= B) is true.
the value of right operand, if yes then condition becomes
true.

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


THE LOGICAL OPERATORS
•There are following logical operators supported by JavaScript
language
•Assume variable A holds 10 and variable B holds 20 then:
Operator Description Example

&& Called Logical AND operator. If both (A && B) is true.


the operands are non zero then then
condition becomes true.

|| Called Logical OR Operator. If any of (A || B) is true.


the two operands are non zero then
then condition becomes true.

! Called Logical NOT Operator. Use to !(A && B) is false.


reverses the logical state of its
operand. If a condition is true then
Logical NOT operator will make
false.

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


THE ASSIGNMENT OPERATORS
Operator Description Example

= Simple assignment operator, Assigns values from C = A + B will assigne value of A +


right side operands to left side operand B into C

+= Add AND assignment operator, It adds right operand C += A is equivalent to C = C + A


to the left operand and assign the result to left
operand
-= Subtract AND assignment operator, It subtracts right C -= A is equivalent to C = C - A
operand from the left operand and assign the result
to left operand
*= Multiply AND assignment operator, It multiplies right C *= A is equivalent to C = C * A
operand with the left operand and assign the result
to left operand
/= Divide AND assignment operator, It divides left C /= A is equivalent to C = C / A
operand with the right operand and assign the result
to left operand
%= Modulus AND assignment operator, It takes modulus C %= A is equivalent to C = C % A
using two operands and assign the result to left
operand

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


THE CONDITIONAL OPERATOR (? :)

Operator Description Example

?: Conditional If Condition is true ? Then


Expression value X : Otherwise value Y

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


CONDITIONAL STATEMENTS
•Conditional statements that allow your program to make correct
decisions and perform right actions.
•JavaScript supports following forms of if..else statement:
• if statement
• if...else statement
• if...else if... statement.

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


IF STATEMENT
•The if statement is the fundamental control statement that allows
JavaScript to make decisions, or, more precisely, to execute statements
conditionally.
•This statement has two forms. The first is:
• if (expression)
• statement
•In this form, expression is evaluated. If the resulting value is true,
statement is exe-cuted. If expression is false, statement is not
executed.
•Note that the parentheses around the expression are a required part of
the syntax for the if statement.
•JavaScript syntax requires a single statement after the if keyword and
parenthesized expression, but you can use a statement block to
combine multiple statements into one.

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


IF STATEMENT
So the if statement might also look like this:
if (expression)
{
statement;
.
.
statement (n);
}

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


IF ELSE STATEMENT
•The second form of the if statement introduces an else
clause that is executed when expression is false. Its
syntax is:
if (expression)
statement1
else
statement2
•This form of the statement executes statement1 if
expression is true and executes statement2 if expression
is false.

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


ELSE IF STATEMENT
•The if...else if... statement is the one level advance form of control statement
that allows JavaScript to make correct decision out of several conditions.
if (expression 1)
{
Statement(s) to be executed if expression 1 is true
}
else if (expression 2)
{
Statement(s) to be executed if expression 2 is true
}
else if (expression 3)
{
Statement(s) to be executed if expression 3 is true
}
else{
Statement(s) to be executed if no expression is true
}

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


ELSE IF
•There is nothing special about this code. It is just a series
of if statements, where each if is part of the else clause of the
previous statement. Statement(s) are executed based on the
true condition, if non of the condition is true then else block is
executed.

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


SWITCH
•You can use multiple if...else if statements, as in the previously learnt,
to perform a multi way branch.
•However, this is not always the best solution, especially when all of the
branches depend on the value of a single variable.
•You can use a switch statement which handles exactly this situation,
and it does so more efficiently than repeated if...else if statements.

The basic syntax of the switch statement is to give an expression to


evaluate
and
several different statements to execute based on the value of the
expression.
•The interpreter checks each case against the value of the expression
until a match is found. If nothing matches, a default condition will be
used.
© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
SWITCH
switch (expression)
{
case condition 1: statement(s) break;
case condition 2: statement(s) break;
...
case condition n: statement(s) break;
default: statement(s)

}
•The break statements indicate to the interpreter the end of that
particular case. If they were omitted, the interpreter would continue
executing each statement in each of the following cases.

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


LOOPS
•The looping statements are those that bend that path back
upon itself to repeat portions of your code.
•While writing a program, there may be a situation when you
need to perform some action over and over again. In such
situation you would need to write loop statements to reduce the
number of lines.
•JavaScript has four looping statements:
1. while
2. do/while
3. for
4. for/in.

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


WHILE
The purpose of a while loop is to execute a statement or code
block repeatedly as long asexpression is true. Once expression
becomes false, the loop will be exited.
while (expression)
{
Statement(s) to be executed if expression is true
}

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


DO WHILE
•The do...while loop is similar to the while loop except that the
condition check happens at the end of the loop. This means that
the loop will always be executed at least once, even if the
condition is false.
Do
{
Statement(s) to be executed;
}
while (expression);

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


FOR LOOP
The for loop is the most compact form of looping and includes the
following three important parts:
• The loop initialization where we initialize our counter to a starting
value. The initialization statement is executed before the loop
begins.
• The test statement which will test if the given condition is true or
not. If condition is true then code given inside the loop will be
executed otherwise loop will come out.
• The iteration statement where you can increase or decrease your
counter.
• for (initialization; test condition; iteration statement)
• {
• Statement(s) to be executed if test condition is true
• }

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


LOOP CONTROL
•To handle all such situations, JavaScript
provides break and continue statements. These statements are used
to immediately come out of any loop or to start the next iteration of any
loop respectively.

The break Statement:
•The break statement, which was briefly introduced with the switch statement, is
used to exit a loop early, breaking out of the enclosing curly braces.

The continue Statement:
•The continue statement tells the interpreter to immediately start the next
iteration of the loop and skip remaining code block.
•When a continue statement is encountered, program flow will move to the loop
check expression immediately and if condition remain true then it start next
iteration otherwise control comes out of the loop.

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


JAVASCRIPT PARSEFLOAT() FUNCTION
The parseFloat() function parses a string and returns a floating
point number.

This function determines if the first character in the specified


string is a number. If it is, it parses the string until it reaches
the end of the number, and returns the number as a number,
not as a string.

Note: Only the first number in the string is returned!


Note: Leading and trailing spaces are allowed.
Note: If the first character cannot be converted to a number,
parseFloat() returns NaN.

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


JAVASCRIPT PARSEINT() FUNCTION

The parseInt() function parses a string and returns an integer.

Note: Only the first number in the string is returned!


Note: Leading and trailing spaces are allowed.
Note: If the first character cannot be converted to a number,
parseInt() returns NaN.

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


JAVASCRIPT NUMBER() FUNCTION

Convert different object values to their numbers.

The Number() function converts the object argument to a


number that represents the object's value.

If the value cannot be converted to a legal number, NaN is


returned.

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org


HTML DOM CONSOLE.LOG() METHOD

The console.log() method writes a message to the console.


The console is useful for testing purposes.
Tip: When testing this method, be sure to have the console view
visible (press F12 to view the console).

© Copyright 2017 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org

You might also like