JavaScript, Fourth Edition

Slides:



Advertisements
Similar presentations
PHP Introduction.
Advertisements

©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
ASP.NET Programming with C# and SQL Server First Edition
JavaScript, Third Edition
1 MATERI PENDUKUNG OPERATOR Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
Computer Science & Engineering
String Escape Sequences
Lecture 2 Introduction to PHP MIS 3501, Spring 2014 Jeremy Shafer Department of MIS Fox School of Business Temple University January 30, 2014.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
A First Book of ANSI C Fourth Edition
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
1 Working with Data Types and Operators. 2 Using Variables and Constants The values stored in computer memory are called variables The values, or data,
Chapter 2: Using Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
C++ Programming: Basic Elements of C++.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
CS346 Javascript -3 Module 3 JavaScript Variables.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Creating PHP Pages Chapter 6 PHP Variables, Constants and Operators.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
PHP Programming with MySQL Slide 3-1 CHAPTER 3 Working with Data Types and Operators.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Chapter 6 JavaScript: Introduction to Scripting
JavaScript, Sixth Edition
Objectives In this chapter you will: Create PHP scripts
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 2: Problem Solving Using C++
Overview: Programming Concepts
Web Programming– UFCFB Lecture 19-20
Java Programming: From Problem Analysis to Program Design, 4e
2.1 Parts of a C++ Program.
Chapter 2: Basic Elements of Java
JavaScript Data Concepts
Chapter 2: Introduction to C++.
The Data Element.
The Data Element.
Review of Java Fundamentals
Presentation transcript:

JavaScript, Fourth Edition Chapter 2 Working with Data Types and Operators

Objectives Work with variables Study data types Use expressions and operators Work with strings Study operator precedence JavaScript, Fourth Edition JavaScript, Fourth Edition 2 2

Using Variables Variable Before using a variable: Specific location in computer’s memory Before using a variable: Write a statement that creates the variable and assigns it a name JavaScript, Fourth Edition

Naming Variable Names Identifier Name you assign to a variable Rules and conventions when naming a variable Identifiers must begin with an uppercase or lowercase ASCII letter, dollar sign ($), or underscore ( _ ) Can use numbers in an identifier, but not as first character Cannot include spaces in an identifier Cannot use reserved words for identifiers JavaScript, Fourth Edition

Naming Variable Names (continued) Reserved words (or keywords) Special words that are part of JavaScript syntax Variable names are case sensitive myVariable, myvariable, MyVariable, and MYVARIABLE are all different variables JavaScript, Fourth Edition

Declaring and Initializing Variables Use the reserved keyword var to create variables To create a variable named myVariable: var myVariable; Declaring a variable Using a statement to create a variable Initializing a variable Assigning a specific value to it Can be done when you declare the variable var variable_name = value; JavaScript, Fourth Edition

Declaring and Initializing Variables (continued) Assignment operator Equal sign (=) Assigns the value on the right side of expression to the variable on the left side of expression Value assigned to a variable can be a literal string or a numeric value Literal string must be enclosed in quotation marks var myName = "Don“; Numeric value is not enclosed in quotation marks var retirementAge = 59; JavaScript, Fourth Edition

Declaring and Initializing Variables (continued) Can declare multiple variables using a single var keyword var customerName = "Don Gosselin", orderQuantity = 100, salesTax = .05; Can assign value of one variable to another var salesTotal; var curOrder = 40; salesTotal = curOrder; JavaScript, Fourth Edition

Displaying Variables To print a variable, pass variable name to document.write() or document.writeln() method Example document.write("<p>Your sales total is $“ + salesTotal + ".</p>"); JavaScript, Fourth Edition

Displaying Variables (continued) Figure 2-1: Results of script that assigns the value of one variable to another JavaScript, Fourth Edition

Modifying Variables To change a variable’s value, use a statement with variable’s name, equal sign, and new value var salesTotal = 40; document.write("<p>Your sales total is $" + salesTotal + ".</p>"); var shipping = 10; salesTotal = salesTotal + shipping; document.write("<p>Your sales total plus shipping is $" + salesTotal + ".</p>"); JavaScript, Fourth Edition

Modifying Variables (continued) Figure 2-3: Results of script that includes a changing variable JavaScript, Fourth Edition

Working with Data Types Category of information that a variable contains Primitive types Data types that can be assigned only a single value There are 5 primitive data types in JavaScript the number type in JavaScript includes both integer and floating-point values. IEEE 754-1985 double-precision floating-point format representation permits exact representation of integers in the range –253 to 253 and floating-point magnitudes as large as ±1.7976 x 10308 and as small as ±2.2250 x 10-308. Table 2-2: Primitive JavaScript data types JavaScript, Fourth Edition

Working with Data Types (continued) Reference, or composite, data types Can contain multiple values or complex types of information Functions, objects, arrays Strongly typed programming languages Must declare data types of variables Loosely typed programming languages Not required to declare data types of variables JavaScript is a loosely typed language JavaScript, Fourth Edition

Numeric Data Types JavaScript supports two numeric data types Integer Integers and floating-point numbers Integer Positive or negative number with no decimal places Floating-point number 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, Fourth Edition

Boolean Values Boolean value Example Logical value of true or false In JavaScript, words true and false indicate Boolean values Example var repeatCustomer = true; var corporateDiscount = false; document.write("<p>Repeat customer: " + repeatCustomer + "</p>"); document.write("<p>Corporate discount: " + corporateDiscount + "</p>"); JavaScript, Fourth Edition

Boolean Values (continued) Figure 2-7: Boolean values JavaScript, Fourth Edition

Figure 2-8: Conceptual example of an array Arrays Array Set of data represented by a single variable name Figure 2-8: Conceptual example of an array JavaScript, Fourth Edition

Declaring and Initializing Arrays Element: each piece of data in an array Create an array named hospitalDepts[] that has 10 elements var hospitalDepts = new Array(10); Assign value to first element in: hospitalDepts[] hospitalDepts[0] = "Anesthesia"; Can assign value to elements when array is created hospitalDepts = new Array("Anesthesia", "Molecular Biology", "Neurology"); JavaScript, Fourth Edition

Accessing Element Information To access an element’s value, include brackets and element index Examples document.writeln(hospitalDepts[0]); // prints "Anesthesia" document.writeln(hospitalDepts[1]); // prints "Molecular Biology" document.writeln(hospitalDepts[2]); // prints "Neurology" JavaScript, Fourth Edition

Modifying Elements To modify values in existing array elements, include brackets and element index Examples hospitalDepts[0] = "Anesthesia"; // first element hospitalDepts[1] = "Molecular Biology"; // second element hospitalDepts[2] = "Neurology"; // third element JavaScript, Fourth Edition

Determining the Number of Elements in an Array length property of Array class returns the number of elements in an array Syntax array_name.length; JavaScript, Fourth Edition

Building Expressions Expression Operands Operators Literal value or variable or a combination of literal values, variables, operators, and other expressions Can be evaluated by the JavaScript interpreter to produce a result Operands Variables and literals contained in an expression Literal: value such as a literal string or a number Operators Symbols used in expressions to manipulate operands JavaScript, Fourth Edition

Building Expressions (continued) Table 2-3: JavaScript operator types JavaScript, Fourth Edition

Building Expressions (continued) Binary operator Requires an operand before and after operator A binary operator requires two operands, one before the operator and one after the operator: operand1 operator operand2 For example, 3+4 or x*y. Unary operator Requires a single operand before or after operator A unary operator requires a single operand, either before or after the operator: operator operand or operand operator For example, x++ or ++x. JavaScript, Fourth Edition

Table 2-4: Arithmetic binary operators Arithmetic Operators Used to perform mathematical calculations Addition, subtraction, multiplication, division, etc. Table 2-4: Arithmetic binary operators JavaScript, Fourth Edition

Arithmetic Unary Operators Prefix operator Placed before a variable Postfix operator Placed after a variable Table 2-5: Arithmetic unary operators JavaScript, Fourth Edition

Assignment Operators Used for assigning a value to a variable Equal sign (=) Compound assignment operators Perform mathematical calculations on variables and literal values, and then assign a new value to the left operand JavaScript, Fourth Edition

Assignment Operators (continued) Table 2-6: Assignment operators JavaScript, Fourth Edition

Comparison and Conditional Operators Comparison operators Compare two operands and determine if one numeric value is greater than another Boolean value of true or false is returned Conditional operator Executes one of two expressions, based on the results of a conditional expression Syntax conditional expression ? expression1: expression2; JavaScript, Fourth Edition

Comparison and Conditional Operators (continued) Table 2-7: Comparison operators JavaScript, Fourth Edition

Table 2-8: Logical operators Compare two Boolean operands for equality Table 2-8: Logical operators JavaScript, Fourth Edition

Working with Strings Text string is text contained within double or single quotation marks Can use text strings as literal values or assign them to a variable Empty string Zero-length string value Valid value for literal strings JavaScript, Fourth Edition

String Operators Operators used to combine two strings Concatenation operator (+) var destination = "Jakarta"; var location = "Indonesia"; destination = destination + " is in " + location; Compound assignment operator (+=) destination += " is in Indonesia"; JavaScript, Fourth Edition

Escape Characters and Sequences Tells the compiler or interpreter that the character that follows has a special purpose In JavaScript, escape character is backslash (\) Escape sequence Escape character combined with other characters Most escape sequences carry out special functions JavaScript, Fourth Edition

Escape Characters and Sequences (continued) Table 2-9: JavaScript escape sequences JavaScript, Fourth Edition

Table 2-10: Special operators JavaScript, Fourth Edition

Special Operators (continued) Table 2-11: Values returned by typeof operator JavaScript, Fourth Edition

Understanding Operator Precedence Order in which operations in an expression are evaluated Associativity Order in which operators of equal precedence execute Left to right associativity Right to left associativity JavaScript, Fourth Edition

Understanding Operator Precedence (continued) Table 2-12: Operator precedence JavaScript, Fourth Edition

Understanding Operator Precedence (continued) Figure 2-20: Conceptual illustration of left to right associativity JavaScript, Fourth Edition

Understanding Operator Precedence (continued) Figure 2-21: Conceptual illustration of right to left associativity JavaScript, Fourth Edition

Summary Values a program stores in computer memory are called variables Name assigned to a variable is called an identifier Reserved words (or keywords) are special words that are part of the JavaScript language syntax Data type is the specific category of information that a variable contains Array contains a set of data represented by a single variable name JavaScript, Fourth Edition

Summary (continued) Expression is a single literal value or variable or a combination of literal values, variables, operators, and other expressions that can be evaluated by JavaScript interpreter to produce a result Operands are variables and literals contained in an expression Operators are symbols used in expressions to manipulate operands Operator precedence is order in which operations in an expression are evaluated JavaScript, Fourth Edition