SlideShare a Scribd company logo
Core Systems Transformation Solutions
Introduction to javaScript
Tutor: Maxim Maltsev, ROI
Confidential 2
The Plan
Re-Introduction to Javascript
• History1
• Variables2
• Types3
• Operators4
• Control structures5
• Functions6
• Objects7
• Scope8
• Closures9
Confidential 3
The Plan
Re-Introduction to Javascript
• History1
• Variables2
• Types3
• Operators4
• Control structures5
• Functions6
• Objects7
• Scope8
• Closures9
Confidential 4
Javascript: History
1995. Brendon Eich,
Netscape
1996. JScript.
Microsoft
1997.
ECMAScript
standard
1999.
ECMAScript
3 standard
Confidential 5
The Plan
Re-Introduction to Javascript
• History1
• Variables2
• Types3
• Operators4
• Control structures5
• Functions6
• Objects7
• Scope8
• Closures9
Confidential 6
Javascript: Variables
var a;
var name = "simon";
var count = 6;
isInserted = false;
Confidential 7
The Plan
Re-Introduction to Javascript
• History1
• Variables2
• Types3
• Operators4
• Control structures5
• Functions6
• Objects7
• Scope8
• Closures9
Confidential 8
Numbers
Strings
Booleans
Null
Undefined
Javascript: Types: Numbers
0.1 + 0.2 == 0.30000000000000004
Math.sin(3.5); var d = Math.PI * r * r;
> parseInt("123")
123
> + "42"
42
> parseInt("hello")
NaN
> isNaN(NaN)
true
> 1 / 0
Infinity
Confidential 9
Numbers
Strings
Booleans
Null
Undefined
Javascript: Types: Strings
Strings in JavaScript are sequences of
characters.
> "hello".length
5
> "hello".charAt(0)
h
> "hello, world".replace("hello", "goodbye")
goodbye, world
> "hello".toUpperCase()
HELLO
Confidential 10
Numbers
Strings
Booleans
Null
Undefined
Javascript: Types: Booleans
> Boolean("")
false
> Boolean(234)
true
false true
0
"“
NaN
null
undefined
all other values
Confidential 11
Numbers
Strings
Booleans
Null
Undefined
Javascript: Types: null
null is a value that indicates a deliberate
non-value
var a = null
Confidential 12
Numbers
Strings
Booleans
Null
Undefined
Javascript: Types: undefined
Undefined means not initialized!
var a;
a // undefined
var b={};
b.name // undefined
Confidential 13
The Plan
Re-Introduction to Javascript
• History1
• Variables2
• Types3
• Operators4
• Control structures5
• Functions6
• Objects7
• Scope8
• Closures9
Confidential 14
Javascript: Operators
Assigning: =
var x = 5
Arithmetic: +, -, /, *, %
x = 3 + 5 // 8
Compound assignment: <operator> = <value>
x /= 4 // 2, mean x = x / 4
X += 5
X = X + 5
Confidential 15
Javascript: Operators
Comparisons: >,<, >=, <=, ==, !=, ===, !==
x == “2” // true, but
x === “2” // false
Logical: &&, ||, !
Confidential 16
Javascript: Operators
Some cases:
“3” + 4 + 5 // “345”
3 + 4 +”5” // “75”
true + 3 // 4
true + “3” // “true3”
false || 5 // 5
false || 0 || 5 || 3 // 5
Confidential 17
The Plan
Re-Introduction to Javascript
• History1
• Variables2
• Types3
• Operators4
• Control structures5
• Functions6
• Objects7
• Scope8
• Closures9
Confidential 18
Javascript: Control Structures
if (condition1) {
//do something
} else if (condition2) {
//do something
} else {
//do something
}
for (statement 1; statement 2; statement 3) {
code block to be executed
}
for (x in person) {
// do something
}
while (condition) {
code block to be executed
}
do {
code block to be executed
} while (condition);
switch(expression) {
case n:
code block
break;
case n:
code block
break;
default:
default code block
}
Confidential 19
The Plan
Re-Introduction to Javascript
• History1
• Variables2
• Types3
• Operators4
• Control structures5
• Functions6
• Objects7
• Scope8
• Closures9
Confidential 20
Javascript: Functions
function doSomething (parameters) {
code to be executed
}
var x = function (a, b) {return a * b};
x(5, 7)
(function () {
var x = "Hello!!"; // I will invoke myself
})();
Confidential 21
Javascript: Functions - this
Place of using Example This references to…
Outside functions this Global object, e.g window
Inside the regular function funcName(); Global object, e.g window
Inside a function by using
call / apply
func.call(obj);
func.apply(obj);
obj
Inside a function, using .
operator
someObject.func() someObj
Confidential 22
The Plan
Re-Introduction to Javascript
• History1
• Variables2
• Types3
• Operators4
• Control structures5
• Functions6
• Objects7
• Scope8
• Closures9
Confidential 23
Javascript: Object
var obj = new Object();
var obj = {};
obj.width = 300;
obj[‘height’] = 200;
Confidential 24
Javascript: Object Instantiations
function Person (parameters) {
// constructor’s body goes here
}
var classInstance = new Person();
What happens here?
1. New object is created, its type is object
2. It set the internal prototype and constructor property to the same in the
function.
3. Constructor is executing with “this” pointing to the object
4. Result of the call becomes the new expression, if not defined, it points to the
object created in step 1
Confidential 25
Javascript: Object’s variables and methods
function Person(name){
this.name = name
}
Person.prototype.sayHi = function() {
return 'Hi, I am ' + this.name
}
var bob = new Person(‘Bob Newman’);
bob.sayHi();
Confidential 26
Javascript: Objects - Inheritance
function Person(name){
this.name = name;
}
Person.prototype.sayHi = function() {
return 'Hi, I am ' + this.name
}
function Employee(name){
Person.prototype.constructor.call(this, name);
}
Employee.prototype = new Person();
var bob = new Employee('Bob Newman');
bob.sayHi();
Confidential 27
The Plan
Re-Introduction to Javascript
• History1
• Variables2
• Types3
• Operators4
• Control structures5
• Functions6
• Objects7
• Scope8
• Closures9
Confidential 28
Javascript: Scope
Global Variables
var myName = "Richard";
firstName = "Richard";
var name;
name;
Local Variables (Function-level scope)
var name = "Richard";
function showName () {
console.log (name); // undefined, beacause of value hoisting!
var name = "Jack"; // local variable; only accessible in this showName function
console.log (name); // Jack
}
console.log (name); // Richard: the global variable
Confidential 29
global
first
second
third
fourth
Javascript: Scope
function first(){
second();
function second(){
third();
function third(){
fourth();
function fourth(){
// do something
}
}
}
}
first();
Confidential 30
The Plan
Re-Introduction to Javascript
• History1
• Variables2
• Types3
• Operators4
• Control structures5
• Functions6
• Objects7
• Scope8
• Closures9
Confidential 31
Javascript: Closures
function makeAdder(a) {
return function(b) {
return a + b;
};
}
x = makeAdder(5);
y = makeAdder(20);
x(6)
?
y(7)
?
Confidential 32
Javascript: Thanks
Confidential 33
Arrays
Date
RegExp
Javascript: Appendix: Arrays
> var a = new Array();
> a[0] = "dog";
> a[1] = "cat";
> a[2] = "hen";
> a.length
3
> var a = ["dog", "cat", "hen"];
> a.length
3
Confidential 34
Javascript: Appendix : Arrays
> var a = ["dog", "cat", "hen"];
> a[100] = "fox";
> a.length
101
> typeof a[90]
undefined
Arrays
Date
RegExp
Confidential 35
Javascript: Appendix : Arrays
for (var i = 0; i < a.length; i++) {
// Do something with a[i]
}
for (var i in a) {
// Do something with i
}
Arrays
Date
RegExp
Confidential 36
Javascript: Appendix : Arrays
Method name Description
a.toString()
Returns a string with the toString() of each
element separated by commas.
a.toLocaleString()
Returns a string with the toLocaleString() of
each element separated by commas.
a.concat(item[, itemN])
Returns a new array with the items added on
to it.
a.join(sep)
Converts the array to a string - values delimited
by the sep param
a.pop() Removes and returns the last item.
a.push(item[, itemN]) Push adds one or more items to the end.
a.reverse() Reverse the array.
a.shift() Removes and returns the first item.
a.slice(start, end) Returns a sub-array.
a.sort([cmpfn]) Takes an optional comparison function.
a.splice(start, delcount[, itemN])
Lets you modify an array by deleting a section
and replacing it with more items.
a.unshift([item]) Prepends items to the start of the array.
Arrays
Date
RegExp
Confidential 37
Javascript: Appendix : Date
new Date();
new Date(value);
new Date(dateString);
new Date(year, month, day, hour, minute,
second, millisecond);
Arrays
Date
RegExp
Useful methods:
toString() – represent the date in the current local
format
parse() – convert the string to date
format() – format date to custom format
getTime() – get time in milliseconds
setTime() – set time in milliseconds
get/set Years, Date, Month, etc…
Confidential 38
Javascript: Appendix : RegExp
Literal and constructor notations are
possible:
/pattern/flags;
new RegExp(pattern [, flags]);
Two identical ways of creation:
/ab+c/i;
new RegExp("ab+c", "i");
Arrays
Date
RegExp
Confidential 39
Javascript: Appendix : RegExp
More examples can be found here:
https://developer.mozilla.org/en-
US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
Method Description
exec
A RegExp method that executes a search for a match in a string. It
returns an array of information.
test
A RegExp method that tests for a match in a string. It returns true or
false.
match
A String method that executes a search for a match in a string. It returns
an array of information or null on a mismatch.
search
A String method that tests for a match in a string. It returns the index of
the match, or -1 if the search fails.
replace
A String method that executes a search for a match in a string, and
replaces the matched substring with a replacement substring.
split
A String method that uses a regular expression or a fixed string to break
a string into an array of substrings.
Arrays
Date
RegExp
Confidential 40
Javascript: Thanks

More Related Content

PDF
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
NAVER / MusicPlatform
 
PDF
Cascadia.js: Don't Cross the Streams
mattpodwysocki
 
PDF
Java & OOP Core Concept
Pin-Lun Huang
 
PDF
Compose Async with RxJS
Kyung Yeol Kim
 
PPTX
Angular2 rxjs
Christoffer Noring
 
PDF
The Ring programming language version 1.3 book - Part 84 of 88
Mahmoud Samir Fayed
 
PDF
RxJS - The Reactive extensions for JavaScript
Viliam Elischer
 
PDF
Extend R with Rcpp!!!
mickey24
 
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
NAVER / MusicPlatform
 
Cascadia.js: Don't Cross the Streams
mattpodwysocki
 
Java & OOP Core Concept
Pin-Lun Huang
 
Compose Async with RxJS
Kyung Yeol Kim
 
Angular2 rxjs
Christoffer Noring
 
The Ring programming language version 1.3 book - Part 84 of 88
Mahmoud Samir Fayed
 
RxJS - The Reactive extensions for JavaScript
Viliam Elischer
 
Extend R with Rcpp!!!
mickey24
 

What's hot (20)

ODP
IIUG 2016 Gathering Informix data into R
Kevin Smith
 
PDF
The Ring programming language version 1.2 book - Part 21 of 84
Mahmoud Samir Fayed
 
PDF
Vasia Kalavri – Training: Gelly School
Flink Forward
 
KEY
11.11.22 かなり役立つ競技プログラミング
Kei Nakazawa
 
PDF
オープンデータを使ったモバイルアプリ開発(応用編)
Takayuki Goto
 
PDF
RxJS Evolved
trxcllnt
 
PDF
Reactive Programming Patterns with RxSwift
Florent Pillet
 
PPTX
Functional Reactive Programming with RxJS
stefanmayer13
 
PPTX
Object Oriented JavaScript
injulkarnilesh
 
PDF
RxJS 5 in Depth
C4Media
 
PDF
Reactive programming with RxSwift
Scott Gardner
 
PDF
Java8 stream
koji lin
 
PPTX
An Introduction to RxJava
Sanjay Acharya
 
PDF
Towards hasktorch 1.0
Junji Hashimoto
 
PPTX
The Great and Mighty C++
Andrey Karpov
 
ZIP
Lisp Primer Key
Yuumi Yoshida
 
PDF
CodeFest 2014. Axel Rauschmayer — JavaScript’s variables: scopes, environment...
CodeFest
 
PDF
Reactive Programming with RxSwift
Scott Gardner
 
PDF
RxJS101 - What you need to know to get started with RxJS tomorrow
Viliam Elischer
 
PDF
Richard Salter: Using the Titanium OpenGL Module
Axway Appcelerator
 
IIUG 2016 Gathering Informix data into R
Kevin Smith
 
The Ring programming language version 1.2 book - Part 21 of 84
Mahmoud Samir Fayed
 
Vasia Kalavri – Training: Gelly School
Flink Forward
 
11.11.22 かなり役立つ競技プログラミング
Kei Nakazawa
 
オープンデータを使ったモバイルアプリ開発(応用編)
Takayuki Goto
 
RxJS Evolved
trxcllnt
 
Reactive Programming Patterns with RxSwift
Florent Pillet
 
Functional Reactive Programming with RxJS
stefanmayer13
 
Object Oriented JavaScript
injulkarnilesh
 
RxJS 5 in Depth
C4Media
 
Reactive programming with RxSwift
Scott Gardner
 
Java8 stream
koji lin
 
An Introduction to RxJava
Sanjay Acharya
 
Towards hasktorch 1.0
Junji Hashimoto
 
The Great and Mighty C++
Andrey Karpov
 
Lisp Primer Key
Yuumi Yoshida
 
CodeFest 2014. Axel Rauschmayer — JavaScript’s variables: scopes, environment...
CodeFest
 
Reactive Programming with RxSwift
Scott Gardner
 
RxJS101 - What you need to know to get started with RxJS tomorrow
Viliam Elischer
 
Richard Salter: Using the Titanium OpenGL Module
Axway Appcelerator
 
Ad

Viewers also liked (7)

PPTX
Java script
Sadeek Mohammed
 
PPTX
An Overview of HTML, CSS & Java Script
Fahim Abdullah
 
PPT
Java servlet life cycle - methods ppt
kamal kotecha
 
PPT
Java Servlets
Nitin Pai
 
ODP
PHP Web Programming
Muthuselvam RS
 
PPT
Php mysql ppt
Karmatechnologies Pvt. Ltd.
 
PPTX
PHP Powerpoint -- Teach PHP with this
Ian Macali
 
Java script
Sadeek Mohammed
 
An Overview of HTML, CSS & Java Script
Fahim Abdullah
 
Java servlet life cycle - methods ppt
kamal kotecha
 
Java Servlets
Nitin Pai
 
PHP Web Programming
Muthuselvam RS
 
PHP Powerpoint -- Teach PHP with this
Ian Macali
 
Ad

Similar to Java Script Overview (20)

PPTX
JavaScript.pptx
KennyPratheepKumar
 
PPT
JavaScript - An Introduction
Manvendra Singh
 
PDF
Javascript
20261A05H0SRIKAKULAS
 
PPTX
Lecture 5: Client Side Programming 1
Artificial Intelligence Institute at UofSC
 
PPTX
JavaScript Fundamentals & JQuery
Jamshid Hashimi
 
PDF
Scalable JavaScript
Ynon Perek
 
PPT
JavaScript Workshop
Pamela Fox
 
PPTX
The JavaScript Programming Language
Mohammed Irfan Shaikh
 
PPTX
JavaScript OOPS Implimentation
Usman Mehmood
 
PPT
Java Script ppt
Priya Goyal
 
PDF
Basics of JavaScript
Bala Narayanan
 
PPT
An introduction to javascript
MD Sayem Ahmed
 
PPTX
Javascript analysis
Uchitha Bandara
 
PPT
JavaScript Tutorial
Bui Kiet
 
ODP
jsbasics-slide
Peter Borkuti
 
PPTX
js.pptx
SuhaibKhan62
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PPT
Javascript
Vishwa Patel
 
PPTX
Intro to Javascript
Anjan Banda
 
PDF
Client sidescripting javascript
Selvin Josy Bai Somu
 
JavaScript.pptx
KennyPratheepKumar
 
JavaScript - An Introduction
Manvendra Singh
 
Lecture 5: Client Side Programming 1
Artificial Intelligence Institute at UofSC
 
JavaScript Fundamentals & JQuery
Jamshid Hashimi
 
Scalable JavaScript
Ynon Perek
 
JavaScript Workshop
Pamela Fox
 
The JavaScript Programming Language
Mohammed Irfan Shaikh
 
JavaScript OOPS Implimentation
Usman Mehmood
 
Java Script ppt
Priya Goyal
 
Basics of JavaScript
Bala Narayanan
 
An introduction to javascript
MD Sayem Ahmed
 
Javascript analysis
Uchitha Bandara
 
JavaScript Tutorial
Bui Kiet
 
jsbasics-slide
Peter Borkuti
 
js.pptx
SuhaibKhan62
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
Javascript
Vishwa Patel
 
Intro to Javascript
Anjan Banda
 
Client sidescripting javascript
Selvin Josy Bai Somu
 

More from Return on Intelligence (20)

PPTX
Clean Code Approach
Return on Intelligence
 
PPTX
Code Coverage
Return on Intelligence
 
PPTX
Effective Communication in english
Return on Intelligence
 
PPTX
Anti-patterns
Return on Intelligence
 
PPTX
Conflicts Resolving
Return on Intelligence
 
PPTX
Database versioning with liquibase
Return on Intelligence
 
PPTX
Effective Feedback
Return on Intelligence
 
PPTX
English for Negotiations 2016
Return on Intelligence
 
PPTX
Lean Software Development
Return on Intelligence
 
PPT
Unit Tests? It is Very Simple and Easy!
Return on Intelligence
 
PPTX
Quick Start to AngularJS
Return on Intelligence
 
PPTX
Introduction to Backbone.js & Marionette.js
Return on Intelligence
 
PPTX
Types of testing and their classification
Return on Intelligence
 
PPTX
Introduction to EJB
Return on Intelligence
 
PPTX
Enterprise Service Bus
Return on Intelligence
 
PPTX
Apache cassandra - future without boundaries (part3)
Return on Intelligence
 
PPTX
Apache cassandra - future without boundaries (part2)
Return on Intelligence
 
PPTX
Apache cassandra - future without boundaries (part1)
Return on Intelligence
 
PPTX
Career development in exigen services
Return on Intelligence
 
PPTX
Introduction to selenium web driver
Return on Intelligence
 
Clean Code Approach
Return on Intelligence
 
Code Coverage
Return on Intelligence
 
Effective Communication in english
Return on Intelligence
 
Anti-patterns
Return on Intelligence
 
Conflicts Resolving
Return on Intelligence
 
Database versioning with liquibase
Return on Intelligence
 
Effective Feedback
Return on Intelligence
 
English for Negotiations 2016
Return on Intelligence
 
Lean Software Development
Return on Intelligence
 
Unit Tests? It is Very Simple and Easy!
Return on Intelligence
 
Quick Start to AngularJS
Return on Intelligence
 
Introduction to Backbone.js & Marionette.js
Return on Intelligence
 
Types of testing and their classification
Return on Intelligence
 
Introduction to EJB
Return on Intelligence
 
Enterprise Service Bus
Return on Intelligence
 
Apache cassandra - future without boundaries (part3)
Return on Intelligence
 
Apache cassandra - future without boundaries (part2)
Return on Intelligence
 
Apache cassandra - future without boundaries (part1)
Return on Intelligence
 
Career development in exigen services
Return on Intelligence
 
Introduction to selenium web driver
Return on Intelligence
 

Recently uploaded (20)

PDF
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 
PDF
The Role of Automation and AI in EHS Management for Data Centers.pdf
TECH EHS Solution
 
PPTX
Services offered by Dynamic Solutions in Pakistan
DaniyaalAdeemShibli1
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PDF
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PPTX
oapresentation.pptx
mehatdhavalrajubhai
 
PPTX
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PPTX
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
PDF
Wondershare Filmora 14.5.20.12999 Crack Full New Version 2025
gsgssg2211
 
DOCX
The Future of Smart Factories Why Embedded Analytics Leads the Way
Varsha Nayak
 
PPTX
Explanation about Structures in C language.pptx
Veeral Rathod
 
PPTX
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
PDF
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
PPTX
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
PDF
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
PPTX
Presentation about variables and constant.pptx
safalsingh810
 
PDF
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
PDF
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 
The Role of Automation and AI in EHS Management for Data Centers.pdf
TECH EHS Solution
 
Services offered by Dynamic Solutions in Pakistan
DaniyaalAdeemShibli1
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
oapresentation.pptx
mehatdhavalrajubhai
 
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
Wondershare Filmora 14.5.20.12999 Crack Full New Version 2025
gsgssg2211
 
The Future of Smart Factories Why Embedded Analytics Leads the Way
Varsha Nayak
 
Explanation about Structures in C language.pptx
Veeral Rathod
 
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
Presentation about variables and constant.pptx
safalsingh810
 
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 

Java Script Overview

  • 1. Core Systems Transformation Solutions Introduction to javaScript Tutor: Maxim Maltsev, ROI
  • 2. Confidential 2 The Plan Re-Introduction to Javascript • History1 • Variables2 • Types3 • Operators4 • Control structures5 • Functions6 • Objects7 • Scope8 • Closures9
  • 3. Confidential 3 The Plan Re-Introduction to Javascript • History1 • Variables2 • Types3 • Operators4 • Control structures5 • Functions6 • Objects7 • Scope8 • Closures9
  • 4. Confidential 4 Javascript: History 1995. Brendon Eich, Netscape 1996. JScript. Microsoft 1997. ECMAScript standard 1999. ECMAScript 3 standard
  • 5. Confidential 5 The Plan Re-Introduction to Javascript • History1 • Variables2 • Types3 • Operators4 • Control structures5 • Functions6 • Objects7 • Scope8 • Closures9
  • 6. Confidential 6 Javascript: Variables var a; var name = "simon"; var count = 6; isInserted = false;
  • 7. Confidential 7 The Plan Re-Introduction to Javascript • History1 • Variables2 • Types3 • Operators4 • Control structures5 • Functions6 • Objects7 • Scope8 • Closures9
  • 8. Confidential 8 Numbers Strings Booleans Null Undefined Javascript: Types: Numbers 0.1 + 0.2 == 0.30000000000000004 Math.sin(3.5); var d = Math.PI * r * r; > parseInt("123") 123 > + "42" 42 > parseInt("hello") NaN > isNaN(NaN) true > 1 / 0 Infinity
  • 9. Confidential 9 Numbers Strings Booleans Null Undefined Javascript: Types: Strings Strings in JavaScript are sequences of characters. > "hello".length 5 > "hello".charAt(0) h > "hello, world".replace("hello", "goodbye") goodbye, world > "hello".toUpperCase() HELLO
  • 10. Confidential 10 Numbers Strings Booleans Null Undefined Javascript: Types: Booleans > Boolean("") false > Boolean(234) true false true 0 "“ NaN null undefined all other values
  • 11. Confidential 11 Numbers Strings Booleans Null Undefined Javascript: Types: null null is a value that indicates a deliberate non-value var a = null
  • 12. Confidential 12 Numbers Strings Booleans Null Undefined Javascript: Types: undefined Undefined means not initialized! var a; a // undefined var b={}; b.name // undefined
  • 13. Confidential 13 The Plan Re-Introduction to Javascript • History1 • Variables2 • Types3 • Operators4 • Control structures5 • Functions6 • Objects7 • Scope8 • Closures9
  • 14. Confidential 14 Javascript: Operators Assigning: = var x = 5 Arithmetic: +, -, /, *, % x = 3 + 5 // 8 Compound assignment: <operator> = <value> x /= 4 // 2, mean x = x / 4 X += 5 X = X + 5
  • 15. Confidential 15 Javascript: Operators Comparisons: >,<, >=, <=, ==, !=, ===, !== x == “2” // true, but x === “2” // false Logical: &&, ||, !
  • 16. Confidential 16 Javascript: Operators Some cases: “3” + 4 + 5 // “345” 3 + 4 +”5” // “75” true + 3 // 4 true + “3” // “true3” false || 5 // 5 false || 0 || 5 || 3 // 5
  • 17. Confidential 17 The Plan Re-Introduction to Javascript • History1 • Variables2 • Types3 • Operators4 • Control structures5 • Functions6 • Objects7 • Scope8 • Closures9
  • 18. Confidential 18 Javascript: Control Structures if (condition1) { //do something } else if (condition2) { //do something } else { //do something } for (statement 1; statement 2; statement 3) { code block to be executed } for (x in person) { // do something } while (condition) { code block to be executed } do { code block to be executed } while (condition); switch(expression) { case n: code block break; case n: code block break; default: default code block }
  • 19. Confidential 19 The Plan Re-Introduction to Javascript • History1 • Variables2 • Types3 • Operators4 • Control structures5 • Functions6 • Objects7 • Scope8 • Closures9
  • 20. Confidential 20 Javascript: Functions function doSomething (parameters) { code to be executed } var x = function (a, b) {return a * b}; x(5, 7) (function () { var x = "Hello!!"; // I will invoke myself })();
  • 21. Confidential 21 Javascript: Functions - this Place of using Example This references to… Outside functions this Global object, e.g window Inside the regular function funcName(); Global object, e.g window Inside a function by using call / apply func.call(obj); func.apply(obj); obj Inside a function, using . operator someObject.func() someObj
  • 22. Confidential 22 The Plan Re-Introduction to Javascript • History1 • Variables2 • Types3 • Operators4 • Control structures5 • Functions6 • Objects7 • Scope8 • Closures9
  • 23. Confidential 23 Javascript: Object var obj = new Object(); var obj = {}; obj.width = 300; obj[‘height’] = 200;
  • 24. Confidential 24 Javascript: Object Instantiations function Person (parameters) { // constructor’s body goes here } var classInstance = new Person(); What happens here? 1. New object is created, its type is object 2. It set the internal prototype and constructor property to the same in the function. 3. Constructor is executing with “this” pointing to the object 4. Result of the call becomes the new expression, if not defined, it points to the object created in step 1
  • 25. Confidential 25 Javascript: Object’s variables and methods function Person(name){ this.name = name } Person.prototype.sayHi = function() { return 'Hi, I am ' + this.name } var bob = new Person(‘Bob Newman’); bob.sayHi();
  • 26. Confidential 26 Javascript: Objects - Inheritance function Person(name){ this.name = name; } Person.prototype.sayHi = function() { return 'Hi, I am ' + this.name } function Employee(name){ Person.prototype.constructor.call(this, name); } Employee.prototype = new Person(); var bob = new Employee('Bob Newman'); bob.sayHi();
  • 27. Confidential 27 The Plan Re-Introduction to Javascript • History1 • Variables2 • Types3 • Operators4 • Control structures5 • Functions6 • Objects7 • Scope8 • Closures9
  • 28. Confidential 28 Javascript: Scope Global Variables var myName = "Richard"; firstName = "Richard"; var name; name; Local Variables (Function-level scope) var name = "Richard"; function showName () { console.log (name); // undefined, beacause of value hoisting! var name = "Jack"; // local variable; only accessible in this showName function console.log (name); // Jack } console.log (name); // Richard: the global variable
  • 29. Confidential 29 global first second third fourth Javascript: Scope function first(){ second(); function second(){ third(); function third(){ fourth(); function fourth(){ // do something } } } } first();
  • 30. Confidential 30 The Plan Re-Introduction to Javascript • History1 • Variables2 • Types3 • Operators4 • Control structures5 • Functions6 • Objects7 • Scope8 • Closures9
  • 31. Confidential 31 Javascript: Closures function makeAdder(a) { return function(b) { return a + b; }; } x = makeAdder(5); y = makeAdder(20); x(6) ? y(7) ?
  • 33. Confidential 33 Arrays Date RegExp Javascript: Appendix: Arrays > var a = new Array(); > a[0] = "dog"; > a[1] = "cat"; > a[2] = "hen"; > a.length 3 > var a = ["dog", "cat", "hen"]; > a.length 3
  • 34. Confidential 34 Javascript: Appendix : Arrays > var a = ["dog", "cat", "hen"]; > a[100] = "fox"; > a.length 101 > typeof a[90] undefined Arrays Date RegExp
  • 35. Confidential 35 Javascript: Appendix : Arrays for (var i = 0; i < a.length; i++) { // Do something with a[i] } for (var i in a) { // Do something with i } Arrays Date RegExp
  • 36. Confidential 36 Javascript: Appendix : Arrays Method name Description a.toString() Returns a string with the toString() of each element separated by commas. a.toLocaleString() Returns a string with the toLocaleString() of each element separated by commas. a.concat(item[, itemN]) Returns a new array with the items added on to it. a.join(sep) Converts the array to a string - values delimited by the sep param a.pop() Removes and returns the last item. a.push(item[, itemN]) Push adds one or more items to the end. a.reverse() Reverse the array. a.shift() Removes and returns the first item. a.slice(start, end) Returns a sub-array. a.sort([cmpfn]) Takes an optional comparison function. a.splice(start, delcount[, itemN]) Lets you modify an array by deleting a section and replacing it with more items. a.unshift([item]) Prepends items to the start of the array. Arrays Date RegExp
  • 37. Confidential 37 Javascript: Appendix : Date new Date(); new Date(value); new Date(dateString); new Date(year, month, day, hour, minute, second, millisecond); Arrays Date RegExp Useful methods: toString() – represent the date in the current local format parse() – convert the string to date format() – format date to custom format getTime() – get time in milliseconds setTime() – set time in milliseconds get/set Years, Date, Month, etc…
  • 38. Confidential 38 Javascript: Appendix : RegExp Literal and constructor notations are possible: /pattern/flags; new RegExp(pattern [, flags]); Two identical ways of creation: /ab+c/i; new RegExp("ab+c", "i"); Arrays Date RegExp
  • 39. Confidential 39 Javascript: Appendix : RegExp More examples can be found here: https://developer.mozilla.org/en- US/docs/Web/JavaScript/Reference/Global_Objects/RegExp Method Description exec A RegExp method that executes a search for a match in a string. It returns an array of information. test A RegExp method that tests for a match in a string. It returns true or false. match A String method that executes a search for a match in a string. It returns an array of information or null on a mismatch. search A String method that tests for a match in a string. It returns the index of the match, or -1 if the search fails. replace A String method that executes a search for a match in a string, and replaces the matched substring with a replacement substring. split A String method that uses a regular expression or a fixed string to break a string into an array of substrings. Arrays Date RegExp

Editor's Notes

  • #5: JavaScript was created in 1995 by Brendan Eich, an engineer at Netscape, and first released with Netscape 2 early in 1996. Microsoft released a mostly-compatible version of the language called JScript with IE 3 several months later. Netscape submitted the language to Ecma International, a European standards organization, which resulted in the first edition of the ECMAScript standard in 1997. The standard received a significant update as ECMAScript edition 3 in 1999, and has stayed pretty much stable ever since.  JavaScript is an object oriented dynamic language; it has types and operators, core objects, and methods. Its syntax comes from the Java and C languages, so many structures from those languages apply to JavaScript as well. One of the key differences is that JavaScript does not have classes; instead, the class functionality is accomplished by object prototypes. The other main difference is that functions are objects, giving functions the capacity to hold executable code and be passed around like any other object.
  • #7: New variables in JavaScript are declared using the var keyword: var a; var name = "simon"; If you declare a variable without assigning any value to it, its type is undefined.  An important difference from other languages like Java is that in JavaScript, blocks do not have scope; only functions have scope. So if a variable is defined using var in a compound statement (for example inside an if control structure), it will be visible to the entire function.
  • #9: Numbers in JavaScript are "double-precision 64-bit format IEEE 754 values", according to the spec. This has some interesting consequences. There's no such thing as an integer in JavaScript, so you have to be a little careful with your arithmetic if you're used to math in C or Java. Watch out for stuff like: 0.1 + 0.2 == 0.30000000000000004 The standard numeric operators are supported, including addition, subtraction, modulus (or remainder) arithmetic and so forth. There's also a built-in object that I forgot to mention earlier called Math to handle more advanced mathematical functions and constants: Math.sin(3.5); var d = Math.PI * r * r; You can convert a string to an integer using the built-in parseInt() function. This takes the base for the conversion as an optional second argument, which you should always provide: > parseInt("123", 10) 123 > parseInt("010", 10) 10 If you don't provide the base, you can get surprising results in older browsers (pre-2013): > parseInt("010") 8 That happened because the parseInt function decided to treat the string as octal due to the leading 0. If you want to convert a binary number to an integer, just change the base: > parseInt("11", 2) 3 Similarly, you can parse floating point numbers using the built-in parseFloat() function which uses base 10 always unlike itsparseInt() cousin. You can also use the unary + operator to convert values to numbers: > + "42" 42
  • #10: Strings Strings in JavaScript are sequences of characters. More accurately, they're sequences of Unicode characters, with each character represented by a 16-bit number. This should be welcome news to anyone who has had to deal with internationalisation. If you want to represent a single character, you just use a string of length 1. To find the length of a string, access its length property: > "hello".length 5 There's our first brush with JavaScript objects! Did I mention that you can use strings like objects too? They have methods as well: > "hello".charAt(0) h > "hello, world".replace("hello", "goodbye") goodbye, world > "hello".toUpperCase() HELLO
  • #11: JavaScript has a boolean type, with possible values true and false (both of which are keywords). Any value can be converted to a boolean according to the following rules: false, 0, the empty string (""), NaN, null, and undefined all become false all other values become true You can perform this conversion explicitly using the Boolean() function: > Boolean("") false > Boolean(234) true However, this is rarely necessary, as JavaScript will silently perform this conversion when it expects a boolean, such as in an if statement (see below). For this reason, we sometimes speak simply of "true values" and "false values," meaning values that become true and false, respectively, when converted to booleans. Alternatively, such values can be called "truthy" and "falsy", respectively. Boolean operations such as && (logical and), || (logical or), and ! (logical not) are supported; see below.
  • #12: JavaScript distinguishes between null, which is a value that indicates a deliberate non-value, and undefined, which is a value of type 'undefined' that indicates an uninitialized value — that is, a value hasn't even been assigned yet. We'll talk about variables later, but in JavaScript it is possible to declare a variable without assigning a value to it. If you do this, the variable's type is undefined.
  • #13: JavaScript distinguishes between null, which is a value that indicates a deliberate non-value, and undefined, which is a value of type 'undefined' that indicates an uninitialized value — that is, a value hasn't even been assigned yet. We'll talk about variables later, but in JavaScript it is possible to declare a variable without assigning a value to it. If you do this, the variable's type is undefined.
  • #15: JavaScript's numeric operators are +, -, *, / and % - which is the remainder operator. Values are assigned using =, and there are also compound assignment statements such as += and -=. These extend out to x = x operator y. x += 5 x = x + 5 You can use ++ and -- to increment and decrement respectively. These can be used as prefix or postfix operators. The + operator also does string concatenation: > "hello" + " world" hello world If you add a string to a number (or other value) everything is converted in to a string first. This might catch you up: > "3" + 4 + 5 "345" > 3 + 4 + "5" "75" Adding an empty string to something is a useful way of converting it. Comparisons in JavaScript can be made using <, >, <= and >=. These work for both strings and numbers. Equality is a little less straightforward. The double-equals operator performs type coercion if you give it different types, with sometimes interesting results: > "dog" == "dog" true > 1 == true true To avoid type coercion, use the triple-equals operator: > 1 === true false > true === true true There are also != and !== operators. JavaScript also has bitwise operations. If you want to use them, follow the link below. https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators
  • #16: JavaScript's numeric operators are +, -, *, / and % - which is the remainder operator. Values are assigned using =, and there are also compound assignment statements such as += and -=. These extend out to x = x operator y. x += 5 x = x + 5 You can use ++ and -- to increment and decrement respectively. These can be used as prefix or postfix operators. The + operator also does string concatenation: > "hello" + " world" hello world If you add a string to a number (or other value) everything is converted in to a string first. This might catch you up: > "3" + 4 + 5 "345" > 3 + 4 + "5" "75" Adding an empty string to something is a useful way of converting it. Comparisons in JavaScript can be made using <, >, <= and >=. These work for both strings and numbers. Equality is a little less straightforward. The double-equals operator performs type coercion if you give it different types, with sometimes interesting results: > "dog" == "dog" true > 1 == true true To avoid type coercion, use the triple-equals operator: > 1 === true false > true === true true There are also != and !== operators. JavaScript also has bitwise operations. If you want to use them, follow the link below. https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators
  • #17: JavaScript's numeric operators are +, -, *, / and % - which is the remainder operator. Values are assigned using =, and there are also compound assignment statements such as += and -=. These extend out to x = x operator y. x += 5 x = x + 5 You can use ++ and -- to increment and decrement respectively. These can be used as prefix or postfix operators. The + operator also does string concatenation: > "hello" + " world" hello world If you add a string to a number (or other value) everything is converted in to a string first. This might catch you up: > "3" + 4 + 5 "345" > 3 + 4 + "5" "75" Adding an empty string to something is a useful way of converting it. Comparisons in JavaScript can be made using <, >, <= and >=. These work for both strings and numbers. Equality is a little less straightforward. The double-equals operator performs type coercion if you give it different types, with sometimes interesting results: > "dog" == "dog" true > 1 == true true To avoid type coercion, use the triple-equals operator: > 1 === true false > true === true true There are also != and !== operators. JavaScript also has bitwise operations. If you want to use them, follow the link below. https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators
  • #19: JavaScript has a similar set of control structures to other languages in the C family. Conditional statements are supported by if and else; you can chain them together if you like: If you don't add a break statement, execution will "fall through" to the next level.
  • #21: Along with objects, functions are the core component in understanding JavaScript. The most basic function couldn't be much simpler: function add(x, y) { var total = x + y; return total; } This demonstrates everything there is to know about basic functions. A JavaScript function can take 0 or more named parameters. The function body can contain as many statements as you like, and can declare its own variables which are local to that function. The return statement can be used to return a value at any time, terminating the function. If no return statement is used (or an empty return with no value), JavaScript returns undefined.
  • #24: JavaScript objects can be thought of as simple collections of name-value pairs. As such, they are similar to: Dictionaries in Python Hashes in Perl and Ruby Hash tables in C and C++ HashMaps in Java Associative arrays in PHP
  • #25: What happens here?
  • #26: What happens here?
  • #27: What happens here?
  • #29: Local: Unlike most programming languages, JavaScript does not have block-level scope (variables scoped to surrounding curly brackets); instead, JavaScript has function-level scope. Variables declared within a function are local variables and are only accessible within that function or by functions inside that function. If you declare a global variable and a local variable with the same name, the local variable will have priority when you attempt to use the variable inside a function (local scope) Global: It is important to note that all global variables are attached to the window object. So, all the global variables we just declared can be accessed on the window object If a variable is initialized (assigned a value) without first being declared with the var keyword, it is automatically added to the global context and it is thus a global variable
  • #30: What happens here?
  • #32: Whenever JavaScript executes a function, a 'scope' object is created to hold the local variables created within that function. It is initialised with any variables passed in as function parameters. This is similar to the global object that all global variables and functions live in, but with a couple of important differences: firstly, a brand new scope object is created every time a function starts executing, and secondly, unlike the global object (which is accessible as this and in browsers is accessible as window) these scope objects cannot be directly accessed from your JavaScript code. There is no mechanism for iterating over the properties of the current scope object, for example. So when makeAdder is called, a scope object is created with one property: a, which is the argument passed to the makeAdder function. makeAdder then returns a newly created function. Normally JavaScript's garbage collector would clean up the scope object created for makeAdder at this point, but the returned function maintains a reference back to that scope object. As a result, the scope object will not be garbage collected until there are no more references to the function object that makeAdder returned.
  • #34: Arrays in JavaScript are actually a special type of object. They work very much like regular objects (numerical properties can naturally be accessed only using [] syntax) but they have one magic property called 'length'. This is always one more than the highest index in the array. The old way of creating arrays is as follows:
  • #35: Note that array.length isn't necessarily the number of items in the array. Consider the following: Remember — the length of the array is one more than the highest index. If you query a non-existent array index, you get undefined
  • #36: This is slightly inefficient as you are looking up the length property once every loop. An improvement is this: for (var i = 0, len = a.length; i < len; i++) { // Do something with a[i] } An even nicer idiom is: for (var i = 0, item; item = a[i++];) { // Do something with item } Here we are setting up two variables. The assignment in the middle part of the for loop is also tested for truthfulness — if it succeeds, the loop continues. Since i is incremented each time, items from the array will be assigned to item in sequential order. The loop stops when a "falsy" item is found (such as undefined). Note that this trick should only be used for arrays which you know do not contain "falsy" values (arrays of objects or DOM nodes for example). If you are iterating over numeric data that might include a 0 or string data that might include the empty string you should use the i, len idiom instead. Another way to iterate is to use the for...in loop. Note that if someone added new properties to Array.prototype, they will also be iterated over by this loop:
  • #38: Note: Note that JavaScript Date objects can only be instantiated by calling JavaScriptDate as a constructor: calling it as a regular function (i.e. without the new operator) will return a string rather than a Date object; unlike other JavaScript object types, JavaScript Date objects have no literal syntax. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
  • #39: The literal notation provides compilation of the regular expression when the expression is evaluated. Use literal notation when the regular expression will remain constant. For example, if you use literal notation to construct a regular expression used in a loop, the regular expression won't be recompiled on each iteration. The constructor of the regular expression object, for example, new RegExp("ab+c"), provides runtime compilation of the regular expression. Use the constructor function when you know the regular expression pattern will be changing, or you don't know the pattern and are getting it from another source, such as user input.
  • #40: The literal notation provides compilation of the regular expression when the expression is evaluated. Use literal notation when the regular expression will remain constant. For example, if you use literal notation to construct a regular expression used in a loop, the regular expression won't be recompiled on each iteration. The constructor of the regular expression object, for example, new RegExp("ab+c"), provides runtime compilation of the regular expression. Use the constructor function when you know the regular expression pattern will be changing, or you don't know the pattern and are getting it from another source, such as user input.