190 likes | 307 Views
2440: 211 Interactive Web Programming. Expressions & Operators. Expressions. Literal value or variable or combination of literal values, variables, operators, and other expressions that can be evaluated to produce a result Literal – value such as literal string or a number
E N D
2440: 211 Interactive Web Programming Expressions & Operators
Expressions • Literal value or variable or combination of literal values, variables, operators, and other expressions that can be evaluated to produce a result • Literal – value such as literal string or a number • Operands – variables and literals in an expression • Operator – symbols used in expressions to manipulate operands • E.g. addition (+), multiplication (*), etc • Variable – named memory location for storing data Expressions & Operators
Variables • A named memory location for storing data • Identifier – names used for variables, functions, and objects • Rules for naming a variable include the following: • First letter must be a letter or an underscore (_) • The other characters can be letters, numbers, or underscore (_) • Spaces cannot be included • Reserved words (keywords) cannot be used Expressions & Operators
JavaScript Keywords Expressions & Operators
Variable Declaration • Creating a variable for usage • The keyword var is used to declare variables, although not required • E.g. var name; Expressions & Operators
Variable Assignment • Storing data in a variable • Data is stored with the assignment operator (=) • E.g. name = “John Doe”; Expressions & Operators
Variable Initialization • Declaring and assigning data to a variable at the same time • E.g. var name = “John Doe”; Expressions & Operators
Data Types • Category of information stored in a variable • JavaScript supports the following data types: • Numeric – any positive or negative number • Integer numbers – whole numbers • Floating-point numbers – exponents or decimal numbers • Boolean – logical value of true or false • String – text values like “Hello” enclosed in either double (“) or single quotes (‘) • Null – an empty value • Undefined – variables with no assigned value, not declared, or does not exist • Reference – for functions, objects and arrays • JavaScript is loosely typed – does not require the data type declaration • Strongly typed languages – require data type declaration for variables Expressions & Operators
Operators • Symbols used in expressions to manipulate operands • E.g. addition (+), multiplication (*), etc • JavaScript operators are unary or binary • Unary – requires a single operand • Binary – requires an operand before and after the operator • JavaScript operator types include: • Arithmetic – used for mathematical calculations • Assignment – assigns values to variables • Comparison – compares operands and returns a Boolean value • Logical – used for boolean operations • String – used on strings • Special – used for various purposes Expressions & Operators
Arithmetic Operators • Used to perform mathematical calculations • Can be binary or unary • Binary – requires two operands • Unary – requires a single operand • Below are the arithmetic binary operators Expressions & Operators
Arithmetic Operators… • Below are the arithmetic unary operators • The increment and decrement unary operators can be used as prefix or postfix operators • Prefix – placed before a variable • Variable is increased by one before value is used • E.g. ++myVariable, --myVariable • Postfix – placed after a variable • Variable is increased by one after value is used • E.g. myVariable++, myVariable-- Expressions & Operators
Assignment Operators • Used for assigning a value to a variable • Below are the assignment operators Expressions & Operators
Comparison Operators • Used to compare two operands for a returned value of true or false • Below are the comparison operators Expressions & Operators
Conditional Operator • Executes one of two expressions, based on the results of a conditional expression • Syntax: condition ? expression1 : expression2 • If the condition is true, expression1 executes, else expression2 executes • E.g. document.write(4<5 ? ”Correct!” : ”Incorrect”); Expressions & Operators
Logical Operators • Used for comparing two Boolean operands for equality • Below are the JavaScript logical operators Expressions & Operators
String Operators • Used on strings • Two string operators used in JavaScript are: + and += • Concatenation operator (+) – used to combine two strings • E.g. “Hello “ + “World” • Compound assignment operator (+=) – used to combine strings • E.g. var greeting = “Hello “; greeting += “everyone”; Expressions & Operators
String Escape Sequences • Combining the backslash (\) with other special characters for a special purpose • Several of the escape sequences are only recognized inside the <pre> tag • Below are some of the JavaScript escape sequences Expressions & Operators
Special JavaScript Operators Expressions & Operators
Operator Precedence • The order in which operations in an expression are evaluated • When operators are in the same precedence group, the order is determined by associativity (left-to-right or right-to-left) • Associativity – the order in which operators of equal precedence execute Expressions & Operators