JavaScript, Third Edition

Slides:



Advertisements
Similar presentations
Chapter 17 Fundamental Concepts Expressed in JavaScript.
Advertisements

IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
PHP Introduction.
 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.
JavaScript, Fourth Edition
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
ASP.NET Programming with C# and SQL Server First Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
1 MATERI PENDUKUNG OPERATOR Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
Basic Elements of C++ Chapter 2.
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
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,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
Lesson 3 – Primitive Data Types Objective: Students will investigate the definition and usage of objects and primitive data types in Java in order to practice.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
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.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 17 JavaScript.
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.
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.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Chapter 6 JavaScript: Introduction to Scripting
JavaScript, Sixth Edition
BASIC ELEMENTS OF A COMPUTER PROGRAM
Basic Elements of C++.
Multiple variables can be created in one declaration
Web Programming– UFCFB Lecture 19-20
Java Programming: From Problem Analysis to Program Design, 4e
Basic Elements of C++ Chapter 2.
Chapter 17 JavaScript.
2.1 Parts of a C++ Program.
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 2: Basic Elements of Java
JavaScript Data Concepts
Chapter 2: Introduction to C++.
Primitive Types and Expressions
Presentation transcript:

JavaScript, Third Edition Chapter 2 Data Types and Operators

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

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

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

JavaScript, Third Edition Variable Names (Cont.) Reserved words (keywords): Special words part of the JavaScript language JavaScript, Third Edition

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

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

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

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

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

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

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

JavaScript, Third Edition Data Types (Cont.) Three reference data types supported by the JavaScript language are Functions Objects Arrays JavaScript, Third Edition

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

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

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

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 JavaScript, Third Edition

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

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

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

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

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

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

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

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

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

Expressions and Operators (Cont.) JavaScript, Third Edition

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

Arithmetic Binary Operators JavaScript, Third Edition

Arithmetic Unary Operators JavaScript, Third Edition

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

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

Assignment Operators (Cont.) JavaScript, Third Edition

Assignment Operators (Cont.) JavaScript, Third Edition

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

Comparison and Conditional Operators (Cont.) JavaScript, Third Edition

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

JavaScript, Third Edition Logical Operators Logical operators are used for comparing two Boolean operands for equality JavaScript, Third Edition

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

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

Escape Characters and Sequences (Cont.) the combination of the escape character with other characters is called an escape sequence JavaScript, Third Edition

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

JavaScript, Third Edition Special Operators JavaScript, Third Edition

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

Special Operators (Cont.) JavaScript, Third Edition

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

Operator Precedence (Cont.) JavaScript, Third Edition

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

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

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