This document contains notes from a JavaScript fundamentals training covering topics like data types, variables, operators, control structures, objects, and functions. It includes examples of JavaScript code and short programming challenges or "labs" for students to complete, like writing a function to return the minimum of two numbers.
1 of 20
Download to read offline
More Related Content
Javascript - ITE
1. 息 2015 PayPal Inc. All rights reserved. Confidential and proprietary.
JavaScript Fundamentals
Deepank Vora
June/11/2015 ITE
2. 息 2015 PayPal Inc. All rights reserved. Confidential and proprietary.
Introduction
3. 息 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 3
4. 息 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 4
The language
Frontend -> Backend
Worlds most misunderstood language
5. 息 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 5
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. 6
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
7. 息 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 7
Data Types
8. 息 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 8
Data Types
Number
String
Boolean
Object
Function
Array
9. 息 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 9
Number
Number can be with, or without decimals
Example
console.log(1);
console.log(2.5);
10. 息 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 10
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. 11
Boolean
true or false
Example
console.log(true); // true
console.log(false); // false
12. 息 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 12
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. 13
Arithmetic operators
Add
console.log(10 + 2)
Subtract
console.log(10 - 2)
Multiply
console.log(10 * 2)
Divide
console.log(10 / 2)
14. 息 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 14
= 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. 15
Array
var a = ["cat", "dog", "mouse"];
console.log(a[0]); // ?
console.log(a[2]); // ?
16. 息 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 16
null and undefined
null: Deliberate non-value
var x = null;
console.log(x); // null
undefined: uninitialized value
var x;
console.log(x); // undefined
17. 息 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 17
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. 18
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. 19
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); // ?
Lab 1
Create an object person with properties, firstName Harry, lastName Potter.
Print the firstName of person.
20. 息 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 20
Function
function max(x, y){
if(x>y){
return x;
}
else{
return y;
}
}
var a = max(2, 4);
console.log(a);
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.
Editor's Notes
Difference between == and ===
String concatenation with +
return; returns undefined, not null.
No return: returns undefined