SlideShare a Scribd company logo
1 of 38
Download to read offline
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://jsddle.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 identier is valid and
can be resolved to nd the entity associated with the identier.
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://jsddle.net/crgwbr/ynraf/
http://jsddle.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://jsddle.net/crgwbr/UjF5n/
http://jsddle.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://jsddle.net/crgwbr/ejnN2/
http://jsddle.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

What's hot

What's hot (18)

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

Similar to 02 Introduction to Javascript

Avinash Kundaliya: Javascript and WordPress
Avinash Kundaliya: Javascript and WordPressAvinash Kundaliya: Javascript and WordPress
Avinash Kundaliya: Javascript and WordPress
wpnepal
 

Similar to 02 Introduction to Javascript (20)

The Return of JavaScript: 3 Open-Source Projects that are driving JavaScript'...
The Return of JavaScript: 3 Open-Source Projects that are driving JavaScript'...The Return of JavaScript: 3 Open-Source Projects that are driving JavaScript'...
The Return of JavaScript: 3 Open-Source Projects that are driving JavaScript'...
 
Understanding Asynchronous JavaScript
Understanding Asynchronous JavaScriptUnderstanding Asynchronous JavaScript
Understanding Asynchronous JavaScript
 
Avinash Kundaliya: Javascript and WordPress
Avinash Kundaliya: Javascript and WordPressAvinash Kundaliya: Javascript and WordPress
Avinash Kundaliya: Javascript and WordPress
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
 
Bonnes pratiques de dĂŠveloppement avec Node js
Bonnes pratiques de dĂŠveloppement avec Node jsBonnes pratiques de dĂŠveloppement avec Node js
Bonnes pratiques de dĂŠveloppement avec Node js
 
JavaScript Interview Questions 2023
JavaScript Interview Questions 2023JavaScript Interview Questions 2023
JavaScript Interview Questions 2023
 
Javascript Everywhere
Javascript EverywhereJavascript Everywhere
Javascript Everywhere
 
JavaScript Multithread or Single Thread.pptx
JavaScript Multithread or Single Thread.pptxJavaScript Multithread or Single Thread.pptx
JavaScript Multithread or Single Thread.pptx
 
JavaScript Misunderstood
JavaScript MisunderstoodJavaScript Misunderstood
JavaScript Misunderstood
 
Expert JavaScript tricks of the masters
Expert JavaScript  tricks of the mastersExpert JavaScript  tricks of the masters
Expert JavaScript tricks of the masters
 
Apache Commons - Don\'t re-invent the wheel
Apache Commons - Don\'t re-invent the wheelApache Commons - Don\'t re-invent the wheel
Apache Commons - Don\'t re-invent the wheel
 
A Journey with React
A Journey with ReactA Journey with React
A Journey with React
 
Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...
Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...
Fundamental Node.js (Workshop bersama Front-end Developer GITS Indonesia, War...
 
Java script Examples by Som
Java script Examples by Som  Java script Examples by Som
Java script Examples by Som
 
Performance patterns
Performance patternsPerformance patterns
Performance patterns
 
04 Advanced Javascript
04 Advanced Javascript04 Advanced Javascript
04 Advanced Javascript
 
Apache Spark in your likeness - low and high level customization
Apache Spark in your likeness - low and high level customizationApache Spark in your likeness - low and high level customization
Apache Spark in your likeness - low and high level customization
 
mobl
moblmobl
mobl
 
The Beauty of Java Script
The Beauty of Java ScriptThe Beauty of Java Script
The Beauty of Java Script
 

More from crgwbr (7)

Lightning Talk: Making JS better with Browserify
Lightning Talk: Making JS better with BrowserifyLightning Talk: Making JS better with Browserify
Lightning Talk: Making JS better with Browserify
 
07 Integration Project Part 1
07 Integration Project Part 107 Integration Project Part 1
07 Integration Project Part 1
 
06 Map Reduce
06 Map Reduce06 Map Reduce
06 Map Reduce
 
05 Web Services
05 Web Services05 Web Services
05 Web Services
 
03 Web Events and jQuery
03 Web Events and jQuery03 Web Events and jQuery
03 Web Events and jQuery
 
01 Introduction To CSS
01 Introduction To CSS01 Introduction To CSS
01 Introduction To CSS
 
08 Integration Project Part 2
08 Integration Project Part 208 Integration Project Part 2
08 Integration Project Part 2
 

Recently uploaded

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
Christopher Logan Kennedy
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 

02 Introduction to Javascript