SlideShare a Scribd company logo
1 of 33
Download to read offline
Chapter 17
Fundamental Concepts Expressed in JavaScript
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Programming Concepts
• Programming is the act of writing an
algorithm or program
• JavaScript is an embedded programming
language
– JavaScript lives in a <script> element in a web
page
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
JavaScript Runner Page
We can test JavaScript statements here:
http://www.headfirstlabs.com/books/hfjs/
hfjs_handson.php
you can type in JavaScript code and run it
directly in your web browser. You don't
even have to create a file—just enter your
code and click the Run button.
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
JavaScript Variables
• In programming terminology, variables are
storage locations that hold values and
have a name
• The values can change => vary
• To change the value of a variable:
– assignment statement
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Variable Identifier Rules
• a variable’s name is called an identifier
• must begin with a letter, followed by any
sequence of letters, numerals, or the
underscore symbol
• Identifiers are not allowed to contain
spaces, punctuation, etc.
• Identifiers are case sensitive
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Variable Declaration Statement
var area, radius;
• This command declares that two identifiers
(area, radius) will be used as variables
• Every variable used must be declared
• Always use the var keyword
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Statement Terminator (;)
• A program is simply a list of statements;
• Each statement must be terminated by a
semicolon;
• JavaScript provides a feature called
Automatic Semicolon Insertion (ASI);
• There are rarely problems in omitting
semicolons;
• Best practice:
always include semicolons;
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Undefined Values
• The declaration states that the identifier is
the name of a variable
• The name has no value at first, it is not
defined
• It is a name that doesn’t name anything
• The name is declared but there is no value
assigned yet
– The value is undefined
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Initializing a Declaration
• Declaring variables with initial values
var taxRate = .088;
var balanceDue = 0;
var dayOfWeek;
• dayOfWeek has a special value
The value is undefined
• This works, too:
var taxRate = .088, balanceDue = 0;
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Three Basic Data Types of
JavaScript
• There are three types of data in JavaScript
programs that will be used in this book:
1. numbers,
2. strings (a.k.a. text)
3. booleans
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Numbers
2
3.14159
-23
4900000000
Math.random();
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Strings
• “Abc”
• “Z”
• “”
• If our string contains single quotes, enclose
it in double quotes:
var book = "That’s a great quote!";
– quote swapping
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Boolean Values
• There are only two Boolean values:
true and false
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
The Assignment Statement
• If variables are to change values in an
algorithm or program, there should be a
command to do so
• The assignment statement changes a
variable’s value
• An assignment statement has three parts
that always occur in this order:
<variable> <assignment symbol> <expression>;
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Assignment Symbol
• Different programming languages use
different symbols for indicating assignment
• The three most widely used symbols are:
– The equal sign (=)
– The colon/equal sign pair (:=)
– The left pointing arrow (←)
• An example of an assignment is:
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Interpreting an Assignment
Statement
• To understand how assignment works, you
must think of a value flowing from the right
side (expression side) to the left side
(variable side)
• The assignment symbol should be read as
“is assigned ” or “becomes” or “gets”
• These terms emphasize the role that the
assignment symbol plays in changing the
value of the variable named
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Interpreting an Assignment
Statement
• example
var days = 44;
var weeks = days / 7;
alert(weeks);
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
An Expression and Its Syntax
• expressions are anything that give you a value
• most expressions are algebra-like formulas
• Expressions are built of variables and operators:
– addition (+) and subtraction(–)
– multiplication (*) and division (/ )
• These are called the arithmetic operators
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Arithmetic Operators
+, -, *, /, %
• The modulus (mod) operation (%) divides
two integers and returns the remainder
– The result of a % b is the remainder of integer
division a / b
– Examples:
4 % 2 => 0
5 % 2 => 1
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Relational Operators
• Relational operators make
comparisons between numerical values
• The outcome of the
comparison is a Boolean
value of true or false
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Logical Operators: &&
• Logical AND
– The && is the logical AND operator
– The outcome of a && b is true if both a and b
are true; otherwise, it is false
var class = “199”, today = “Fri”;
alert((class == “110”) && (today == “Wed”))
//this alert displays false
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Logical AND
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Logical Operators: ||
• Logical OR
– The outcome of a || b is true if:
• either a is true or b is true
• if they are both true
– It is false only if both are false
• && and || have lower precedence than the
relational operators
– Relational operators are always evaluated before logic
operators
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Logical Operators: !
• logical NOT (!)
– It is a unary operator, taking only a single
operand
– Its outcome is the opposite of the value of its
operand
– This alert will display true
var class = “CIS 110”;
alert(!(class == “CIS 199”));
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Operator Overload (+)
• Operator overload is a technical term
meaning the “use of an operator with
different data types”
• Operators usually apply to a single data
type
– 4 + 5 produces the numerical result of 9
• If operands are strings, what does the
+ mean?
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Concatenation
• + joins strings => concatenation
• The meaning of + is overloaded:
2 + 2 => 4, a number
“2” + “2” => “22”, a string
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Conditional Statement: IF
var waterTemp = parseInt(prompt(‘enter temp’));
if (waterTemp < 32)
waterState = "Frozen";
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
if/else Statements
if (<Boolean expression>)
<then-statement>;
else
<else-statement>;
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
if with else
var waterTemp = parseInt(prompt(‘enter temp’));
if (waterTemp < 32)
waterState = "Frozen";
else
waterState = " Liquid ";
alert(" state = " + waterState);
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Statement Block: { }
var waterTemp = parseInt(prompt(‘enter temp’));
if (waterTemp < 32) {
waterState = "Frozen";
alert(" state = " + waterState);
}
else {
waterState = " Liquid ";
alert(" state = " + waterState);
}
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
WaterStates Problem
Write a JavaScript that recognizes the
states of water
Embed your script in a web page
Wednesday, April 9, 14
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Wednesday, April 9, 14

More Related Content

Viewers also liked

Naina gupta ; compensation plan in indian airways
Naina gupta ; compensation plan in indian airwaysNaina gupta ; compensation plan in indian airways
Naina gupta ; compensation plan in indian airwaysNaina Gupta
 
The function of the heart
The function of the heartThe function of the heart
The function of the heartLouise Crocombe
 
TAR Lazio: il Ministro dell'Interno ed il Prefetto non possono annullare le t...
TAR Lazio: il Ministro dell'Interno ed il Prefetto non possono annullare le t...TAR Lazio: il Ministro dell'Interno ed il Prefetto non possono annullare le t...
TAR Lazio: il Ministro dell'Interno ed il Prefetto non possono annullare le t...Mario Di Carlo
 
Rtdna facebook 082313_final
Rtdna facebook 082313_finalRtdna facebook 082313_final
Rtdna facebook 082313_finalDonna Petersen
 
Direct marketing of agriculture produce Training Program at Barabanki
Direct marketing of agriculture produce Training Program at BarabankiDirect marketing of agriculture produce Training Program at Barabanki
Direct marketing of agriculture produce Training Program at Barabankipromptinfotech
 
Manpower || Paccific Agro Lucknow Pvt Ltd.
Manpower || Paccific Agro Lucknow Pvt Ltd.Manpower || Paccific Agro Lucknow Pvt Ltd.
Manpower || Paccific Agro Lucknow Pvt Ltd.promptinfotech
 
Разумное потребление тепла.
Разумное потребление тепла.Разумное потребление тепла.
Разумное потребление тепла.Yuliya Kyamileva
 
Lawrence house funds
Lawrence house fundsLawrence house funds
Lawrence house fundshousefunds57
 
Ch. 4 FIT5, CIS 110 13F
Ch. 4 FIT5, CIS 110 13FCh. 4 FIT5, CIS 110 13F
Ch. 4 FIT5, CIS 110 13Fmh-108
 
Práctica cero a la oportunidad de empresa
Práctica cero a la oportunidad de empresaPráctica cero a la oportunidad de empresa
Práctica cero a la oportunidad de empresaMichael Julca Requejo
 
Безопасная одежда
Безопасная одеждаБезопасная одежда
Безопасная одеждаYuliya Kyamileva
 
Project on elements of talent managagement
Project on elements of talent managagementProject on elements of talent managagement
Project on elements of talent managagementDeepak Rai
 
Экологическое воспитание. Правильная версия.
Экологическое воспитание. Правильная версия.Экологическое воспитание. Правильная версия.
Экологическое воспитание. Правильная версия.Yuliya Kyamileva
 
«Натуральные бытовые средства»
«Натуральные бытовые средства»«Натуральные бытовые средства»
«Натуральные бытовые средства»Yuliya Kyamileva
 

Viewers also liked (20)

Naina gupta ; compensation plan in indian airways
Naina gupta ; compensation plan in indian airwaysNaina gupta ; compensation plan in indian airways
Naina gupta ; compensation plan in indian airways
 
The function of the heart
The function of the heartThe function of the heart
The function of the heart
 
TAR Lazio: il Ministro dell'Interno ed il Prefetto non possono annullare le t...
TAR Lazio: il Ministro dell'Interno ed il Prefetto non possono annullare le t...TAR Lazio: il Ministro dell'Interno ed il Prefetto non possono annullare le t...
TAR Lazio: il Ministro dell'Interno ed il Prefetto non possono annullare le t...
 
Rtdna facebook 082313_final
Rtdna facebook 082313_finalRtdna facebook 082313_final
Rtdna facebook 082313_final
 
Direct marketing of agriculture produce Training Program at Barabanki
Direct marketing of agriculture produce Training Program at BarabankiDirect marketing of agriculture produce Training Program at Barabanki
Direct marketing of agriculture produce Training Program at Barabanki
 
Rac lecture 5
Rac lecture 5Rac lecture 5
Rac lecture 5
 
Manpower || Paccific Agro Lucknow Pvt Ltd.
Manpower || Paccific Agro Lucknow Pvt Ltd.Manpower || Paccific Agro Lucknow Pvt Ltd.
Manpower || Paccific Agro Lucknow Pvt Ltd.
 
Разумное потребление тепла.
Разумное потребление тепла.Разумное потребление тепла.
Разумное потребление тепла.
 
Lawrence house funds
Lawrence house fundsLawrence house funds
Lawrence house funds
 
Ch. 4 FIT5, CIS 110 13F
Ch. 4 FIT5, CIS 110 13FCh. 4 FIT5, CIS 110 13F
Ch. 4 FIT5, CIS 110 13F
 
Layer session
Layer sessionLayer session
Layer session
 
Práctica cero a la oportunidad de empresa
Práctica cero a la oportunidad de empresaPráctica cero a la oportunidad de empresa
Práctica cero a la oportunidad de empresa
 
Sim ohms law
Sim ohms lawSim ohms law
Sim ohms law
 
Rac lecture 3
Rac lecture 3Rac lecture 3
Rac lecture 3
 
Al toolz
Al toolzAl toolz
Al toolz
 
Rac lecture 1
Rac  lecture 1Rac  lecture 1
Rac lecture 1
 
Безопасная одежда
Безопасная одеждаБезопасная одежда
Безопасная одежда
 
Project on elements of talent managagement
Project on elements of talent managagementProject on elements of talent managagement
Project on elements of talent managagement
 
Экологическое воспитание. Правильная версия.
Экологическое воспитание. Правильная версия.Экологическое воспитание. Правильная версия.
Экологическое воспитание. Правильная версия.
 
«Натуральные бытовые средства»
«Натуральные бытовые средства»«Натуральные бытовые средства»
«Натуральные бытовые средства»
 

More from mh-108

Ch. 16 Database Case Study: XML/XSLT
Ch. 16 Database Case Study: XML/XSLTCh. 16 Database Case Study: XML/XSLT
Ch. 16 Database Case Study: XML/XSLTmh-108
 
Ch. 15 FIT5, CIS 110 13F
Ch. 15 FIT5, CIS 110 13FCh. 15 FIT5, CIS 110 13F
Ch. 15 FIT5, CIS 110 13Fmh-108
 
Ch. 10 FIT5, CIS 110 13F
Ch. 10 FIT5, CIS 110 13FCh. 10 FIT5, CIS 110 13F
Ch. 10 FIT5, CIS 110 13Fmh-108
 
Ch. 12 FIT5, CIS 110 13F
Ch. 12 FIT5, CIS 110 13FCh. 12 FIT5, CIS 110 13F
Ch. 12 FIT5, CIS 110 13Fmh-108
 
Ch. 7 FIT5, CIS 110 13F
Ch. 7 FIT5, CIS 110 13FCh. 7 FIT5, CIS 110 13F
Ch. 7 FIT5, CIS 110 13Fmh-108
 
Ch. 3 HTML5, CIS 110 13F
Ch. 3 HTML5, CIS 110 13FCh. 3 HTML5, CIS 110 13F
Ch. 3 HTML5, CIS 110 13Fmh-108
 
Ch. 3 FIT5, CIS 110 13F
Ch. 3 FIT5, CIS 110 13FCh. 3 FIT5, CIS 110 13F
Ch. 3 FIT5, CIS 110 13Fmh-108
 
FIT5 Ch. 5, CIS 110 13F
FIT5 Ch. 5, CIS 110 13FFIT5 Ch. 5, CIS 110 13F
FIT5 Ch. 5, CIS 110 13Fmh-108
 

More from mh-108 (8)

Ch. 16 Database Case Study: XML/XSLT
Ch. 16 Database Case Study: XML/XSLTCh. 16 Database Case Study: XML/XSLT
Ch. 16 Database Case Study: XML/XSLT
 
Ch. 15 FIT5, CIS 110 13F
Ch. 15 FIT5, CIS 110 13FCh. 15 FIT5, CIS 110 13F
Ch. 15 FIT5, CIS 110 13F
 
Ch. 10 FIT5, CIS 110 13F
Ch. 10 FIT5, CIS 110 13FCh. 10 FIT5, CIS 110 13F
Ch. 10 FIT5, CIS 110 13F
 
Ch. 12 FIT5, CIS 110 13F
Ch. 12 FIT5, CIS 110 13FCh. 12 FIT5, CIS 110 13F
Ch. 12 FIT5, CIS 110 13F
 
Ch. 7 FIT5, CIS 110 13F
Ch. 7 FIT5, CIS 110 13FCh. 7 FIT5, CIS 110 13F
Ch. 7 FIT5, CIS 110 13F
 
Ch. 3 HTML5, CIS 110 13F
Ch. 3 HTML5, CIS 110 13FCh. 3 HTML5, CIS 110 13F
Ch. 3 HTML5, CIS 110 13F
 
Ch. 3 FIT5, CIS 110 13F
Ch. 3 FIT5, CIS 110 13FCh. 3 FIT5, CIS 110 13F
Ch. 3 FIT5, CIS 110 13F
 
FIT5 Ch. 5, CIS 110 13F
FIT5 Ch. 5, CIS 110 13FFIT5 Ch. 5, CIS 110 13F
FIT5 Ch. 5, CIS 110 13F
 

Recently uploaded

Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 

Recently uploaded (20)

Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 

Ch. 17 FIT5, CIS 110 13F

  • 1. Chapter 17 Fundamental Concepts Expressed in JavaScript Wednesday, April 9, 14
  • 2. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Programming Concepts • Programming is the act of writing an algorithm or program • JavaScript is an embedded programming language – JavaScript lives in a <script> element in a web page Wednesday, April 9, 14
  • 3. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley JavaScript Runner Page We can test JavaScript statements here: http://www.headfirstlabs.com/books/hfjs/ hfjs_handson.php you can type in JavaScript code and run it directly in your web browser. You don't even have to create a file—just enter your code and click the Run button. Wednesday, April 9, 14
  • 4. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley JavaScript Variables • In programming terminology, variables are storage locations that hold values and have a name • The values can change => vary • To change the value of a variable: – assignment statement Wednesday, April 9, 14
  • 5. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Variable Identifier Rules • a variable’s name is called an identifier • must begin with a letter, followed by any sequence of letters, numerals, or the underscore symbol • Identifiers are not allowed to contain spaces, punctuation, etc. • Identifiers are case sensitive Wednesday, April 9, 14
  • 6. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Variable Declaration Statement var area, radius; • This command declares that two identifiers (area, radius) will be used as variables • Every variable used must be declared • Always use the var keyword Wednesday, April 9, 14
  • 7. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Statement Terminator (;) • A program is simply a list of statements; • Each statement must be terminated by a semicolon; • JavaScript provides a feature called Automatic Semicolon Insertion (ASI); • There are rarely problems in omitting semicolons; • Best practice: always include semicolons; Wednesday, April 9, 14
  • 8. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Undefined Values • The declaration states that the identifier is the name of a variable • The name has no value at first, it is not defined • It is a name that doesn’t name anything • The name is declared but there is no value assigned yet – The value is undefined Wednesday, April 9, 14
  • 9. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Initializing a Declaration • Declaring variables with initial values var taxRate = .088; var balanceDue = 0; var dayOfWeek; • dayOfWeek has a special value The value is undefined • This works, too: var taxRate = .088, balanceDue = 0; Wednesday, April 9, 14
  • 10. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Three Basic Data Types of JavaScript • There are three types of data in JavaScript programs that will be used in this book: 1. numbers, 2. strings (a.k.a. text) 3. booleans Wednesday, April 9, 14
  • 11. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Numbers 2 3.14159 -23 4900000000 Math.random(); Wednesday, April 9, 14
  • 12. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Strings • “Abc” • “Z” • “” • If our string contains single quotes, enclose it in double quotes: var book = "That’s a great quote!"; – quote swapping Wednesday, April 9, 14
  • 13. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Boolean Values • There are only two Boolean values: true and false Wednesday, April 9, 14
  • 14. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Assignment Statement • If variables are to change values in an algorithm or program, there should be a command to do so • The assignment statement changes a variable’s value • An assignment statement has three parts that always occur in this order: <variable> <assignment symbol> <expression>; Wednesday, April 9, 14
  • 15. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Assignment Symbol • Different programming languages use different symbols for indicating assignment • The three most widely used symbols are: – The equal sign (=) – The colon/equal sign pair (:=) – The left pointing arrow (←) • An example of an assignment is: Wednesday, April 9, 14
  • 16. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Interpreting an Assignment Statement • To understand how assignment works, you must think of a value flowing from the right side (expression side) to the left side (variable side) • The assignment symbol should be read as “is assigned ” or “becomes” or “gets” • These terms emphasize the role that the assignment symbol plays in changing the value of the variable named Wednesday, April 9, 14
  • 17. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Interpreting an Assignment Statement • example var days = 44; var weeks = days / 7; alert(weeks); Wednesday, April 9, 14
  • 18. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley An Expression and Its Syntax • expressions are anything that give you a value • most expressions are algebra-like formulas • Expressions are built of variables and operators: – addition (+) and subtraction(–) – multiplication (*) and division (/ ) • These are called the arithmetic operators Wednesday, April 9, 14
  • 19. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Arithmetic Operators +, -, *, /, % • The modulus (mod) operation (%) divides two integers and returns the remainder – The result of a % b is the remainder of integer division a / b – Examples: 4 % 2 => 0 5 % 2 => 1 Wednesday, April 9, 14
  • 20. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Relational Operators • Relational operators make comparisons between numerical values • The outcome of the comparison is a Boolean value of true or false Wednesday, April 9, 14
  • 21. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Logical Operators: && • Logical AND – The && is the logical AND operator – The outcome of a && b is true if both a and b are true; otherwise, it is false var class = “199”, today = “Fri”; alert((class == “110”) && (today == “Wed”)) //this alert displays false Wednesday, April 9, 14
  • 22. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Logical AND Wednesday, April 9, 14
  • 23. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Logical Operators: || • Logical OR – The outcome of a || b is true if: • either a is true or b is true • if they are both true – It is false only if both are false • && and || have lower precedence than the relational operators – Relational operators are always evaluated before logic operators Wednesday, April 9, 14
  • 24. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Logical Operators: ! • logical NOT (!) – It is a unary operator, taking only a single operand – Its outcome is the opposite of the value of its operand – This alert will display true var class = “CIS 110”; alert(!(class == “CIS 199”)); Wednesday, April 9, 14
  • 25. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Operator Overload (+) • Operator overload is a technical term meaning the “use of an operator with different data types” • Operators usually apply to a single data type – 4 + 5 produces the numerical result of 9 • If operands are strings, what does the + mean? Wednesday, April 9, 14
  • 26. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Concatenation • + joins strings => concatenation • The meaning of + is overloaded: 2 + 2 => 4, a number “2” + “2” => “22”, a string Wednesday, April 9, 14
  • 27. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Wednesday, April 9, 14
  • 28. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Conditional Statement: IF var waterTemp = parseInt(prompt(‘enter temp’)); if (waterTemp < 32) waterState = "Frozen"; Wednesday, April 9, 14
  • 29. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley if/else Statements if (<Boolean expression>) <then-statement>; else <else-statement>; Wednesday, April 9, 14
  • 30. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley if with else var waterTemp = parseInt(prompt(‘enter temp’)); if (waterTemp < 32) waterState = "Frozen"; else waterState = " Liquid "; alert(" state = " + waterState); Wednesday, April 9, 14
  • 31. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Statement Block: { } var waterTemp = parseInt(prompt(‘enter temp’)); if (waterTemp < 32) { waterState = "Frozen"; alert(" state = " + waterState); } else { waterState = " Liquid "; alert(" state = " + waterState); } Wednesday, April 9, 14
  • 32. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley WaterStates Problem Write a JavaScript that recognizes the states of water Embed your script in a web page Wednesday, April 9, 14
  • 33. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Wednesday, April 9, 14