SlideShare a Scribd company logo
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.
github.com/nodeworkshop/setup
Setup your machine !
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.
JavaScript Fundamentals
Deepank Vora
March/25/2015 NTU IEEE
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.
Introduction
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 4
About the language
• Object-oriented dynamic language
• Frontend -> Backend
• World’s most misunderstood language
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 5
Differences from Java/C++
• No Classes. Object Prototypes
• Functions are first-class objects
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 6
Types
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 7
Types
• Number
• String
• Boolean
• Symbol
• Object
• Function
• Array
• Date
• RegExp
• null
• undefined
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 8
Number
• 64-bit floating point
• Be careful of floating point precision
Special Values
• NaN
• Infinity
• -Infinity
parseInt("123"); // 123
isNaN(parseInt("a")); // true
isFinite(1/0); // false
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 9
String
• Made up of Unicode characters
• Use string of length 1 to represent characters
Examples
"North Spine".length; // 11
"North Spine".charAt(0); // "N"
"North Spine".replace("North", "South"); // "South Spine"
"North Spine".toLowerCase(); // "north spine"
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 10
null and undefined
• null: Deliberate non-value
• var x = null;
console.log(x); // null
• undefined: uninitialized value
• var x;
console.log(x); // undefined
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 11
Boolean
• true or false
• Any value can be converted to a boolean
• false, 0, “”, NaN, null, undefined -> false
• Everything else -> true
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 12
Operators
• Arithmetic: +, -, *, /, %, ++, --
• Assignment: =, +=, -=, *=, /=, %=
• Logical: &&, ||, !
• Conditional: (condition) ? value1 : value2
• Comparison: ==, ===, !=, !==, >, <, >=, <=
• Bitwise
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 13
Control Structures
If-else
if (time < 10) {
greeting = "Good morning";
} else if (time < 20){
greeting = "Good day";
} else {
greeting = "Good evening";
}
Switch
switch(day){
case 0:
case 6:
text = "Weekend :)";
break;
default:
text = "Weekday :(";
break;
}
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 14
Control Structures
For
for(var i=0; i < 10; i++){
// executes 10 times
}
While and do-while
while(index<10){
index++;
}
var input;
do{
input = getInput();
} while (input != "0")
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 15
Lab 1.1
Write a program that uses console.log to print all the numbers from 1 to
100, with two exceptions. For numbers divisible by 3, print "Fizz" instead of
the number, and for numbers divisible by 5 (and not 3), print "Buzz" instead.
For numbers that are divisible by both 3 and 5, print "FizzBuzz".
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 16
Objects
• Simply a collection of name-value pairs
• Similar to Dictionaries in Python and HashMaps in Java
• Creating objects
var dog = { breed:"labrador", color:"brown" };
// OR
var dog = new Object();
dog.breed = "labrador";
dog.color = "brown";
• Get properties
dog.breed;
dog["breed"];
• Set properties
• dog.breed = "labrador";
dog["breed"] = "labrador";
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 17
Array
Creation
var a = new Array();
a[0] = "cat";
a[1] = "dog";
a.length; // 2
// OR
var a = ["cat", "dog"];
a[1]; //"dog"
a[10] = "mouse"
a[10]; // "mouse"
a[5]; //undefined
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 18
Function
function add(x, y) {
return x + y;
}
add(1, 2); // ?
add(); // ?
add(1, 2, 5); // ?
function add() {
var sum = 0;
for (var i = 0, j = arguments.length; i < j; i++) {
sum += arguments[i];
}
return sum;
}
add(1,2,3,4); // ?
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 19
Lab 1.2
Write a range function that takes two arguments, start and end, and returns
an array containing all the numbers from start up to (and including) end.
Next, write a sum function that takes an array of numbers and returns the
sum of these numbers. Print sum(range(1,10)) and see whether it does
indeed return 55.
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 20
Custom Objects
function Person(first, last) {
this.first = first;
this.last = last;
this.fullName = function() {
return this.first + ' ' + this.last;
};
}
var p = new Person("Barack", "Obama");
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 21
Lab 1.3
Write a constructor Vector that represents a vector in two-dimensional
space. It takes x and y parameters (numbers), which it should save to
properties of the same name.
Give Vector two methods, plus and minus, that take another vector as a
parameter and return a new vector that has the sum or difference of the two
vectors’ (the one in this and the parameter) x and y values.
Add a method length which computes the length of the vector—that is, the
distance of the point (x, y) from the origin (0, 0).
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 22
Callback functions
• A callback is a function reference
• Often defined in-line where it's required
• Typically used for "one-off" asynchronous invocations
Calls function
after 1 sec
setTimeout(function() {
console.log(new Date())
}, 1000);
Inline
callback
Anonymous
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 23
Lab 1.4
Define a function add that takes in two numbers as parameters and a third
callback parameter. This function should call the callback function, passing
the result of addition to the callback function as an argument.
Call add, passing two numbers and a callback (defined in-line). The
callback should print “The sum is: __”

More Related Content

PPTX
Paws: A Perl AWS SDK - YAPC Europe 2015
PPTX
Building an aws sdk for Perl - Granada Perl Workshop 2014
PPTX
Paws - A Perl AWS SDK
PPTX
Paws - Perl AWS SDK Update - November 2015
PPTX
Evaluating Hype in scala
PDF
Synchronize applications with akeneo/batch
PPTX
Programação reativa e o actor model
PDF
Tech fest
Paws: A Perl AWS SDK - YAPC Europe 2015
Building an aws sdk for Perl - Granada Perl Workshop 2014
Paws - A Perl AWS SDK
Paws - Perl AWS SDK Update - November 2015
Evaluating Hype in scala
Synchronize applications with akeneo/batch
Programação reativa e o actor model
Tech fest

What's hot (19)

PDF
JavaScript for ABAP Programmers - 5/7 Functions
KEY
RIAs Done Right: Grails, Flex, and EXT GWT
PDF
Crossing the Bridge: Connecting Rails and your Front-end Framework
PDF
Jakarta EE Recipes
PDF
Painless Persistence in a Disconnected World
PPT
Wcf data services
PPTX
How Pony ORM translates Python generators to SQL queries
PDF
Introduction to React Native Workshop
PDF
Julio Capote, Twitter
PDF
Reactive Applications in Enterprise Java
ODP
Scala Presentation Work
PPTX
Firebase ng2 zurich
PDF
React Native Workshop - React Alicante
PPTX
CiklumJavaSat_15112011:Alex Kruk VMForce
PDF
Journey's end
PDF
Second Level Cache in JPA Explained
PDF
An introduction to property-based testing
PDF
Akka persistence == event sourcing in 30 minutes
PDF
ES6 Simplified
JavaScript for ABAP Programmers - 5/7 Functions
RIAs Done Right: Grails, Flex, and EXT GWT
Crossing the Bridge: Connecting Rails and your Front-end Framework
Jakarta EE Recipes
Painless Persistence in a Disconnected World
Wcf data services
How Pony ORM translates Python generators to SQL queries
Introduction to React Native Workshop
Julio Capote, Twitter
Reactive Applications in Enterprise Java
Scala Presentation Work
Firebase ng2 zurich
React Native Workshop - React Alicante
CiklumJavaSat_15112011:Alex Kruk VMForce
Journey's end
Second Level Cache in JPA Explained
An introduction to property-based testing
Akka persistence == event sourcing in 30 minutes
ES6 Simplified
Ad

Viewers also liked (14)

PPTX
Actitudes en un despacho
PPTX
Oasis Hi-Tech Sportswear Ltd. Social Responsibility
PDF
How to Increase Audience Engagement at Events
PPTX
How ready is your organization for change?
PPTX
How RGB Broadcasting Competes with Other Brand Players in Over the Top (OTT) ...
PPTX
Lwm content marketing event public
PPTX
anakage_skilling
DOCX
Dapatan kajian
PDF
Give More Value to Your Association Members and Sponsors
PPTX
Engl102 assignment3final
PDF
Vincent Walsh MSc by Research Thesis Sept 2014 (1)
PPTX
Interaksi 7.1 ulasan jurnal
PPTX
Javascript - SST
DOCX
Season and Career Totals, Records, and Rankings for 2014-2015 Season
Actitudes en un despacho
Oasis Hi-Tech Sportswear Ltd. Social Responsibility
How to Increase Audience Engagement at Events
How ready is your organization for change?
How RGB Broadcasting Competes with Other Brand Players in Over the Top (OTT) ...
Lwm content marketing event public
anakage_skilling
Dapatan kajian
Give More Value to Your Association Members and Sponsors
Engl102 assignment3final
Vincent Walsh MSc by Research Thesis Sept 2014 (1)
Interaksi 7.1 ulasan jurnal
Javascript - SST
Season and Career Totals, Records, and Rankings for 2014-2015 Season
Ad

Similar to JavaScript Fundamentals (20)

PPTX
Javascript - ITE
PDF
JavaScript Interview Questions Part - 1.pdf
PDF
Node.js Workshop
PDF
Cooking your Ravioli "al dente" with Hexagonal Architecture
PPTX
Nalinee java
ZIP
Javascript Everywhere
PDF
First impression of the new cloud native programming language ballerina
PPTX
Node.js primer
PDF
Drupal 8: A story of growing up and getting off the island
PDF
OSGi Enterprise R6 specs are out! - David Bosschaert & Carsten Ziegeler
PPTX
Toronto MuleSoft Meetup: Virtual Meetup #2
PDF
Liberated APIs in ClojureLand - Paris Clojure User Group
PDF
Best Practices in Qt Quick/QML - Part 1 of 4
 
PDF
PuppetDB: A Single Source for Storing Your Puppet Data - PUG NY
ODP
Intro To Spring Python
PPTX
Mule soft meetup_charlotte_4__draft_v2.0
PPSX
Java Tutorial
PPTX
Tests in Javascript using Jasmine and Testacular
PPTX
Why you should be using the shiny new C# 6.0 features now!
PPTX
Reactive Extensions .NET
Javascript - ITE
JavaScript Interview Questions Part - 1.pdf
Node.js Workshop
Cooking your Ravioli "al dente" with Hexagonal Architecture
Nalinee java
Javascript Everywhere
First impression of the new cloud native programming language ballerina
Node.js primer
Drupal 8: A story of growing up and getting off the island
OSGi Enterprise R6 specs are out! - David Bosschaert & Carsten Ziegeler
Toronto MuleSoft Meetup: Virtual Meetup #2
Liberated APIs in ClojureLand - Paris Clojure User Group
Best Practices in Qt Quick/QML - Part 1 of 4
 
PuppetDB: A Single Source for Storing Your Puppet Data - PUG NY
Intro To Spring Python
Mule soft meetup_charlotte_4__draft_v2.0
Java Tutorial
Tests in Javascript using Jasmine and Testacular
Why you should be using the shiny new C# 6.0 features now!
Reactive Extensions .NET

Recently uploaded (20)

PPTX
history of c programming in notes for students .pptx
PDF
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
PDF
Website Design Services for Small Businesses.pdf
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
assetexplorer- product-overview - presentation
PPTX
Oracle Fusion HCM Cloud Demo for Beginners
PPTX
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
PDF
CapCut Video Editor 6.8.1 Crack for PC Latest Download (Fully Activated) 2025
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
17 Powerful Integrations Your Next-Gen MLM Software Needs
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PDF
Complete Guide to Website Development in Malaysia for SMEs
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
history of c programming in notes for students .pptx
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
Website Design Services for Small Businesses.pdf
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
assetexplorer- product-overview - presentation
Oracle Fusion HCM Cloud Demo for Beginners
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
CapCut Video Editor 6.8.1 Crack for PC Latest Download (Fully Activated) 2025
Design an Analysis of Algorithms I-SECS-1021-03
wealthsignaloriginal-com-DS-text-... (1).pdf
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
17 Powerful Integrations Your Next-Gen MLM Software Needs
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
Operating system designcfffgfgggggggvggggggggg
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
Complete Guide to Website Development in Malaysia for SMEs
Navsoft: AI-Powered Business Solutions & Custom Software Development

JavaScript Fundamentals

  • 1. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. github.com/nodeworkshop/setup Setup your machine !
  • 2. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. JavaScript Fundamentals Deepank Vora March/25/2015 NTU IEEE
  • 3. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. Introduction
  • 4. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 4 About the language • Object-oriented dynamic language • Frontend -> Backend • World’s most misunderstood language
  • 5. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 5 Differences from Java/C++ • No Classes. Object Prototypes • Functions are first-class objects
  • 6. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 6 Types
  • 7. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 7 Types • Number • String • Boolean • Symbol • Object • Function • Array • Date • RegExp • null • undefined
  • 8. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 8 Number • 64-bit floating point • Be careful of floating point precision Special Values • NaN • Infinity • -Infinity parseInt("123"); // 123 isNaN(parseInt("a")); // true isFinite(1/0); // false
  • 9. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 9 String • Made up of Unicode characters • Use string of length 1 to represent characters Examples "North Spine".length; // 11 "North Spine".charAt(0); // "N" "North Spine".replace("North", "South"); // "South Spine" "North Spine".toLowerCase(); // "north spine"
  • 10. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 10 null and undefined • null: Deliberate non-value • var x = null; console.log(x); // null • undefined: uninitialized value • var x; console.log(x); // undefined
  • 11. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 11 Boolean • true or false • Any value can be converted to a boolean • false, 0, “”, NaN, null, undefined -> false • Everything else -> true
  • 12. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 12 Operators • Arithmetic: +, -, *, /, %, ++, -- • Assignment: =, +=, -=, *=, /=, %= • Logical: &&, ||, ! • Conditional: (condition) ? value1 : value2 • Comparison: ==, ===, !=, !==, >, <, >=, <= • Bitwise
  • 13. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 13 Control Structures If-else if (time < 10) { greeting = "Good morning"; } else if (time < 20){ greeting = "Good day"; } else { greeting = "Good evening"; } Switch switch(day){ case 0: case 6: text = "Weekend :)"; break; default: text = "Weekday :("; break; }
  • 14. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 14 Control Structures For for(var i=0; i < 10; i++){ // executes 10 times } While and do-while while(index<10){ index++; } var input; do{ input = getInput(); } while (input != "0")
  • 15. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 15 Lab 1.1 Write a program that uses console.log to print all the numbers from 1 to 100, with two exceptions. For numbers divisible by 3, print "Fizz" instead of the number, and for numbers divisible by 5 (and not 3), print "Buzz" instead. For numbers that are divisible by both 3 and 5, print "FizzBuzz".
  • 16. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 16 Objects • Simply a collection of name-value pairs • Similar to Dictionaries in Python and HashMaps in Java • Creating objects var dog = { breed:"labrador", color:"brown" }; // OR var dog = new Object(); dog.breed = "labrador"; dog.color = "brown"; • Get properties dog.breed; dog["breed"]; • Set properties • dog.breed = "labrador"; dog["breed"] = "labrador";
  • 17. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 17 Array Creation var a = new Array(); a[0] = "cat"; a[1] = "dog"; a.length; // 2 // OR var a = ["cat", "dog"]; a[1]; //"dog" a[10] = "mouse" a[10]; // "mouse" a[5]; //undefined
  • 18. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 18 Function function add(x, y) { return x + y; } add(1, 2); // ? add(); // ? add(1, 2, 5); // ? function add() { var sum = 0; for (var i = 0, j = arguments.length; i < j; i++) { sum += arguments[i]; } return sum; } add(1,2,3,4); // ?
  • 19. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 19 Lab 1.2 Write a range function that takes two arguments, start and end, and returns an array containing all the numbers from start up to (and including) end. Next, write a sum function that takes an array of numbers and returns the sum of these numbers. Print sum(range(1,10)) and see whether it does indeed return 55.
  • 20. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 20 Custom Objects function Person(first, last) { this.first = first; this.last = last; this.fullName = function() { return this.first + ' ' + this.last; }; } var p = new Person("Barack", "Obama");
  • 21. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 21 Lab 1.3 Write a constructor Vector that represents a vector in two-dimensional space. It takes x and y parameters (numbers), which it should save to properties of the same name. Give Vector two methods, plus and minus, that take another vector as a parameter and return a new vector that has the sum or difference of the two vectors’ (the one in this and the parameter) x and y values. Add a method length which computes the length of the vector—that is, the distance of the point (x, y) from the origin (0, 0).
  • 22. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 22 Callback functions • A callback is a function reference • Often defined in-line where it's required • Typically used for "one-off" asynchronous invocations Calls function after 1 sec setTimeout(function() { console.log(new Date()) }, 1000); Inline callback Anonymous
  • 23. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 23 Lab 1.4 Define a function add that takes in two numbers as parameters and a third callback parameter. This function should call the callback function, passing the result of addition to the callback function as an argument. Call add, passing two numbers and a callback (defined in-line). The callback should print “The sum is: __”

Editor's Notes

  • #13: Difference between == and === String concatenation with +
  • #19: return; returns undefined, not null. No return: returns undefined
  • #21: New – 1. Creates empty object. 2. Calls Person function with ‘this’ set to the new object. 3. Returns the new object.