SlideShare a Scribd company logo
Introduction to Javascript
Web Development 101
Lesson 02.01
Java·script
Noun

An interpreted computer programming language. As part of
web browsers, implementations allow client-side scripts to
interact with the user, control the browser, communicate
asynchronously, and alter the document content that is
displayed.
DISCUSS
WHY AND HOW DOES JAVASCRIPT ENABLE GMAIL TO WORK?
Example 02.01.01
http://jsfiddle.net/crgwbr/Fqhy4/
Javascript clock
var writeTime = function () {	
var parent = document.getElementById('clock'),	
timeElem = document.createElement('p'),	
time = new Date();	
!

timeElem.innerHTML = time.toUTCString();	
parent.appendChild(timeElem);	
};	
!

writeTime();
Javascript clock
var writeTime = function () {	
var parent = document.getElementById('clock'),	
timeElem = document.createElement('p'),	
time = new Date();	
!

timeElem.innerHTML = time.toUTCString();	
parent.innerHTML = “”;	
parent.appendChild(timeElem);	
};	
!

setInterval(writeTime, 500);
The DOM API
var writeTime = function () {	
var parent = document.getElementById('clock'),	
timeElem = document.createElement('p'),	
time = new Date();	
!

timeElem.innerHTML = time.toUTCString();	
parent.innerHTML = “”;	
parent.appendChild(timeElem);	
};	
!

setInterval(writeTime, 500);
Javascript

[proper]
Data Types
String:	
var myString = “Hello World”;	
!
Number	
var myNum = 42;	
!
Array	
var myArr = [5, 6, “seven”, 8];	
!
Object	
var myObj = {	
key1: “Value”,	
key2: 42,	
anotherKey: myArr	
};
Statements & Expressions
An expression produces a value.

Can be written wherever a value is expected.

A value is in itself an expression.

A statement performs an action.

Statements manipulate interpreter flow and perform actions.

Wherever a statement is expected, you may write an Expression. The
opposite is not true.
Expressions
var f = function(x) {	
return (x * x) + 5; 	
};	
!
var a = f(1);	
var b = f(2 * 3);

f(x) → x2 + 5
!

f(1) ≍ 1
f(2 × 3) ≍ 36
Statements
var myStr = “Hello World”,	
i;	
!
for (i = 0; i < myStr.length; i++) {	
console.log(myStr[i]);	
}	

var myStr = “Hello World”,	
i = 0;	
!
while (i < myStr.length) {	
i++;	
console.log(myStr[i]);	
}
Statements
var comp = function(x, y) {	
var comp = function(x, y) {	
if (x > y) {	
if (x > y) {	
return 1;	
return 1;	
} else {	
} else if (x < y) {	
if (x < y) {	
return -1;	
return -1;	
}	
}	
!
}	
return 0;	
!
};
return 0;	
};
Getting Fancy
var random = (function() {	
return 4; // Verified random by dice roll	
}());
IIFE
Immediately Invoked Function Expression.
Why?
scope
The context within the program in which an identifier is valid and
can be resolved to find the entity associated with the identifier.
Javascript has lexical scoping nested at the
function level, with the global scope being the
outermost scope.
Global Scope is dangerous
myCoolApp.js	
!
var myFunc = function() {	
...	
};

someFancyPlugin.js	
!
var myFunc = function() {	
...	
};
Something more sane
myCoolApp.js	
!
(function() {	
var myFunc = function() {	
...	
};	
}());

someFancyPlugin.js	
!
(function() {	
var myFunc = function() {	
...	
};	
}());
Closure
A function together with a referencing environment—a table storing a reference to
each of the non-local variables of that function. A closure allows a function to
access non-local variables even when invoked outside its immediate lexical scope.
Closures
var increment = (function() {	
var i = 0, 	
inc;	
	
inc = function(step) { 	
i += step;	
};	
!
return inc;	
}());

>>> increment(1);	
1	
>>> increment(1);	
2	
>>> increment(5);	
7	
>>> i	
undefined
Closures
var counter = function(step) {	
var i = 0, 	
inc;	
!
inc = function() { 	
i += step;	
};	
!
return inc;	
};

>>>
>>>
5	
>>>
10	
>>>
15

var n = counter(5);	
n();	
n();	
n();
Brainstorm
I need to read the state of the counter without
incrementing it. How?
OOJS
var Counter = function(step) {	
var i = 0;	
!
this.inc = function() { 	
i += step;	
};	
!
this.get = function() {	
return i;	
}	
};

>>>
>>>
0	
>>>
5	
>>>
5

var n = new Counter(5);	
n.get();	
n.inc();	
n.get();
OOJS
var Counter = function(step) {	
this.i = 0;	
this.step = step;	
};	
!
Counter.prototype = {	
inc: function() { 	
this.i += this.step;	
},	
!
get: function() {	
return this.i;	
}	
};

>>>
>>>
0	
>>>
5	
>>>
5

var n = new Counter(5);	
n.get();	
n.inc();	
n.get();
Javascript

[DOM]
Requirements
We just wrote a counter object

Add a user interface

Current value should be displayed in the browser document

There should be a button to increment the count

There should be another button to reset the count
Example 02.01.02
http://jsfiddle.net/crgwbr/ynraf/
http://jsfiddle.net/crgwbr/ynraf/
Requirements Change
Page now needs to have support having n number of counters

Defaults to 3 counters

User can add another by clicking a button
Example 02.01.03
http://jsfiddle.net/crgwbr/UjF5n/
http://jsfiddle.net/crgwbr/UjF5n/
Requirements Change

User should be able to delete any counter on the page

User can name counters they add
Example 02.01.04
http://jsfiddle.net/crgwbr/ejnN2/
http://jsfiddle.net/crgwbr/ejnN2/
That’s a Web Application.
Review
Javascript is a general purpose,
interpreted language.


Access the outside world
through injected APIs


First-class functions

C-style syntax


DOM is an API for interacting
with the browser and it’s HTML
document


Prototypical Inheritance


Most UI code is event driven


Runs in a Virtual Machine


When used well, closures are
awesome
Homework
Read: Web Fundamental—Javascript

Watch: Javascript—The Good Parts

http://www.youtube.com/watch?v=hQVTIJBZook

More Related Content

PDF
Reactive, component 그리고 angular2
PDF
Functional Reactive Programming - RxSwift
PDF
NoSQL Injections in Node.js - The case of MongoDB
PPT
Ruby on Rails Intro
PDF
Reactive Programming Patterns with RxSwift
PDF
Security Challenges in Node.js
PDF
New Framework - ORM
Reactive, component 그리고 angular2
Functional Reactive Programming - RxSwift
NoSQL Injections in Node.js - The case of MongoDB
Ruby on Rails Intro
Reactive Programming Patterns with RxSwift
Security Challenges in Node.js
New Framework - ORM

What's hot (18)

PDF
Workshop 5: JavaScript testing
PPTX
C programming
PPTX
Avoiding callback hell in Node js using promises
PDF
Debugging JavaScript with Chrome
PDF
The evolution of java script asynchronous calls
PDF
To Err Is Human
PDF
RxJS 5 in Depth
PPTX
MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS
PDF
Finch.io - Purely Functional REST API with Finagle
PDF
JavaScript Promise
PDF
Cascadia.js: Don't Cross the Streams
ODP
Introduccion a Jasmin
PPTX
Advanced JavaScript Concepts
PDF
Swift & ReactiveX – Asynchronous Event-Based Funsies with RxSwift
PDF
Asynchronous programming done right - Node.js
PPTX
JavaScript - i och utanför webbläsaren (2010-03-03)
PDF
Practical JavaScript Promises
DOCX
Computer Science investigatory project class 12
Workshop 5: JavaScript testing
C programming
Avoiding callback hell in Node js using promises
Debugging JavaScript with Chrome
The evolution of java script asynchronous calls
To Err Is Human
RxJS 5 in Depth
MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS
Finch.io - Purely Functional REST API with Finagle
JavaScript Promise
Cascadia.js: Don't Cross the Streams
Introduccion a Jasmin
Advanced JavaScript Concepts
Swift & ReactiveX – Asynchronous Event-Based Funsies with RxSwift
Asynchronous programming done right - Node.js
JavaScript - i och utanför webbläsaren (2010-03-03)
Practical JavaScript Promises
Computer Science investigatory project class 12
Ad

Similar to 02 Introduction to Javascript (20)

KEY
The Return of JavaScript: 3 Open-Source Projects that are driving JavaScript'...
PDF
Understanding Asynchronous JavaScript
PDF
Avinash Kundaliya: Javascript and WordPress
PPTX
Intro to Javascript
PPTX
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
PDF
Bonnes pratiques de développement avec Node js
PDF
JavaScript Interview Questions 2023
ZIP
Javascript Everywhere
PPTX
JavaScript Multithread or Single Thread.pptx
PPT
JavaScript Misunderstood
PPT
Expert JavaScript tricks of the masters
PDF
Apache Commons - Don\'t re-invent the wheel
PPTX
1-JAVA SCRIPT. servere-side applications vs client side applications
PDF
A Journey with React
PDF
Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...
PDF
Java script Examples by Som
PDF
Performance patterns
PDF
04 Advanced Javascript
PPTX
Apache Spark in your likeness - low and high level customization
PDF
mobl
The Return of JavaScript: 3 Open-Source Projects that are driving JavaScript'...
Understanding Asynchronous JavaScript
Avinash Kundaliya: Javascript and WordPress
Intro to Javascript
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Bonnes pratiques de développement avec Node js
JavaScript Interview Questions 2023
Javascript Everywhere
JavaScript Multithread or Single Thread.pptx
JavaScript Misunderstood
Expert JavaScript tricks of the masters
Apache Commons - Don\'t re-invent the wheel
1-JAVA SCRIPT. servere-side applications vs client side applications
A Journey with React
Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...
Java script Examples by Som
Performance patterns
04 Advanced Javascript
Apache Spark in your likeness - low and high level customization
mobl
Ad

More from crgwbr (7)

PDF
Lightning Talk: Making JS better with Browserify
PDF
07 Integration Project Part 1
PDF
06 Map Reduce
PDF
05 Web Services
PDF
03 Web Events and jQuery
PDF
01 Introduction To CSS
PDF
08 Integration Project Part 2
Lightning Talk: Making JS better with Browserify
07 Integration Project Part 1
06 Map Reduce
05 Web Services
03 Web Events and jQuery
01 Introduction To CSS
08 Integration Project Part 2

Recently uploaded (20)

PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
A Presentation on Artificial Intelligence
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
Encapsulation theory and applications.pdf
PPTX
Tartificialntelligence_presentation.pptx
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PPTX
Machine Learning_overview_presentation.pptx
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
1. Introduction to Computer Programming.pptx
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PPTX
Spectroscopy.pptx food analysis technology
Group 1 Presentation -Planning and Decision Making .pptx
Empathic Computing: Creating Shared Understanding
Digital-Transformation-Roadmap-for-Companies.pptx
A Presentation on Artificial Intelligence
Heart disease approach using modified random forest and particle swarm optimi...
Encapsulation theory and applications.pdf
Tartificialntelligence_presentation.pptx
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
SOPHOS-XG Firewall Administrator PPT.pptx
Machine Learning_overview_presentation.pptx
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
OMC Textile Division Presentation 2021.pptx
NewMind AI Weekly Chronicles - August'25-Week II
A comparative analysis of optical character recognition models for extracting...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Unlocking AI with Model Context Protocol (MCP)
1. Introduction to Computer Programming.pptx
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Spectroscopy.pptx food analysis technology

02 Introduction to Javascript