Chapter 2 Lesson 3 PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

ICT1512

Chapter 2
Lesson 3

Using Operators to Build


Expressions
Understanding Operator
Precedence
Using Expressions with
Web Form Controls
Locating Errors with the
Browser Console
Objectives
• You will have mastered the material in this lesson
when you can:
– Create expressions using arithmetic, assignment,
comparison, logical, string, and special operators.
– Understand order precedence and associativity of
operations.
– Work with events and values associated with form
controls.
– Access your browser’s debugging console.
Using Operators to Build Expressions
• Two types of JavaScript operators: binary and unary
• Binary operator: requires an operand before and after the
operator – myNumber = 100;
• Unary operator: requires single operand either before or
after operator – myNumber++
Arithmetic Operators

• Arithmetic operators used to perform mathematical


operations

Figure 2-8
Unary Operators
• Two types of unary operators:
– Prefix operators, which are placed before the variable
– Postfix operators, which are placed after the variable
• Prefix operator is applied before assignment operator:
let x = 5;
let y = ++x // x = 6 and y = 6
• Postfix operator is applied after assignment operator:
let x = 5;
let y = x++ // x = 6 and y = 5

Figure 2-9
Assignment Operators
• An assignment operator (e.g., =) is used for assigning a value to a
variable
• Compound assignment operators both assign a value and perform
a calculation
– Interpreter will attempt to convert a nonnumeric to a numeric operand

Figure 2-10
Comparison operators
Comparison operators (relational operators): used to compare two operands
• Two nonnumerical operands are compared in lexicographical order
• String plus number: interpreter converts string to number or returns false

Figure 2-11
Conditional operators

• Conditional operators (ternary operators) return one of


two possible values given the Boolean value of comparison
– Syntax: condition ? trueValue : falseValue;
– Condition can be any expression that equals true or false,
including a Boolean variable
– Can return an expression instead of a value
• Understanding falsy and truthy values
– Falsy values, equivalent to false: "" (empty string), -0, 0, NaN,
null, undefined
– Everything else is a truthy value, equivalent to true
– Can often use truthy and falsy values to make comparison
operations more compact by omitting the comparison operator
Logical operators
– Used to combine expressions that will result in a
Boolean value of true or false
– Used for negating (swapping) a Boolean value
– Multiple conditions can be grouped within parentheses to
create more complicated statements

Figure 2-12
Special Operators

Figure 2-6
Figure 2-13
Understanding Operator Precedence

• Operator precedence determines the order in which


operations in an expression are evaluated
• Associativity determines precedence for operators with
equal intrinsic precedence
• Examples:
– 5 + 2 * 8 evaluates to 21
– 30 / 5 * 2 evaluates to 12
– let x = 3;
let y = 2;
x = y *= ++x; // Value of both x and y is 8
– (5 + 2) * 8 evaluates to 56
Understanding Operator Precedence Continue 1

Figure 2-16
Understanding Operator Precedence Continue 2

Figure 2-17
Locating Errors with the Browser Console

• Accessing the browser console (console) displays


error messages from the browser
• Locating an error in your program
– Browser console reports the line where detected each
error is located
– Also reports lines that failed to run
– Be sure to make permanent corrections to code within
your code editor

You might also like