Chapter 2 Working with Data Types and Operators

Similar documents
Full file at

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Full file at

Visual C# Instructor s Manual Table of Contents

Chapter 2 Basic Elements of C++

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Chapter 2: Using Data

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Working with JavaScript

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

ARG! Language Reference Manual

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Chapter 17. Fundamental Concepts Expressed in JavaScript

Basics of Java Programming

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

ENGR 101 Engineering Design Workshop

VARIABLES & ASSIGNMENTS

3 The Building Blocks: Data Types, Literals, and Variables

Datatypes, Variables, and Operations

CMPT 125: Lecture 3 Data and Expressions

COMS 469: Interactive Media II

Variables, Constants, and Data Types

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

4. Inputting data or messages to a function is called passing data to the function.

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

Data Types, Literals, Operators

DaMPL. Language Reference Manual. Henrique Grando

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Unit 3. Operators. School of Science and Technology INTRODUCTION

Expressions and Casting

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

CSC Web Programming. Introduction to JavaScript

Computational Expression

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Prefix/Infix/Postfix Notation

SECTION II: LANGUAGE BASICS

Outline. Data and Operations. Data Types. Integral Types

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Pseudocode is an abbreviated version of the actual statement t t (or code ) in the program.

Chapter 2: Using Data

Java Notes. 10th ICSE. Saravanan Ganesh

easel LANGUAGE REFERENCE MANUAL

Programming with Java

Programming Languages & Translators. XML Document Manipulation Language (XDML) Language Reference Manual

Language Reference Manual simplicity

Basic Operations jgrasp debugger Writing Programs & Checkstyle

CSc Introduction to Computing

LECTURE 02 INTRODUCTION TO C++

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Learning Language. Reference Manual. George Liao (gkl2104) Joseanibal Colon Ramos (jc2373) Stephen Robinson (sar2120) Huabiao Xu(hx2104)

JAVASCRIPT. Computer Science & Engineering LOGO

CGS 3066: Spring 2015 JavaScript Reference

Chapter 2 Elementary Programming

Client-Side Web Technologies. JavaScript Part I

CSCE 120: Learning To Code

CS112 Lecture: Primitive Types, Operators, Strings

Fundamental of Programming (C)

Lecture 2 Tao Wang 1

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Zheng-Liang Lu Java Programming 45 / 79

CS112 Lecture: Working with Numbers

Operators & Expressions

Chapter 2: Introduction to C++

Operators in C. Staff Incharge: S.Sasirekha

IT 374 C# and Applications/ IT695 C# Data Structures

The Warhol Language Reference Manual

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

COMP Primitive and Class Types. Yi Hong May 14, 2015

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

Petros: A Multi-purpose Text File Manipulation Language

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

A variable is a name for a location in memory A variable must be declared

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

Java Programming Unit 3: Variables and Arithmetic Operations

Differentiate Between Keywords and Identifiers

Data Types and Expressions. C# Programming: From Problem Analysis to Program Design 2nd Edition

FRAC: Language Reference Manual

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

EXPRESSIONS AND ASSIGNMENT CITS1001

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Fundamentals of Programming. Lecture 3: Introduction to C Programming

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

3. Java - Language Constructs I

Unit 3. Constants and Expressions

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

4 Programming Fundamentals. Introduction to Programming 1 1

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Variables and Values

Cellular Automata Language (CAL) Language Reference Manual

Programming for Engineers Introduction to C

CS313D: ADVANCED PROGRAMMING LANGUAGE

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Transcription:

JavaScript, Fourth Edition 2-1 Chapter 2 Working with Data Types and Operators At a Glance Instructor s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics Additional Projects Additional Resources Key Terms

JavaScript, Fourth Edition 2-2 Lecture Notes Overview In this chapter, students will learn about JavaScript variables and data types, and the operations that can be performed on them. Students will start by learning how to declare, initialize, and use variables. Then, they will study the different data types of JavaScript. After that, students will learn about expressions, operands, and the different types of operators available in JavaScript. Then, students will learn more about working with strings. Finally, students will study operator precedence. Chapter Objectives Work with variables Study data types Use expressions and operators Work with strings Study operator precedence Teaching Tips Using Variables 1. Explain that a variable is a specific location in the computer s memory. However, the term is commonly used to refer to the values a program stores in computer memory. 2. Mention that to use a variable in a program, you first have to write a statement that creates the variable and assigns it a name. Naming Variable Names 1. Point out that the name you assign to a variable is called an identifier. 2. Mention the following rules and conventions that must be observed 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. 3. Define reserved words (also called keywords) as special words that are part of the JavaScript language syntax. Use Table 2-1 to identify the JavaScript reserved words.

JavaScript, Fourth Edition 2-3 Declaring and Initializing Variables 1. Mention that creating a variable is known as declaring the variable. In JavaScript, you usually use the reserved keyword var to declare variables. To declare a variable named myvariable, the following statement is used: var myvariable; 2. Point out that when you declare a variable, you can also assign a specific value to, or initialize, the variable using the following syntax: var variable_name = value; 3. Mention that the equal sign (=) in the preceding statement is called an assignment operator. 4. Describe how to declare multiple variables in a single statement using a single var keyword. Teaching Tip Mention that even though JavaScript lets you declare a variable without using the var keyword, it is good programming practice to always use the var keyword when declaring variables. Not doing so can lead to problems in some cases. Displaying Variables 1. Describe how to print a variable (that is, display its value on the screen) using the document.write() or document.writeln() methods. 2. Note that when printing a variable, the plus sign (+) is used to combine a literal string with a variable containing a numeric value. The plus sign can also be used to perform arithmetic operations involving variables that contain numeric values. Modifying Variables 1. Describe how to change a variable s value. 2. Point out that you can change a variable s value at any point in a script. Quick Quiz 1 1. The values a program stores in computer memory are commonly called. Answer: variables 2. The name you assign to a variable is called a(n). Answer: identifier

JavaScript, Fourth Edition 2-4 3. Reserved words are also called. Answer: keywords 4. True or False: Identifiers must begin with an ASCII letter. Answer: False 5. True or False: The plus sign (+) is called the assignment operator. Answer: False Working with Data Types 1. Define a data type as the specific category of information that a variable contains. 2. Define primitive types as data types that can be assigned only a single value. Use Table 2-2 to identify the JavaScript primitive types. 3. Define reference, or composite, data types as those that can contain multiple values or complex types of information. 4. Explain the difference between a strongly typed programming language and a loosely typed programming language. Mention that JavaScript is a loosely typed programming language. Numeric Data Types 1. Mention that JavaScript supports two numeric data types: integers and floating-point numbers. 2. Define an integer as a positive or negative number with no decimal places. 3. Define a floating-point number as a number that contains decimal places or that is written in exponential notation. 4. Explain that exponential notation, or scientific notation, is a shortened format for writing very large numbers or numbers with many decimal places. Boolean Values Arrays 1. Define a Boolean value as a logical value of true or false. 2. Mention that in JavaScript programming, you can only use the words true and false to indicate Boolean values. 1. Mention that an array contains a set of data represented by a single variable name. Use Figure 2-8 to show a conceptual example of an array.

JavaScript, Fourth Edition 2-5 2. Explain how to create new arrays using the keyword new and the Array() constructor. 3. Mention that each piece of data contained in an array is called an element. 4. Define an index as an element s numeric position within the array. Point out that the numbering of elements within an array starts with an index number of zero (0). Teaching Tip The concept of an array will be confusing to students who have not dealt with arrays before. Be prepared to spend some time in introducing this subject to students. You may also want to draw a model of an array and its elements, along with the index of each element. The fact that the first element has an index number of 0 might cause a lot of confusion and even some errors in the work of students in the beginning. 5. Explain how to access the elements of an array. 6. Demonstrate how to modify the elements of an array. 7. Discuss how to use the length property to determine the number of elements in an array. Quick Quiz 2 1. A(n) is the specific category of information that a variable contains. Answer: data type 2. Data types that can be assigned only a single value are called. Answer: primitive types 3. True or False: The String data type is a reference data type in JavaScript. Answer: False 4. A(n) is a positive or negative number with no decimal places. Answer: integer 5. True or False: The numbering of elements within an array starts with an index number of zero (0). Answer: True

JavaScript, Fourth Edition 2-6 Building Expressions 1. Define an expression as a 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. 2. Define operands as variables and literals contained in an expression. Mention that a literal is a value such as a literal string or a number. 3. Define operators as symbols used in expressions to manipulate operands. Use Table 2-3 to discuss the main types of JavaScript operators. 4. Explain the difference between a binary operator and a unary operator. Arithmetic Operators 1. Mention that arithmetic operators are used in JavaScript to perform mathematical calculations, such as addition, subtraction, multiplication, and division. 2. Use Table 2-4 to show the binary arithmetic operators available in JavaScript. 3. Use Table 2-5 to show the unary arithmetic operators available in JavaScript. 4. Explain the difference between a prefix operator and a postfix operator. Assignment Operators 1. Mention that assignment operators are used for assigning a value to a variable. 2. Use Table 2-6 to discuss the assignment operators available in JavaScript. Comparison and Conditional Operators 1. Define comparison operators as those that are 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. 2. Use Table 2-7 to point out the JavaScript comparison operators. 3. Point out that the conditional operator executes one of two expressions, based on the results of a conditional expression. 4. Describe the syntax for the conditional operator. Logical Operators 1. Explain that logical operators are used for comparing two Boolean operands for equality. A Boolean value of true or false is returned after two operands are compared.

JavaScript, Fourth Edition 2-7 2. Use Table 2-8 to describe the JavaScript logical operators. Quick Quiz 3 1. are variables and literals contained in an expression. Answer: Operands 2. True or False: A unary operator requires an operand before and after the operator. Answer: False 3. True or False: A prefix operator is placed before a variable. Answer: True 4. operators are used for assigning a value to a variable. Answer: Assignment 5. operators are used for comparing two Boolean operands for equality. Answer: Logical Working with Strings 1. Remind students that a text string is text that is contained within double or single quotation marks. 2. Mention that you can use text strings as literal values or assign them to a variable. 3. Point out that literal strings can be assigned a zero-length string value called an empty string. String Operators 1. Point out that when used with strings, the plus sign is known as the concatenation operator. It is used to combine two strings. Show how the operator works with strings. 2. Mention that the compound assignment operator (+=) can also be used to combine two strings. Show how this operator works with strings. Escape Characters and Sequences 1. Define an escape character as a character that tells the compiler or interpreter that the character that follows it has a special purpose. In JavaScript, the escape character is the backslash (\). 2. Mention that when you combine the escape character with other characters, the combination is called an escape sequence. Most escape sequences carry out special functions.

JavaScript, Fourth Edition 2-8 3. Use Table 2-9 to show the JavaScript escape sequences. Special Operators 1. Use Table 2-10 to discuss the special operators in JavaScript. These operators are used for various purposes and do not fit within any other category. 2. Discuss why the typeof operator is useful. 3. Describe the syntax of the typeof operator. Use Table 2-11 to list the values that can be returned by the typeof operator. Understanding Operator Precedence 1. Explain what is meant by the term operator precedence. 2. Use Table 2-12 to show the order of precedence for JavaScript operators. 3. Define an operator s associativity as the order in which operators of equal precedence execute. 4. Use Figure 2-20 to explain left to right associativity, and use Figure 2-21 to explain right to left associativity. Quick Quiz 4 1. True or False: Empty strings are valid values for literal strings. Answer: True 2. True or False: You can use the compound assignment operator (+=) to combine two strings. Answer: True 3. In JavaScript, the escape character is the. Answer: backslash (\) 4. When you combine the escape character with other characters, the combination is called a(n). Answer: escape sequence 5. Operator is the order in which operators of equal precedence execute. Answer: associativity

JavaScript, Fourth Edition 2-9 Class Discussion Topics 1. Why does there need to be a set order of precedence for the JavaScript operators? 2. Is it necessary to use arrays in programming? Explain your reasoning. Is it very useful to use arrays in programming? Explain your reasoning. Additional Projects 1. Write a script in which you: a. Declare a variable called employee1 and assign it the value: Susan Harper b. Declare a variable called hoursworked1 and assign it the value: 45 c. Print the variables onto the screen in such a way that the following is displayed on the screen: Susan Harper has worked 45 hours. 2. Declare an array called cities that has six elements. Assign the following values to the elements: a. First element New York b. Second element Chicago c. Third element Los Angeles d. Fourth element Austin e. Fifth element Miami f. Sixth element - Philadelphia Additional Resources 1. JavaScript Operations on Variables: www.functionx.com/javascript/lesson04.htm 2. JavaScript Tutorial Variables: www.howtocreate.co.uk/tutorials/javascript/variables 3. JavaScript Arrays - Creating and Storing Values: www.webdevelopersnotes.com/tutorials/javascript/javascript_arrays.php3 Key Terms Arithmetic operators are used in JavaScript to perform mathematical calculations, such as addition, subtraction, multiplication, and division. An array contains a set of data represented by a single variable name. Assignment operators are used for assigning a value to a variable. Associativity is the order in which operators of equal precedence execute. A binary operator requires an operand before and after the operator. A Boolean value is a logical value of true or false.

JavaScript, Fourth Edition 2-10 Comparison operators are used to compare two operands and determine if one numeric value is greater than another. Reference, or composite, data types can contain multiple values or complex types of information, as opposed to the single values stored in primitive data types. Compound assignment operators perform mathematical calculations on variables and literal values in an expression, and then assign a new value to the left operand. The concatenation operator (+) is used to combine two strings. The conditional operator executes one of two expressions, based on the results of a conditional expression. A constructor is a special type of function that is used as the basis for creating reference variables (that is, variables whose data type is the reference data type). A data type is the specific category of information that a variable contains. Using a statement to create a variable is called declaring the variable. Loose typing is known as dynamic typing because data types can change after they have been declared. Each piece of data contained in an array is called an element. A zero-length string is called an empty string. An escape character tells the compiler or interpreter that the character that follows it has a special purpose. When you combine the escape character with other characters, the combination is called an escape sequence. Exponential notation, or scientific notation, is a shortened format for writing very large numbers or numbers with many decimal places. An expression is a 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. A floating-point number is a number that contains decimal places or that is written in exponential notation. The name you assign to a variable is called an identifier. An index is an element s numeric position within the array. When you assign a specific value to a variable, you are said to initialize the variable. An integer is a positive or negative number with no decimal places. Reserved words (also called keywords) are special words that are part of the JavaScript language syntax. The length property returns the number of elements in an array. A literal is a value such as a literal string or a number. Logical operators are used for comparing two Boolean operands for equality. Programming languages that do not require you to declare the data types of variables are called loosely typed programming languages. Operands are variables and literals contained in an expression. The term operator precedence refers to the order in which operations in an expression are evaluated. Operators, such as the addition operator (+) and multiplication operator (*), are symbols used in expressions to manipulate operands. A postfix operator is placed after a variable. A prefix operator is placed before a variable. Data types that can be assigned only a single value are called primitive types. Reference, or composite, data types can contain multiple values or complex types of information, as opposed to the single values stored in primitive data types.

JavaScript, Fourth Edition 2-11 Reserved words (also called keywords) are special words that are part of the JavaScript language syntax. Exponential notation, or scientific notation, is a shortened format for writing very large numbers or numbers with many decimal places. Strong typing is known as static typing, because data types do not change after they have been declared. Programming languages that require you to declare the data types of variables are called strongly typed programming languages. A unary operator requires a single operand either before or after the operator. The values a program stores in computer memory are commonly called variables.