Download presentation
1
JavaScript, Third Edition
Chapter 2 Data Types and Operators
2
JavaScript, Third Edition
Objectives Work with variables Study data types Use expressions and operators Work with strings Study operator precedence JavaScript, Third Edition
3
JavaScript, Third Edition
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 JavaScript, Third Edition
4
JavaScript, Third Edition
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 JavaScript, Third Edition
5
JavaScript, Third Edition
Variable Names (Cont.) Reserved words (keywords): Special words part of the JavaScript language JavaScript, Third Edition
6
JavaScript, Third Edition
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 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 JavaScript, Third Edition
8
JavaScript, Third Edition
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 JavaScript, Third Edition
9
JavaScript, Third Edition
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 JavaScript, Third Edition
10
JavaScript, Third Edition
Data Types (Cont.) Data types that can be assigned only a single value are called primitive types JavaScript supports six primitive data types JavaScript, Third Edition
11
JavaScript, Third Edition
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 JavaScript, Third Edition
12
JavaScript, Third Edition
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 JavaScript, Third Edition
13
JavaScript, Third Edition
Data Types (Cont.) Three reference data types supported by the JavaScript language are Functions Objects Arrays JavaScript, Third Edition
14
JavaScript, Third Edition
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 JavaScript, Third Edition
15
JavaScript, Third Edition
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 JavaScript, Third Edition
16
JavaScript, Third Edition
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 JavaScript, Third Edition
17
Numeric Data Types (Cont.)
Integer Positive or negative number with no decimal places Can range from (-253) to (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 JavaScript, Third Edition
18
JavaScript, Third Edition
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 JavaScript, Third Edition
19
JavaScript, Third Edition
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 JavaScript, Third Edition
20
JavaScript, Third Edition
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 JavaScript, Third Edition
21
JavaScript, Third Edition
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 JavaScript, Third Edition
22
JavaScript, Third Edition
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 JavaScript, Third Edition
23
JavaScript, Third Edition
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 JavaScript, Third Edition
24
JavaScript, Third Edition
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 JavaScript, Third Edition
25
Expressions and Operators
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 JavaScript, Third Edition
26
Expressions and Operators (Cont.)
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 JavaScript, Third Edition
27
Expressions and Operators (Cont.)
JavaScript, Third Edition
28
JavaScript, Third Edition
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 JavaScript, Third Edition
29
Arithmetic Binary Operators
JavaScript, Third Edition
30
Arithmetic Unary Operators
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 JavaScript, Third Edition
32
JavaScript, Third Edition
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 JavaScript, Third Edition
33
Assignment Operators (Cont.)
JavaScript, Third Edition
34
Assignment Operators (Cont.)
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 JavaScript, Third Edition
36
Comparison and Conditional Operators (Cont.)
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; JavaScript, Third Edition
38
JavaScript, Third Edition
Logical Operators Logical operators are used for comparing two Boolean operands for equality JavaScript, Third Edition
39
JavaScript, Third Edition
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 JavaScript, Third Edition
40
Escape Characters and Sequences
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 JavaScript, Third Edition
41
Escape Characters and Sequences (Cont.)
the combination of the escape character with other characters is called an escape sequence JavaScript, Third Edition
42
JavaScript, Third Edition
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 JavaScript, Third Edition
43
JavaScript, Third Edition
Special Operators 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); JavaScript, Third Edition
45
Special Operators (Cont.)
JavaScript, Third Edition
46
JavaScript, Third Edition
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 JavaScript, Third Edition
47
Operator Precedence (Cont.)
JavaScript, Third Edition
48
JavaScript, Third Edition
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 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 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 JavaScript, Third Edition
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.