Chapter 2 Data Types and Operators JavaScript, Third Edition.
-
Upload
betty-mcdaniel -
Category
Documents
-
view
237 -
download
4
Transcript of Chapter 2 Data Types and Operators JavaScript, Third Edition.
![Page 1: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/1.jpg)
Chapter 2
Data Types and Operators
JavaScript, Third Edition
![Page 2: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/2.jpg)
JavaScript, Third Edition 2
Objectives
• Work with variables
• Study data types
• Use expressions and operators
• Work with strings
• Study operator precedence
![Page 3: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/3.jpg)
JavaScript, Third Edition 3
Variables
• Values that a program stores in computer memory
• Similar to a storage locker
• To use in a program:
– Write a statement that creates the variable
– Assign it a name
• Assigning a value to a variable is the same as storing a value in it
![Page 4: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/4.jpg)
JavaScript, Third Edition 4
Variable names
• Name you assign to a variable is called an identifier
• Rules and conventions when naming a variable:
– Identifiers must begin with an uppercase or lowercase ASCII letter, dollar sign ($), or underscore ( _ )
– You can use numbers in an identifier, but not as the first character
– You cannot include spaces in an identifier
– You cannot use reserved words for identifiers
![Page 5: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/5.jpg)
JavaScript, Third Edition 5
Variable Names (Cont.)• Reserved words (keywords):
– Special words part of the JavaScript language
![Page 6: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/6.jpg)
JavaScript, Third Edition 6
Declaring Variables
• In JavaScript, the reserved keyword var is used to create variables
• Creating a variable is called declaring the variable
• Assign a specific value to, or initialize, the variable by using the syntax:
– Var variable_name = value;
• The value assigned to a variable can be a literal string or a numeric value
![Page 7: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/7.jpg)
JavaScript, Third Edition 7
Declaring Variables (Cont.)
• When assigning a literal string value to a variable:
– Enclose text in quotation marks
• When assigning a numeric value to a variable:
– Do not enclose value in quotation marks
• You can declare multiple variables in the statement using a single var keyword followed by a series of variable names and assigned values separated by commas
![Page 8: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/8.jpg)
JavaScript, Third Edition 8
Modifying Variables
• You can change the variable’s value at any point in a script:
– Use a statement that includes variable’s name, followed by an equal sign, followed by the value to assign to the variable
![Page 9: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/9.jpg)
JavaScript, Third Edition 9
Data Types
• Specific category of information that a variable contains
• Helps determine how much memory the computer allocates for data stored in the variable
• Data type also governs the kinds of operations that can be performed on a variable
![Page 10: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/10.jpg)
JavaScript, Third Edition 10
Data Types (Cont.)• Data types that can be assigned only a single value
are called primitive types • JavaScript supports six primitive data types
![Page 11: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/11.jpg)
JavaScript, Third Edition 11
Data Types (Cont.)
• The null value
– data type/value that can be assigned to a variable
– Indicates the variable does not contain a usable value
– A variable with a value of “null” has a value assigned to it
• Null is really “no value”
– You assign the “null” value to a variable when you want to ensure that the variable does not contain any data
![Page 12: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/12.jpg)
JavaScript, Third Edition 12
Data Types (Cont.)
• The JavaScript language also supports reference, or composite, data types
• Reference data types:
– Can contain multiple values or complex types of information
• As opposed to single values stored in primitive data types
![Page 13: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/13.jpg)
JavaScript, Third Edition 13
Data Types (Cont.)
– Three reference data types supported by the JavaScript language are
• Functions
• Objects
• Arrays
![Page 14: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/14.jpg)
JavaScript, Third Edition 14
Data Types (Cont.)
• Strongly typed programming languages– Require you to declare the data types of variables
– Also known as static typing• Data types do not change after they have been declared
• Loosely typed programming languages– Do not require you to declare the data types of
variables
– Also known as dynamic typing • Data types can change after they have been declared
![Page 15: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/15.jpg)
JavaScript, Third Edition 15
Data Types (Cont.)
• JavaScript is a loosely typed programming language
– You are not required, and not allowed, to declare the data type of variables
• JavaScript interpreter
– Automatically determines what type of data is stored in a variable
– Assigns the variable’s data type accordingly
![Page 16: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/16.jpg)
JavaScript, Third Edition 16
Numeric Data Types
• Are an important part of any programming language
• Are particularly useful for arithmetic calculations
• JavaScript supports two numeric data types
– Integers
– Floating-point numbers
![Page 17: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/17.jpg)
JavaScript, Third Edition 17
Numeric Data Types (Cont.)• Integer
– Positive or negative number with no decimal places
– Can range from -9007199254740990 (-253) to 9007199254740990 (253)
• Floating-point number– Contains decimal places
• OR
– Written in exponential notation
• Exponential notation, or scientific notation– Shortened format for writing very large numbers or
numbers with many decimal places
![Page 18: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/18.jpg)
JavaScript, Third Edition 18
Boolean Values
• Logical value of true or false
• Can also be thought of as being yes or no, or on or off
• Most often used for
– Deciding which parts of a program should execute
– Comparing data
• In JavaScript programming, you can only use the words true and false to indicate Boolean values
![Page 19: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/19.jpg)
JavaScript, Third Edition 19
Boolean Values (Cont.)
• In other programming languages, you can use the integer values of 1 and 0 to indicate Boolean values of true and false
– 1 indicates true and 0 indicates false
– JavaScript converts the values true and false to the integers 1 and 0 when necessary
![Page 20: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/20.jpg)
JavaScript, Third Edition 20
Arrays
• Contain set of data represented by a single variable name
• Collection of variables contained within a single variable
• Use arrays when you want to store groups or lists of related information in a single, easily managed location
![Page 21: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/21.jpg)
JavaScript, Third Edition 21
Arrays (Cont.)
• Represented in JavaScript by the Array object:
– Contains a special constructor named Array() which is used for creating an array
• Constructor:
– special type of function used as the basis for creating reference variables
![Page 22: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/22.jpg)
JavaScript, Third Edition 22
Arrays (Cont.)
• Create new arrays by using the keyword new and the Array() constructor with the following syntax:
– Var arrayName = new Array(numberƒofƒelements);
• Within the parentheses of the Array() construction
– Include an integer to represent the number of elements to be contained in the array
• Each piece of data contained in an array is called an element
![Page 23: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/23.jpg)
JavaScript, Third Edition 23
Arrays (Cont.)
• The numbering of elements within an array starts with an index number of zero (0)
• Index number:
– An element’s numeric position within the array
• Refer to a specific element by enclosing its index number in brackets at the end of the array name
![Page 24: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/24.jpg)
JavaScript, Third Edition 24
Arrays (Cont.)• Assign values to individual array elements in the
same fashion as you assign values to a standard variable:
– EXCEPT you include the index for an individual element of the array
• When you create a new array with the Array() constructor:
– Declaring the number of array elements is optional
![Page 25: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/25.jpg)
JavaScript, Third Edition 25
Expressions and Operators• Expression
– Literal value or variable or a combination of literal values, variables, operators, and other expressions that can be evaluated by the JavaScript interpreter to produce a result
– Use operands and operators to create expressions in JavaScript
• Operands – Variables and literals contained in an expression
• Literal – Value such as a literal string or a number
![Page 26: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/26.jpg)
JavaScript, Third Edition 26
Expressions and Operators (Cont.)
• Operators – Symbols used in expressions to manipulate operands,
such as the addition operator (+) and multiplication operator (*)
• Binary operator – Requires an operand before and after the operator
• Unary operator – Requires a single operand either before or after the
operator
![Page 27: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/27.jpg)
JavaScript, Third Edition 27
Expressions and Operators (Cont.)
![Page 28: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/28.jpg)
JavaScript, Third Edition 28
Arithmetic Operators
• Arithmetic operators
– Used in JavaScript to perform mathematical calculations, such as addition
– Also used to return the modulus of a calculation:
• Remainder left when you divide one number by another number
![Page 29: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/29.jpg)
JavaScript, Third Edition 29
Arithmetic Binary Operators
![Page 30: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/30.jpg)
JavaScript, Third Edition 30
Arithmetic Unary Operators
![Page 31: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/31.jpg)
JavaScript, Third Edition 31
Arithmetic Unary Operators (Cont.)
• The increment (++) and decrement (--) unary operators can be used as prefix or postfix operators
• A prefix operator is placed before a variable
• A postfix operator is placed after a variable
![Page 32: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/32.jpg)
JavaScript, Third Edition 32
Assignment Operators
• Assignment operators are used for assigning a value to a variable
• JavaScript includes other assignment operators called compound assignment operators:
– Perform mathematical calculations on variables and literal values in an expression, and then assign a new value to the left operand
![Page 33: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/33.jpg)
JavaScript, Third Edition 33
Assignment Operators (Cont.)
![Page 34: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/34.jpg)
JavaScript, Third Edition 34
Assignment Operators (Cont.)
![Page 35: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/35.jpg)
JavaScript, Third Edition 35
Comparison and Conditional Operators
• Comparison operators:
– Used to compare two operands and determine if one numeric value is greater than another
– A Boolean value of true or false is returned after two operands are compared
![Page 36: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/36.jpg)
JavaScript, Third Edition 36
Comparison and Conditional Operators (Cont.)
![Page 37: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/37.jpg)
JavaScript, Third Edition 37
Comparison and Conditional Operators (Cont.)
• Comparison operator is often used with the conditional operator:
– Executes one of two expressions, based on the results of a conditional expression
– The syntax for the conditional operator is conditional expression ? expression1: expression2;
![Page 38: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/38.jpg)
JavaScript, Third Edition 38
Logical Operators
• Logical operators are used for comparing two Boolean operands for equality
![Page 39: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/39.jpg)
JavaScript, Third Edition 39
Working With Strings
• A text string:
– Contains zero or more characters surrounded by double or single quotation marks
– Can be used as literal value or can be assigned to a variable
– Literal strings can be also be assigned a zero-length string value:
• Called an empty string
![Page 40: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/40.jpg)
JavaScript, Third Edition 40
Escape Characters and Sequences
• Escape character
– Tells compiler or interpreter that the character following it has a special purpose
– In JavaScript, the escape character is the backslash ( \ )
– Placing a backslash in front of an apostrophe tells the JavaScript interpreter:
» Apostrophe is to be treated as a regular keyboard character
![Page 41: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/41.jpg)
JavaScript, Third Edition 41
Escape Characters and Sequences (Cont.)
• the combination of the escape character with other characters is called an escape sequence
![Page 42: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/42.jpg)
JavaScript, Third Edition 42
String Operators
• Two operators used with strings:
– (+)
– (+=)
• When used with strings, the plus sign is known as the concatenation operator (+):
– Used to combine two strings
![Page 43: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/43.jpg)
JavaScript, Third Edition 43
Special Operators
![Page 44: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/44.jpg)
JavaScript, Third Edition 44
Special Operators (Cont.)
• One special operator is the typeof operator
– Useful because data type of variables can change during course of program execution
– Use to determine the data type of a variable
– Syntax is typeof(variablename);
![Page 45: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/45.jpg)
JavaScript, Third Edition 45
Special Operators (Cont.)
![Page 46: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/46.jpg)
JavaScript, Third Edition 46
Operator Precedence
• Operator precedence:
– Order in which operations in expression are evaluated
• When performing operations with operators in the same precedence group
– The order of precedence is determined by the operator’s associativity
– That is, the order in which operators of equal precedence execute
![Page 47: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/47.jpg)
JavaScript, Third Edition 47
Operator Precedence (Cont.)
![Page 48: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/48.jpg)
JavaScript, Third Edition 48
Chapter Summary
• Variables– Values a program stores in computer memory
• Data type– Specific category of information that a variable
contains • Array
– Contains a set of data represented by a single variable name
![Page 49: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/49.jpg)
JavaScript, Third Edition 49
Chapter Summary (cont.)
• Constructor
– Special type of function used as basis for creating variables of reference data types
• Operands
– Variables and literals contained in an expression
![Page 50: Chapter 2 Data Types and Operators JavaScript, Third Edition.](https://reader035.fdocuments.in/reader035/viewer/2022062320/56649d825503460f94a67a96/html5/thumbnails/50.jpg)
JavaScript, Third Edition 50
Chapter Summary (cont.)
• Operators
– Symbols used in expressions to manipulate operands, such as the addition operator (+) and multiplication operator (*)
• Operator precedence
– Order in which operations in an expression are evaluated