Professional Documents
Culture Documents
4lab - Webdev - Javascript
4lab - Webdev - Javascript
DEVELOPMENT
(JAVASCRIPT)
By: Vera A. Panaguiton
OUTLINE
WHAT IS JAVASCRIPT?
VARIABLES
CONDITIONALS
FUNCTIONS
OBJECTS
DOM
AJAX
JQUERY
APDEV 1 by Vera A. Panaguiton 2
WHAT IS A JAVASCRIPT?
JavaScript is a lightweight, cross-platform and
interpreted programming language with object oriented
capabilities.
It is originally called LiveScript and was developed by
Brendan Eich at Netscape in 1995 and was shipped with
Netscape Navigator 2.0 beta releases.
It is well-known for the development of web pages, many
non-browser environments also use it. JavaScript can be
used for Client-side developments as well as Server-
side developments.
JavaScript syntax is similar to C, Perl, and Java; for example,
if statements, while and for loops are almost identical.
APDEV 1 by Vera A. Panaguiton 3
PLATFORMS FOR JAVASCRIPT
Web Development
Adding interactivity and behavior to static sites. (Use AngularJS)
Web Applications
With technology, browsers have improved to the extent that a language was required to create robust web
applications. When we explore a map in Google Maps then we only need to click and drag the mouse. All
detailed view is just a click away. It uses Application Programming Interfaces(APIs) that provide extra power to
the code. (Electron and React is helpful in this department)
Server Applications
JavaScript made its way from client to server with the help of node.js, the most powerful in the server-side.
Games
JavaScript also helps in creating games for leisure. The combination of JavaScript and HTML 5 makes JavaScript
popular in game development as well. (Use EaseJS library which provides solutions for working with rich
graphics)
Smartwatches
JavaScript is being used in all possible devices and applications. This framework works for applications that
require the internet for its functioning. (Use PebbleJS library which is used in smartwatch applications).
Art
Artists and designers can create whatever they want using JavaScript to draw on HTML 5 canvas, make sound
more effective also can be used p5.js library.
Machine Learning
This JavaScript ml5.js library can be used inAPDEV
web development
1 by Vera A. Panaguiton by using machine learning. 4
JAVASCRIPT FRAMEWORKS & LIBRARIES
Angular Ember.js
React Meteor
jQuery Mithril
Node.js Polymer
Vue.js Aurelia
Ext.js Backbone.js
APDEV 1 by Vera A. Panaguiton 5
The <script> tag
JavaScript can be placed in the body and head sections
of an HTML page with the help of the <script> tag.
The <script> tag contains javascript code which is
automatically executed when the browser processes the
tag.
The optional type=“text/javascript” is not needed in
HTML5 because it is the default type.
<script> </script>
APDEV 1 by Vera A. Panaguiton 6
How to insert javascript.
Inside <head></head>
CODE OUTPUT
<script src=“js/scriptfile.js”></script>
APDEV 1 by Vera A. Panaguiton 10
JAVASCRIPT EXAMPLE : EXTERNAL FILE
CODES
OUTPUT
OBJECTS
Entities that typically represents elements of an HTML page.
JavaScript objects are written with curly braces with properties written as
name:value pairs, separated by commas.
Most important datatype and forms the building blocks for modern JavaScript
DYNAMIC TYPES
JavaScript has dynamic types – this means that the same variable can be used as
different types
Note: The typeof operator is used to find the type of a JavaScript variable.
APDEV 1 by Vera A. Panaguiton 13
Converting Between Data Types
There are a number of techniques for converting
between data types. To convert from a string several
parse and other functions are available.
Note: Semi-colons are optional in JavaScript because of ASI (Automatic Semicolon Insertion) but omitting them can
lead to dire consequences on rare occasions. APDEV 1 by Vera A. Panaguiton 20
OPERATORS
Arithmetic Operators :
+, -, *, /, %, ++, --
Comparison (Relational) Operators :
==, !=, >, <, >=, <=
Logical Operators :
&&, ||, !
Assignment Operators:
=, +=, -=, *=, /=, %=
Note: Addition operator (+) works for Numeric as well as Strings. e.g. "a" + 10 will give "a10".
APDEV 1 by Vera A. Panaguiton 21
CONDITIONAL STATEMENTS
JavaScript supports conditional statements which are used
to perform different actions based on different conditions.
if
if else
if else if
switch
while
do while
for
Note: If you already know Java, then this is very easy for you.
APDEV 1 by Vera A. Panaguiton 22
Comments
JavaScript uses two types of comments:
single-line comments and multiline comments.
A function consists of the function keyword followed by the name of the function , a set
of open and close parentheses enclosing an optional parameter list and a body
enclosed in a set of curly braces.
function functionName (parameterList)
{
// body
}
The code inside the function will execute when something invokes the function: when
an event occurs, when it is invoked from JavaScript code, or automatically (self-invoked).
JavaScript has a set of its own core objects that allow you to manipulate strings, numbers,
functions, dates, and so on, and JavaScript allows you to create your own objects.
cat.play();
bird.fly();
dog.fetch("ball");
APDEV 1 by Vera A. Panaguiton 38
OBJECT example
Base on the example below, the calculate property is assigned an inline or anonymous function. Because it is
part of the definition of the Distance() constructor, only objects of the Distance class will have access to the
method, thereby encapsulating the method.
The value assigned to a property can be of any data type, including array literals and object literals. An
object literal is both simple and logical, but the biggest short-coming is that the code is not reusable; that
is, you would have to retype the code to use it again within the program, whereas you can use a
constructor function to create multiple instances of an object.
var object = { property1: value, property2: value };
var area =APDEV
{ length: 15, width: 5 };
1 by Vera A. Panaguiton 42
Object Literals: exaple
Note: Make
APDEV 1 by Vera A. Panaguiton sure to check the pairing of your curly 43
braces.
For/in loop
JavaScript provides the for/in loop, which can be used to iterate through
a list of object properties or array elements.
The for/in loop reads: for each property in an object (or for each element
in an array) get the name of each property (element), in turn, and for
each of the properties (elements), execute the statements in the block
that follows.
The for/in loop is a convenient mechanism for looping through the
properties of an object.
Normally the id and name attribute should match but this is not always possible. For example, when
creating radio buttons, one name suffices for all the buttons, because only one of the buttons can be
selected, but if you use the id attribute, each id has to be unique as shown here:
AJAX allows web pages to be updated asynchronously by exchanging data with a web server
behind the scenes. This means that it is possible to update parts of a web page, without
reloading the whole page.
Some examples of Ajax applications are Flickr for photo storage and display, Gmail, Google
Suggest, and perhaps the best example, Google Maps.
The jQuery syntax is tailor made for selecting HTML elements and performing some
action on the element(s).
$(document).ready(function(){ <script>
// jQuery methods go here... $(document).ready(function(){
$("button").click(function(){
}); $("#test").hide();
});
shorter method });
</script>
$(function(){
// jQuery methods go here... <p id="test">hide me.</p>
}); <button>Click me</button>
click submit
mouseenter keyup
mouseleave focus
keypress hover