22
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. JavaScript Fundamentals Deepank Vora June/03/2015 SST

Javascript - SST

Embed Size (px)

Citation preview

© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.

JavaScript FundamentalsDeepank VoraJune/03/2015 SST

© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.

Introduction

© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.

3

4© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.

The language

• Frontend -> Backend

• World’s most misunderstood language

5© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.

Lab 0: Our first JavaScript program!

Write a program that prints out “Hello” to the console. Since this is the first lab, we provide the answer for you! Just copy the below code:

console.log("Hello");

6© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.

Comments

• Write something meant to be read by humans only (not the computer)

• Everything after // is a comment

console.log("Hello"); // This statement is a comment

© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.

7

Data Types

8© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.

Data Types

• Number

• String

• Boolean

• Object• Function• Array

9© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.

Number

• Number can be with, or without decimals

• Exampleconsole.log(1);console.log(2.5);

10© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.

String

• Simply a series of characters

• Written with quotes (single or double)

Examples

console.log("Hello");

"JavaScript Rocks".length; // 16

"JavaScript Rocks".charAt(0); // "J"

"JavaScript Rocks".replace("JavaScript", "Java"); // "Java Rocks"

"JavaScript Rocks".toLowerCase(); // "javascript rocks"

11© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.

Boolean

• true or false

• Example

console.log(true); // true

console.log(false); // false

12© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.

Variables

var greeting = "Hello";

console.log(greeting);

greeting = "Hello, how are you?"

console.log(greeting);

13© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.

Arithmetic operators

• Add

console.log(10 + 2)

• Subtract

console.log(10 - 2)

• Multiplyconsole.log(10 * 2)

• Divide

console.log(10 / 2)

14© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.

= and == are completely different!

•= (assignment) : stores the value on RHS to the variable on LHS

greeting = "Hello, how are you?"

•== (comparison) : compares LHS and RHS and returns boolean (true or false)

console.log(1 == 1)

15© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.

Array

var a = ["cat", "dog", "mouse"];

console.log(a[0]); // ?

console.log(a[2]); // ?

16© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.

null and undefined

• null: Deliberate non-valuevar x = null;console.log(x); // null

• undefined: uninitialized valuevar x;console.log(x); // undefined

17© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.

Quiz

What is the output of the program?

var list = [5, 10];var x = list[0] + list[1];var y = list[1] - list[0];var z = y / x;

console.log(z);

18© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.

Control Structures

If-else

if (time < 10) { greeting = "Good morning";} else if (time < 20){ greeting = "Good day";} else { greeting = "Good evening";}

19© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.

Objects

• Simply a collection of name-value pairs

var dog = { breed:"labrador", color:"brown"};

dog.name = "Tommy";

console.log(dog.name); // ?

console.log(dog.breed); // ?

20© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.

Lab 1

Create an object person with properties, firstName “Harry”, lastName “Potter”.

Print the firstName of person.

21© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.

Function

function max(x, y){

if(x>y){

return x;

}

else{

return y;

}

}

var a = max(2, 4);

console.log(a);

22© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.

Lab 2

Write a function min that returns the minimum of two numbers.

Call the function with two numbers. Use console.log to print the result and check if it is correct.