Chapter 3: Data Types and Operators JavaScript - Introductory.

Slides:



Advertisements
Similar presentations
Data Types and Operators Using Data Types
Advertisements

JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Using Data Types (No Audio Component) © Dr. David C. Gibbs
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
PHP Introduction.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
 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
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.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Lecture 2 Introduction to PHP MIS 3501, Spring 2014 Jeremy Shafer Department of MIS Fox School of Business Temple University January 30, 2014.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
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
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Data Types and Operators (NON Audio Version) © Dr. David C. Gibbs
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
2440: 211 Interactive Web Programming Expressions & Operators.
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?
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.
C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.
C++ Programming: Basic Elements of C++.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e 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.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Operators in JAVA. Operator An operator is a symbol that operates on one or more arguments to produce a result. Java provides a rich set of operators.
Creating PHP Pages Chapter 6 PHP Variables, Constants and Operators.
Module 5 JavaScript Operators. CS346 Javascript-52 Examples  JS-5 Examples.
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.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
Introduction to JavaScript 21 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 15.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
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.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
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.
Chapter 6 JavaScript: Introduction to Scripting
Lecture 3 Java Operators.
JavaScript, Sixth Edition
BASIC ELEMENTS OF A COMPUTER PROGRAM
2.5 Another Java Application: Adding Integers
Java Programming: From Problem Analysis to Program Design, 4e
JavaScript and Ajax (Expression and Operators)
WEB PROGRAMMING JavaScript.
Chapter 2: Basic Elements of Java
JavaScript Data Concepts
PHP.
Chapter 2: Introduction to C++.
JavaScript CS 4640 Programming Languages for Web Applications
Presentation transcript:

Chapter 3: Data Types and Operators JavaScript - Introductory

Tutorial3Calculator.html

Section A: Using Data Types and Arrays

Objectives In this section, students will learn: How to use data types About numeric data types About Boolean values How to use strings How to use arrays

Data Types Data type is the specific category of information that a variable contains Data types that can be assigned only a single value are called primitive types JavaScript supports five primitive data types: –Integer numbers –Floating-point numbers –Boolean values –Strings –Null value

Primitive Types

Data Types Programming languages that require you to declare the data types of variables are called strongly-typed programming languages Strong typing is also known as static typing Programming languages that do not require you to declare the data types of variables are called loosely-typed programming languages Loose typing is also known as dynamic typing since data types change after being declared

Values Returned by Typeof () Operator

PrintDataTypes.html

Numeric Data Types An integer is a positive or negative number with no decimal places Integer values in JavaScript can range from (-2 53 ) to (2 53 ) A floating-point number contains decimal places or is written using exponential notation

Numeric Data Types Exponential notation, or scientific notation, is a way of writing very large numbers or numbers with many decimal places using shortened format Floating-point values that exceed the largest positive value of x result in a special value of Infinity

Boolean Values A Boolean value is a logical value of true or false You can also think of a Boolean value as being yes or no, or on or off. Boolean values are most often used for decision making and comparing data Only the words true and false can be used to indicate Boolean values Boolean values get their name from the 19th century mathematician George Boole

Program That Returns a Boolean Value to an Event Handler

LiteralStrings Program

Strings A text string contains zero or more characters surrounded by double or single quotation marks There is no special data type in JavaScript for a single character, such as the char data type in the C, C++, and Java programming languages Take extra care when using single quotations with possessives and contractions in strings JavaScript interpreter always looks for the first closing single or double quotation mark to match an opening single or double quotation mark

Output of LiteralStrings Program in a Web Browser

Strings 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 In addition to including escape sequences in strings, you can include HTML tags If you include HTML tags within JavaScript strings, they must be located within a string’s opening and closing quotation marks

JavaScript Escape Sequences

Program Containing Strings with Escape Sequences

Output of Program Containing Strings with Escape Sequences

Program Containing Strings with HTML Tags

Output of Program Containing Strings with HTML Tags

Output of DailySpecials.html

Arrays An array contains a set of data represented by a single variable name Since arrays are objects, you create them using new keyword and JavaScript’s Array() constructor object The Array () object is comparable to a constructor function and is created using similar syntax, as follows: variable_name = new Array (number of elements);

Arrays Each piece of data contained in an array is called an element The numbering of elements within an array starts with an index number of zero (0). Array elements that are created but not assigned values have an initial value of null

Output of MonthsofYear.html

Section A: Chapter Summary A data type is the specific category of information that a variable contains Data types that can only be assigned a single value are called primitive types Assigning the value null to a variable indicates the variable does not contain a value An integer is a positive or negative number with no decimal point A floating-point number contains decimal places or is written using exponential notation

Section A: Chapter Summary A Boolean value is a logical value of true or false Literal strings and string variables contain zero or more characters An escape character is used to tell the compiler or interpreter that the character following it has a special purpose An array contains a set of data represented by a single variable name An array is created with the Array () constructor object

Section A: Chapter Summary The Array () constructor object receives a single argument representing the number of elements to be contained in the array The numbering of elements within an array starts with an index number of zero (0) You can create an array without any elements and then add new elements to the array as needed The size of an array can change dynamically Someone can assign values to an array’s elements when first created

Section B: Expressions and Operators

Objectives In this section, you will learn how to: Use expressions Use arithmetic, assignment, comparison, logical, and string operators Create the calculator program

Expressions Variables and data become most useful when you use them in an expression An expression is a combination of literal values, variables, operators, and other expressions that can be evaluated by the JavaScript interpreter to produce a result Operands are variables and literals contained in an expression Operators are symbols used in expressions to manipulate operands

Literal and Variable Expressions

JavaScript Operator Types

Expressions JavaScript operators are binary or unary A binary operator requires an operand before the operator and an operand after the operator –The equal sign in the statement myNumber = 100; is an example of a binary operator A unary operator requires a single operand either before or after the operator Operand to the left of an operator is known as the left operand and on the right is the right operand

Arithmetic Operators Arithmetic operators are used to perform mathematical calculations

Examples of Arithmetic Binary Operators

Arithmetic Operators When JavaScript performs an arithmetic calculation, it performs the right side of the assignment operator, then assigns the value to a variable on the left side The JavaScript interpreter will not convert strings to numbers when you use the addition operator Arithmetic operations can also be performed on a single variable using unary operators A prefix operator is placed before a variable A postfix operator is placed after a variable

Arithmetic Operators The statements ++myVariable; and myVariable++; both increase myVariable by one

Output of ArithmeticExamples.html

Assignment Operators Assignment operators are used for assigning a value to a variable var myCar = “Ford”; MyCar = “Corvette”; Can use the + = assignment operator to combine two strings as well as to add numbers

Assignment Operators

Examples of Assignment Operators

Output of AssignmentsExamples.html

Comparison Operators Comparison operators are used to compare two operands for equality and to determine if one numeric value is greater than another A Boolean value of true or false is returned after two operands are compared The comparison operator compares values, while the assignment operator assigns values

Comparison Operators

Examples of Comparison Operators

Output of ComparisonExamples.html

Logical Operators 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 Logical operators are often used within conditional and looping statements such as the if else, for, and while statements

Logical Operators

Output of LogicalExamples.html

String Operators The concatenation operator (+) is used to combine two strings The following code combines a string variable and a literal string, and assigns the new value to another variable: var firstString = “Ernest Hemingway wrote”; var newString; newString = firstString + “ For Whom the Bell Tolls ; You can also use the += assignment operator to combine two strings

Operator Precedence Operator precedence is the order of priority in which operations in an expression are evaluated Expressions are evaluated on a left-to-right basis

Order of precedence for JavaScript operators: –Parentheses/brackets/dot (() []. ) - highest precedence –Negation/ increment (! typeof void) –Multiplication/division/modulus (* / % ) –Addition/ subtraction (+ -) –Comparison ( >=) –Equality (==!=) –Logical and (&&)- Logical or (II) –Assignment operators (=+=-=*=/=%=) - lowest This list does not include ALL the operators that JavaScript evaluates in the order of precedence Operator Precedence

Creating the Calculator Program The eval() function evaluates expressions contained within strings Can include a string literal or string variable as the argument for the eval() function

Section B: Chapter Summary An expression is a single literal or variable, or a combination of literal values, variables, operators, and other expressions that can be evaluated by interpreter to produce result Operands are variables and literals contained in an expression Operators are symbols used in expressions to manipulate operands A binary operator requires an operand before operator and an operand after operator

Section B: Chapter Summary A unary operator requires a single operand either before or after operator Arithmetic operators are used for performing addition, subtraction, multiplication, and division in JavaScript The increment (++) and decrement (--) unary operators can be used as prefix or postfix operators Use assignment operators to assign a value to a variable

Section B: Chapter Summary Use comparison operators to compare two operands for equality and determine if one numeric value is greater than another Use logical operators to compare two Boolean operands for equality Logical operators are often used with comparison operators to evaluate expressions, allowing you to combine results of several expressions into a single statement

Section B: Chapter Summary When used with strings, the +sign, or addition operator, is known as the concatenation operator Operator precedence is the order of priority in which operations in an expression are evaluated Parentheses are used with expressions to change the order in which individual operations in an expression are evaluated Use the eval() function to evaluate expressions contained within strings