SlideShare a Scribd company logo
1 of 27
A290 TOOLS IN COMPUTING
JavaScript Fundamentals
Week 1
Class 2
Before you start
ā—¦ Make sure to review chapter 2 from the the Elequent JavaScript
(2nd edition) book before you start this presentation.
ā—¦ There is a compressed (.zip) file called week1class2examples in
the class website schedule page. Download that file, which has
all the examples necessary for this presentation.
ā—¦Unzip the folder and save it to your computer.
ā—¦Make sure to read the slides and the below notes carefully. There
are 5 assignment questions hidden in this presentation.
2
JavaScript Basics
<script> </script> tags are used to embed JS code within HTML file
JS allows control of HTML documents and behavior of those documents
1. Control document appearance and content (create dynamic and conditional HTML)
2. Control the browser (e.g. pop up dialog boxes)
3. Interact with HTML forms (e.g. read and write user inputs)
4. Interact with the user (e.g. event handlers)
5. Read and write client state with cookies (cookies allow a web page to remember things about the client- e.g. user
preference about the colors and layout)
Example: Interacting with a user >> When a user clicks on a button, a particular event occurs:
1. Lets see an example
2. Go to the same link, and explore and apply different events.
(Here are some other events you can use: https://www.w3schools.com/js/js_events.asp)
3
Lexical Structure of JS
JS is case sensitive: For example, myName and myname are two different variables.
JS ignores whitespace between lines of code
ā—¦ Try creating spaces in an example
4
Comments
Comments:
// This is a single line comment
/* This is a multi line
comment*/
ā—¦ Open the 1example.html file with a text editor and write comments
for what each line does. This is question1 for assignment1.
ā—¦ Create a folder named assignment1 and put this 1example.html file
in that folder with your comments. You will be asked to complete
other tasks. Put all those files in this same folder.
5
Identifier
Identifier: An identifier is simply a name. When we declare a variable
ā€œnumberā€, we are creating an identifier for the number variable.
ā—¦ In JS, identifiers are used to name variables and functions, and to provide
labels for certain loops
ā—¦ Reserved names cannot be used as an identifier (e.g. var, break,
continue, new)
6
Data Types and Values
The types of values that can be represented and manipulated in a programming language are known
as data types.
JS Primitive Data Types
ā—¦ Numbers (e.g. 3, 2.15, 345)
ā—¦ Strings (e.g. Hello World)
ā—¦ Boolean (true, false)
ā—¦ NULL (a special value that indicates no value)
ā—¦ Undefined (a variable that has been declared but no value assigned) (e.g. var name)
ā—¦ NaN
JS Reference Data Types
ā—¦ Objects (e.g. document object)
ā—¦ Function (functions are true values in JS that can be stored in variables, arrays, and objects, and can
be passed as arguments to other functions)
7
Variables
Declaration
var i;
var name, lastname;
var quote=ā€˜The goal of education is not to increase the amount of knowledge but to create the
possibilities for a child to invent and discover. Piagetā€™;
Scope: The scope of a variable is the region of your program in which it is defined.
ā—¦ Global variable: It is defined everywhere in your code
ā—¦ Local variable: Variables declared within a function
ā—¦ See 2example.html
8
Undeclared VS Unassigned Variable
var x; //declare an unassigned variable. Its value is undefined
alert(u); //using an undeclared variable causes an error
//alert is a JS method. Go ahead and test it
var u=3; //assigning a value to an undeclared variable creates the variable
9
Primitive and Reference Types
A primitive type has a fixed size in memory.
ā—¦ Examples:
ā—¦ A number occupies eight bytes of memory
ā—¦ A Boolean value can be represented with only one bit
10
Primitive and Reference Types
Reference types do not have a fixed size.
ā—¦ Examples:
ā—¦ An array can have any number of elements
ā—¦ A function can contain any amount of JS code
ā—¦ Since these types do not have a fixed size, their values cannot be stored directly in the eight bytes of
memory associated with each variable. Instead, the variable stores a reference to a value. Typically, this
reference is some sort of pointer or memory address. It is not the data value itself, but it tells the
variable where to look to find the value.
Assignment Question #2: See the notes section of this slide.
11
Variables as Properties of the Global Object
Variables in JS are fundamentally the same as object properties.
In JS, the window object serves as the global object for all JS code contained in the browser
window it represents.
When you declare a global JS variable, you are actually defining a property of the global window
object.
Window object also has its own properties
ā—¦ See window object properties: https://www.w3schools.com/jsref/obj_window.asp
12
Expressions and Operators
An expression is a phrase of JS that a JS interpreter can evaluate to produce a value
Examples
3.17 //a numerical literal
ā€œThis is a stringā€ // a string literal
[3,6,9,21,15] //an array literal
Name //the name variable
result //the result variable
13
Operator Behaviors
Some operators expect certain data types
ā—¦ ā€˜xā€™ * ā€˜yā€™ //this is not legal in JS
However, in some instances JS tries to convert expressions to the appropriate type whenever
possible:
ā—¦ ā€˜3ā€™ * ā€˜7ā€™ // the result is 21
Some operators act differently in different expressions
ā—¦ + operator adds numeric values but concatenates string values
14
Operator Precedence
Operator precedence controls the order in which operations are performed.
ā—¦ Ex: result= 2+4*4; //outputs 18
The multiplication operator has a higher precedence than the addition operator +.
Operator precedence can be overwritten with the explicit use of parentheses.
ā—¦ Ex: result=(2+4)*4 //outputs 24
ā—¦ See more: http://www.javascriptkit.com/jsref/precedence_operators.shtml
15
Arithmetic Operators
Addition +
Subtraction -
Multiplication *
Division /
Modulo % (returns the reminder: 7%3 //returns 1 )
Unary minus ā€“ (converts a positive value to negative: -4)
Increment ++ (adds 1: x=3; x++ //returns 4)
Decrement ā€“ (subtracts 1: x=3; x-- //returns 2)
See 3example.html
16
Equality Operators
These are operators that compare two values to determine whether they are
the same or different, and return a Boolean value (true or false)
They are commonly used in control or flow statements
Be careful, = is not an equality operator. It is an assignment operator for assigning values.
17
Equality Operators
== checks whether two variables are same in value
ā—¦ EX: if (x==y) alert(message);
=== checks whether two variables are identical (same value and data type)
ā—¦ EX: if (x===y) alert(message);
See 4example.html and 5example.html
18
Equality Operators
!= checks whether two variables are not equal
ā—¦ EX: if (x!=y) //returns true when the variables are not equal
!== checks whether two variables are not identical
ā—¦ EX: if (x!==y) //returns true when the variables are not identical
19
Relational Operators >> Comparison
Less than <
Greater than >
Less than or equal <=
Greater than or equal >=
20
String Operators
The + operator can be used to add (concatenate) strings.
cont=ā€Merhabaā€+ā€ DĆ¼nyaā€; //This is Hello World in Turkish.
The += assignment operator can be used to add (concatenate) strings
txt1 = "What a very ";
txt1 += "nice day";
Adding two numbers will return the sum, but adding a number and a string will return a string:
var z = "Hello" + 5;
document.write(z);
21
Logical Operators
&& and
|| or
! not
See 6example.html
22
Assignment Operator
varia= 5;
varia+=5; // this is equal to var=var+5; This is addition and assignment
together.
This is same with others such as -=, *=, /=, %=
See 7example.html
23
Other operators we will apply later
The in operator returns true if the specified property is in the specified object.
The typeof operator returns the type of a variable, object, function or expression
ā—¦ See the 8example.html example
The delete operator deletes a property from an object.
The instanceof operator returns true if the specified object is an instance of the specified object.
The void operator evaluates an expression and returns undefined. This operator is often used to
obtain the undefined primitive value, using "void(0)ā€
The new operator creates a new object and invokes a constructor function to initialize it.
Ternary operator ?: //used to shorten conditional statements
[] and . are both operators to access elements of an object
24
Assignment Question #4
Open the 9example.html file
There are three errors in that code.
Find and correct the errors and write comments for each error in the script.
Submit the corrected/commented file with other files in the assignment1 folder.
25
Assignment Question #5
Write a script that converts a user entered Fahrenheit degree to Celsius.
ā—¦ You need to create an html page with the .js file included with the following line of code:
<script type="application/javascript" src=ā€question5.js"> </script>.
Hints
ā—¦ The user will enter fahrenheit value in a text box (hint: prompt)
ā—¦ An alert box will show the celsius value of the user input
ā—¦ Ex: 32 fahrenheit is 0 degrees Celsius
ā—¦ Equation is Ā°C = 5/9 (Ā°F - 32).
ā—¦ Name the html and js files as question5.html and question5.js
ā—¦ Put them with the other assignment files in the same folder.
26
Assignment #1
ā—¦ There are 5 questions in this assignment.
ā—¦ Q1: 20 points Q2: 10 points Q3: 10 points Q4: 20 points Q5: 30 points
ā—¦ You need to put all the html and js files, and the presentation file in the same folder.
ā—¦ We will be looking at the notes section of the presentation file for the conceptual
questionsā€™ answers.
ā—¦ Then, compress all your work (zip) and submit your work in Canvas assignments.
ā—¦ You are allowed to work in teams. Make sure to include both team membersā€™ last
names in the submission file.
27

More Related Content

Similar to 13665449.ppt

Java script final presentation
Java script final presentationJava script final presentation
Java script final presentation
Adhoura Academy
Ā 

Similar to 13665449.ppt (20)

1669958779195.pdf
1669958779195.pdf1669958779195.pdf
1669958779195.pdf
Ā 
Ch3- Java Script.pdf
Ch3- Java Script.pdfCh3- Java Script.pdf
Ch3- Java Script.pdf
Ā 
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdfBasic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
Ā 
BITM3730 10-17.pptx
BITM3730 10-17.pptxBITM3730 10-17.pptx
BITM3730 10-17.pptx
Ā 
Basics of Javascript
Basics of Javascript Basics of Javascript
Basics of Javascript
Ā 
Java script final presentation
Java script final presentationJava script final presentation
Java script final presentation
Ā 
Html JavaScript and CSS
Html JavaScript and CSSHtml JavaScript and CSS
Html JavaScript and CSS
Ā 
BITM3730Week6.pptx
BITM3730Week6.pptxBITM3730Week6.pptx
BITM3730Week6.pptx
Ā 
FFW Gabrovo PMG - JavaScript 1
FFW Gabrovo PMG - JavaScript 1FFW Gabrovo PMG - JavaScript 1
FFW Gabrovo PMG - JavaScript 1
Ā 
Unit 2.4
Unit 2.4Unit 2.4
Unit 2.4
Ā 
Java script basics
Java script basicsJava script basics
Java script basics
Ā 
Javascript
JavascriptJavascript
Javascript
Ā 
Javascript
JavascriptJavascript
Javascript
Ā 
Unit 2.5
Unit 2.5Unit 2.5
Unit 2.5
Ā 
AJS UNIT-1 2021-converted.pdf
AJS UNIT-1 2021-converted.pdfAJS UNIT-1 2021-converted.pdf
AJS UNIT-1 2021-converted.pdf
Ā 
Javascript
JavascriptJavascript
Javascript
Ā 
Lecture2_MCS4_Evening.pptx
Lecture2_MCS4_Evening.pptxLecture2_MCS4_Evening.pptx
Lecture2_MCS4_Evening.pptx
Ā 
copa-ii.pptx
copa-ii.pptxcopa-ii.pptx
copa-ii.pptx
Ā 
Java tut1
Java tut1Java tut1
Java tut1
Ā 
Tutorial java
Tutorial javaTutorial java
Tutorial java
Ā 

More from JP Chicano (15)

cssfasfasfasfas afsasfasfasfas safasfasf
cssfasfasfasfas afsasfasfasfas safasfasfcssfasfasfasfas afsasfasfasfas safasfasf
cssfasfasfasfas afsasfasfasfas safasfasf
Ā 
TRAINING PLAN 1ST Year-1st Sem.dasdasdasdpdf
TRAINING PLAN 1ST Year-1st Sem.dasdasdasdpdfTRAINING PLAN 1ST Year-1st Sem.dasdasdasdpdf
TRAINING PLAN 1ST Year-1st Sem.dasdasdasdpdf
Ā 
TRAINING PLAN 2ND Year-2nd Semgsdgsdg.pdf
TRAINING PLAN 2ND Year-2nd Semgsdgsdg.pdfTRAINING PLAN 2ND Year-2nd Semgsdgsdg.pdf
TRAINING PLAN 2ND Year-2nd Semgsdgsdg.pdf
Ā 
fwfgsdfsagsdag sdgsdagsadf qwf sdafasdaf sdaf
fwfgsdfsagsdag sdgsdagsadf qwf sdafasdaf sdaffwfgsdfsagsdag sdgsdagsadf qwf sdafasdaf sdaf
fwfgsdfsagsdag sdgsdagsadf qwf sdafasdaf sdaf
Ā 
MariaSoccorro-Ramosfagsagsdgsdagdsagsadgsag
MariaSoccorro-RamosfagsagsdgsdagdsagsadgsagMariaSoccorro-Ramosfagsagsdgsdagdsagsadgsag
MariaSoccorro-Ramosfagsagsdgsdagdsagsadgsag
Ā 
1.pdf
1.pdf1.pdf
1.pdf
Ā 
parts of mobo for sharing.pdf
parts of mobo for sharing.pdfparts of mobo for sharing.pdf
parts of mobo for sharing.pdf
Ā 
12786246.ppt
12786246.ppt12786246.ppt
12786246.ppt
Ā 
CSS HTML.pdf
CSS HTML.pdfCSS HTML.pdf
CSS HTML.pdf
Ā 
download.pdf
download.pdfdownload.pdf
download.pdf
Ā 
MIS.ppt
MIS.pptMIS.ppt
MIS.ppt
Ā 
11.ppt
11.ppt11.ppt
11.ppt
Ā 
a.pdf
a.pdfa.pdf
a.pdf
Ā 
oopsinvb-191021101327.pdf
oopsinvb-191021101327.pdfoopsinvb-191021101327.pdf
oopsinvb-191021101327.pdf
Ā 
3306565.ppt
3306565.ppt3306565.ppt
3306565.ppt
Ā 

Recently uploaded

Call Girls Jp Nagar Just Call šŸ‘— 7737669865 šŸ‘— Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call šŸ‘— 7737669865 šŸ‘— Top Class Call Girl Service Bang...Call Girls Jp Nagar Just Call šŸ‘— 7737669865 šŸ‘— Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call šŸ‘— 7737669865 šŸ‘— Top Class Call Girl Service Bang...
amitlee9823
Ā 
Nelamangala Call Girls: šŸ“ 7737669865 šŸ“ High Profile Model Escorts | Bangalore...
Nelamangala Call Girls: šŸ“ 7737669865 šŸ“ High Profile Model Escorts | Bangalore...Nelamangala Call Girls: šŸ“ 7737669865 šŸ“ High Profile Model Escorts | Bangalore...
Nelamangala Call Girls: šŸ“ 7737669865 šŸ“ High Profile Model Escorts | Bangalore...
amitlee9823
Ā 
Call Girls Hebbal Just Call šŸ‘— 7737669865 šŸ‘— Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call šŸ‘— 7737669865 šŸ‘— Top Class Call Girl Service BangaloreCall Girls Hebbal Just Call šŸ‘— 7737669865 šŸ‘— Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call šŸ‘— 7737669865 šŸ‘— Top Class Call Girl Service Bangalore
amitlee9823
Ā 
Insurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usageInsurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usage
Matteo Carbone
Ā 
Chandigarh Escorts Service šŸ“ž8868886958šŸ“ž JustšŸ“² Call Nihal Chandigarh Call Girl...
Chandigarh Escorts Service šŸ“ž8868886958šŸ“ž JustšŸ“² Call Nihal Chandigarh Call Girl...Chandigarh Escorts Service šŸ“ž8868886958šŸ“ž JustšŸ“² Call Nihal Chandigarh Call Girl...
Chandigarh Escorts Service šŸ“ž8868886958šŸ“ž JustšŸ“² Call Nihal Chandigarh Call Girl...
Sheetaleventcompany
Ā 
Call Girls Electronic City Just Call šŸ‘— 7737669865 šŸ‘— Top Class Call Girl Servi...
Call Girls Electronic City Just Call šŸ‘— 7737669865 šŸ‘— Top Class Call Girl Servi...Call Girls Electronic City Just Call šŸ‘— 7737669865 šŸ‘— Top Class Call Girl Servi...
Call Girls Electronic City Just Call šŸ‘— 7737669865 šŸ‘— Top Class Call Girl Servi...
amitlee9823
Ā 
Russian Call Girls In Gurgaon ā¤ļø8448577510 āŠ¹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ā¤ļø8448577510 āŠ¹Best Escorts Service In 24/7 Delh...Russian Call Girls In Gurgaon ā¤ļø8448577510 āŠ¹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ā¤ļø8448577510 āŠ¹Best Escorts Service In 24/7 Delh...
lizamodels9
Ā 
Call Girls In Noida 959961āŠ¹3876 Independent Escort Service Noida
Call Girls In Noida 959961āŠ¹3876 Independent Escort Service NoidaCall Girls In Noida 959961āŠ¹3876 Independent Escort Service Noida
Call Girls In Noida 959961āŠ¹3876 Independent Escort Service Noida
dlhescort
Ā 
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
dlhescort
Ā 

Recently uploaded (20)

Call Girls Jp Nagar Just Call šŸ‘— 7737669865 šŸ‘— Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call šŸ‘— 7737669865 šŸ‘— Top Class Call Girl Service Bang...Call Girls Jp Nagar Just Call šŸ‘— 7737669865 šŸ‘— Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call šŸ‘— 7737669865 šŸ‘— Top Class Call Girl Service Bang...
Ā 
Falcon's Invoice Discounting: Your Path to Prosperity
Falcon's Invoice Discounting: Your Path to ProsperityFalcon's Invoice Discounting: Your Path to Prosperity
Falcon's Invoice Discounting: Your Path to Prosperity
Ā 
Business Model Canvas (BMC)- A new venture concept
Business Model Canvas (BMC)-  A new venture conceptBusiness Model Canvas (BMC)-  A new venture concept
Business Model Canvas (BMC)- A new venture concept
Ā 
Nelamangala Call Girls: šŸ“ 7737669865 šŸ“ High Profile Model Escorts | Bangalore...
Nelamangala Call Girls: šŸ“ 7737669865 šŸ“ High Profile Model Escorts | Bangalore...Nelamangala Call Girls: šŸ“ 7737669865 šŸ“ High Profile Model Escorts | Bangalore...
Nelamangala Call Girls: šŸ“ 7737669865 šŸ“ High Profile Model Escorts | Bangalore...
Ā 
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableCall Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Ā 
šŸ‘‰Chandigarh Call Girls šŸ‘‰9878799926šŸ‘‰Just CallšŸ‘‰Chandigarh Call Girl In Chandiga...
šŸ‘‰Chandigarh Call Girls šŸ‘‰9878799926šŸ‘‰Just CallšŸ‘‰Chandigarh Call Girl In Chandiga...šŸ‘‰Chandigarh Call Girls šŸ‘‰9878799926šŸ‘‰Just CallšŸ‘‰Chandigarh Call Girl In Chandiga...
šŸ‘‰Chandigarh Call Girls šŸ‘‰9878799926šŸ‘‰Just CallšŸ‘‰Chandigarh Call Girl In Chandiga...
Ā 
Eluru Call Girls Service ā˜Ž ļø93326-06886 ā¤ļøā€šŸ”„ Enjoy 24/7 EscortĀ Service
Eluru Call Girls Service ā˜Ž ļø93326-06886 ā¤ļøā€šŸ”„ Enjoy 24/7 EscortĀ ServiceEluru Call Girls Service ā˜Ž ļø93326-06886 ā¤ļøā€šŸ”„ Enjoy 24/7 EscortĀ Service
Eluru Call Girls Service ā˜Ž ļø93326-06886 ā¤ļøā€šŸ”„ Enjoy 24/7 EscortĀ Service
Ā 
It will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayIt will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 May
Ā 
Call Girls Hebbal Just Call šŸ‘— 7737669865 šŸ‘— Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call šŸ‘— 7737669865 šŸ‘— Top Class Call Girl Service BangaloreCall Girls Hebbal Just Call šŸ‘— 7737669865 šŸ‘— Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call šŸ‘— 7737669865 šŸ‘— Top Class Call Girl Service Bangalore
Ā 
Insurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usageInsurers' journeys to build a mastery in the IoT usage
Insurers' journeys to build a mastery in the IoT usage
Ā 
Chandigarh Escorts Service šŸ“ž8868886958šŸ“ž JustšŸ“² Call Nihal Chandigarh Call Girl...
Chandigarh Escorts Service šŸ“ž8868886958šŸ“ž JustšŸ“² Call Nihal Chandigarh Call Girl...Chandigarh Escorts Service šŸ“ž8868886958šŸ“ž JustšŸ“² Call Nihal Chandigarh Call Girl...
Chandigarh Escorts Service šŸ“ž8868886958šŸ“ž JustšŸ“² Call Nihal Chandigarh Call Girl...
Ā 
Call Girls Electronic City Just Call šŸ‘— 7737669865 šŸ‘— Top Class Call Girl Servi...
Call Girls Electronic City Just Call šŸ‘— 7737669865 šŸ‘— Top Class Call Girl Servi...Call Girls Electronic City Just Call šŸ‘— 7737669865 šŸ‘— Top Class Call Girl Servi...
Call Girls Electronic City Just Call šŸ‘— 7737669865 šŸ‘— Top Class Call Girl Servi...
Ā 
Russian Call Girls In Gurgaon ā¤ļø8448577510 āŠ¹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ā¤ļø8448577510 āŠ¹Best Escorts Service In 24/7 Delh...Russian Call Girls In Gurgaon ā¤ļø8448577510 āŠ¹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ā¤ļø8448577510 āŠ¹Best Escorts Service In 24/7 Delh...
Ā 
Monthly Social Media Update April 2024 pptx.pptx
Monthly Social Media Update April 2024 pptx.pptxMonthly Social Media Update April 2024 pptx.pptx
Monthly Social Media Update April 2024 pptx.pptx
Ā 
Call Girls In Noida 959961āŠ¹3876 Independent Escort Service Noida
Call Girls In Noida 959961āŠ¹3876 Independent Escort Service NoidaCall Girls In Noida 959961āŠ¹3876 Independent Escort Service Noida
Call Girls In Noida 959961āŠ¹3876 Independent Escort Service Noida
Ā 
A DAY IN THE LIFE OF A SALESMAN / WOMAN
A DAY IN THE LIFE OF A  SALESMAN / WOMANA DAY IN THE LIFE OF A  SALESMAN / WOMAN
A DAY IN THE LIFE OF A SALESMAN / WOMAN
Ā 
Cracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptxCracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptx
Ā 
How to Get Started in Social Media for Art League City
How to Get Started in Social Media for Art League CityHow to Get Started in Social Media for Art League City
How to Get Started in Social Media for Art League City
Ā 
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
Ā 
Uneak White's Personal Brand Exploration Presentation
Uneak White's Personal Brand Exploration PresentationUneak White's Personal Brand Exploration Presentation
Uneak White's Personal Brand Exploration Presentation
Ā 

13665449.ppt

  • 1. A290 TOOLS IN COMPUTING JavaScript Fundamentals Week 1 Class 2
  • 2. Before you start ā—¦ Make sure to review chapter 2 from the the Elequent JavaScript (2nd edition) book before you start this presentation. ā—¦ There is a compressed (.zip) file called week1class2examples in the class website schedule page. Download that file, which has all the examples necessary for this presentation. ā—¦Unzip the folder and save it to your computer. ā—¦Make sure to read the slides and the below notes carefully. There are 5 assignment questions hidden in this presentation. 2
  • 3. JavaScript Basics <script> </script> tags are used to embed JS code within HTML file JS allows control of HTML documents and behavior of those documents 1. Control document appearance and content (create dynamic and conditional HTML) 2. Control the browser (e.g. pop up dialog boxes) 3. Interact with HTML forms (e.g. read and write user inputs) 4. Interact with the user (e.g. event handlers) 5. Read and write client state with cookies (cookies allow a web page to remember things about the client- e.g. user preference about the colors and layout) Example: Interacting with a user >> When a user clicks on a button, a particular event occurs: 1. Lets see an example 2. Go to the same link, and explore and apply different events. (Here are some other events you can use: https://www.w3schools.com/js/js_events.asp) 3
  • 4. Lexical Structure of JS JS is case sensitive: For example, myName and myname are two different variables. JS ignores whitespace between lines of code ā—¦ Try creating spaces in an example 4
  • 5. Comments Comments: // This is a single line comment /* This is a multi line comment*/ ā—¦ Open the 1example.html file with a text editor and write comments for what each line does. This is question1 for assignment1. ā—¦ Create a folder named assignment1 and put this 1example.html file in that folder with your comments. You will be asked to complete other tasks. Put all those files in this same folder. 5
  • 6. Identifier Identifier: An identifier is simply a name. When we declare a variable ā€œnumberā€, we are creating an identifier for the number variable. ā—¦ In JS, identifiers are used to name variables and functions, and to provide labels for certain loops ā—¦ Reserved names cannot be used as an identifier (e.g. var, break, continue, new) 6
  • 7. Data Types and Values The types of values that can be represented and manipulated in a programming language are known as data types. JS Primitive Data Types ā—¦ Numbers (e.g. 3, 2.15, 345) ā—¦ Strings (e.g. Hello World) ā—¦ Boolean (true, false) ā—¦ NULL (a special value that indicates no value) ā—¦ Undefined (a variable that has been declared but no value assigned) (e.g. var name) ā—¦ NaN JS Reference Data Types ā—¦ Objects (e.g. document object) ā—¦ Function (functions are true values in JS that can be stored in variables, arrays, and objects, and can be passed as arguments to other functions) 7
  • 8. Variables Declaration var i; var name, lastname; var quote=ā€˜The goal of education is not to increase the amount of knowledge but to create the possibilities for a child to invent and discover. Piagetā€™; Scope: The scope of a variable is the region of your program in which it is defined. ā—¦ Global variable: It is defined everywhere in your code ā—¦ Local variable: Variables declared within a function ā—¦ See 2example.html 8
  • 9. Undeclared VS Unassigned Variable var x; //declare an unassigned variable. Its value is undefined alert(u); //using an undeclared variable causes an error //alert is a JS method. Go ahead and test it var u=3; //assigning a value to an undeclared variable creates the variable 9
  • 10. Primitive and Reference Types A primitive type has a fixed size in memory. ā—¦ Examples: ā—¦ A number occupies eight bytes of memory ā—¦ A Boolean value can be represented with only one bit 10
  • 11. Primitive and Reference Types Reference types do not have a fixed size. ā—¦ Examples: ā—¦ An array can have any number of elements ā—¦ A function can contain any amount of JS code ā—¦ Since these types do not have a fixed size, their values cannot be stored directly in the eight bytes of memory associated with each variable. Instead, the variable stores a reference to a value. Typically, this reference is some sort of pointer or memory address. It is not the data value itself, but it tells the variable where to look to find the value. Assignment Question #2: See the notes section of this slide. 11
  • 12. Variables as Properties of the Global Object Variables in JS are fundamentally the same as object properties. In JS, the window object serves as the global object for all JS code contained in the browser window it represents. When you declare a global JS variable, you are actually defining a property of the global window object. Window object also has its own properties ā—¦ See window object properties: https://www.w3schools.com/jsref/obj_window.asp 12
  • 13. Expressions and Operators An expression is a phrase of JS that a JS interpreter can evaluate to produce a value Examples 3.17 //a numerical literal ā€œThis is a stringā€ // a string literal [3,6,9,21,15] //an array literal Name //the name variable result //the result variable 13
  • 14. Operator Behaviors Some operators expect certain data types ā—¦ ā€˜xā€™ * ā€˜yā€™ //this is not legal in JS However, in some instances JS tries to convert expressions to the appropriate type whenever possible: ā—¦ ā€˜3ā€™ * ā€˜7ā€™ // the result is 21 Some operators act differently in different expressions ā—¦ + operator adds numeric values but concatenates string values 14
  • 15. Operator Precedence Operator precedence controls the order in which operations are performed. ā—¦ Ex: result= 2+4*4; //outputs 18 The multiplication operator has a higher precedence than the addition operator +. Operator precedence can be overwritten with the explicit use of parentheses. ā—¦ Ex: result=(2+4)*4 //outputs 24 ā—¦ See more: http://www.javascriptkit.com/jsref/precedence_operators.shtml 15
  • 16. Arithmetic Operators Addition + Subtraction - Multiplication * Division / Modulo % (returns the reminder: 7%3 //returns 1 ) Unary minus ā€“ (converts a positive value to negative: -4) Increment ++ (adds 1: x=3; x++ //returns 4) Decrement ā€“ (subtracts 1: x=3; x-- //returns 2) See 3example.html 16
  • 17. Equality Operators These are operators that compare two values to determine whether they are the same or different, and return a Boolean value (true or false) They are commonly used in control or flow statements Be careful, = is not an equality operator. It is an assignment operator for assigning values. 17
  • 18. Equality Operators == checks whether two variables are same in value ā—¦ EX: if (x==y) alert(message); === checks whether two variables are identical (same value and data type) ā—¦ EX: if (x===y) alert(message); See 4example.html and 5example.html 18
  • 19. Equality Operators != checks whether two variables are not equal ā—¦ EX: if (x!=y) //returns true when the variables are not equal !== checks whether two variables are not identical ā—¦ EX: if (x!==y) //returns true when the variables are not identical 19
  • 20. Relational Operators >> Comparison Less than < Greater than > Less than or equal <= Greater than or equal >= 20
  • 21. String Operators The + operator can be used to add (concatenate) strings. cont=ā€Merhabaā€+ā€ DĆ¼nyaā€; //This is Hello World in Turkish. The += assignment operator can be used to add (concatenate) strings txt1 = "What a very "; txt1 += "nice day"; Adding two numbers will return the sum, but adding a number and a string will return a string: var z = "Hello" + 5; document.write(z); 21
  • 22. Logical Operators && and || or ! not See 6example.html 22
  • 23. Assignment Operator varia= 5; varia+=5; // this is equal to var=var+5; This is addition and assignment together. This is same with others such as -=, *=, /=, %= See 7example.html 23
  • 24. Other operators we will apply later The in operator returns true if the specified property is in the specified object. The typeof operator returns the type of a variable, object, function or expression ā—¦ See the 8example.html example The delete operator deletes a property from an object. The instanceof operator returns true if the specified object is an instance of the specified object. The void operator evaluates an expression and returns undefined. This operator is often used to obtain the undefined primitive value, using "void(0)ā€ The new operator creates a new object and invokes a constructor function to initialize it. Ternary operator ?: //used to shorten conditional statements [] and . are both operators to access elements of an object 24
  • 25. Assignment Question #4 Open the 9example.html file There are three errors in that code. Find and correct the errors and write comments for each error in the script. Submit the corrected/commented file with other files in the assignment1 folder. 25
  • 26. Assignment Question #5 Write a script that converts a user entered Fahrenheit degree to Celsius. ā—¦ You need to create an html page with the .js file included with the following line of code: <script type="application/javascript" src=ā€question5.js"> </script>. Hints ā—¦ The user will enter fahrenheit value in a text box (hint: prompt) ā—¦ An alert box will show the celsius value of the user input ā—¦ Ex: 32 fahrenheit is 0 degrees Celsius ā—¦ Equation is Ā°C = 5/9 (Ā°F - 32). ā—¦ Name the html and js files as question5.html and question5.js ā—¦ Put them with the other assignment files in the same folder. 26
  • 27. Assignment #1 ā—¦ There are 5 questions in this assignment. ā—¦ Q1: 20 points Q2: 10 points Q3: 10 points Q4: 20 points Q5: 30 points ā—¦ You need to put all the html and js files, and the presentation file in the same folder. ā—¦ We will be looking at the notes section of the presentation file for the conceptual questionsā€™ answers. ā—¦ Then, compress all your work (zip) and submit your work in Canvas assignments. ā—¦ You are allowed to work in teams. Make sure to include both team membersā€™ last names in the submission file. 27

Editor's Notes

  1. YOUR ASSIGNMENT QUESTIONS CAN BE HIDDEN IN THE SLIDES THE NOTES ARE HERE THE EXAMPLES
  2. Lexical Structure: set of elementary rules that specifies how you write programs in that language
  3. Strings are unusual. Even though they behave as primitive data type in many ways, they cannot be stored in directly in fixed-size variables and for efficiency we would expect JSS to copy references to strings, not the actual content of strings. Question #2 (ANSWER THIS QUESTION HERE IN THE NOTES SECTION OF THIS SLIDE): a) Explain what reference type is with your own words? Give an example. b) What is garbage collection?
  4. In the global execution context (outside of any function), this refers to the global object. Inside a function, the value ofĀ thisĀ depends on how the function is called.
  5. Assignment Question #3: What is the output of the following? (Explain your answer in the notes section here) var z = "Hello" + 5; document.write(z); ?