Chapter 4: Fundamentals of JavaScript 4.1 Capabilities 4.2 Essential Terminology 4.3 Structure of JavaScript Code 4.4 Data and Objects 4.5 Tokens, Operators,

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Advertisements

 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Data types, declarations, and expressions in Java.
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.
25 October Conditionals and Loops. Presentations Brendan: Cyberwarfare Casey: Online shopping (No current event today)
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
JavaScript, Fourth Edition
JavaScript, Third Edition
Introduction to C Programming
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
CSS, Programming Intro Week 4 INFM 603. Agenda JavaScript Intro.
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.
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
JavaScript Part 2 – Page 1 of 35CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: JavaScript Part 2.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
JavaScript – Part II Data Types and Operations George Mason University June 3, 2010.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
IMS 3253: Math 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Five Fundamental Math Operations Precedence of Math.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
Input, Output, and Processing
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 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.
JavaScript, Fourth Edition
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
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.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
1 2. Program Construction in Java. 01 Java basics.
JavaScript Tutorial. What is JavaScript JavaScript is the programming language of HTML and the Web Programming makes computers do what you want them to.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
Java Programming Fifth Edition
Topics Designing a Program Input, Processing, and Output
BIL 104E Introduction to Scientific and Engineering Computing
Chapter 19 JavaScript.
WEB PROGRAMMING JavaScript.
T. Jumana Abu Shmais – AOU - Riyadh
Java Basics.
Topics Designing a Program Input, Processing, and Output
Radoslaw Jedynak, PhD Poland, Technical University of Radom
Topics Designing a Program Input, Processing, and Output
Javascript Chapter 19 and 20 5/3/2019.
Using C++ Arithmetic Operators and Control Structures
Presentation transcript:

Chapter 4: Fundamentals of JavaScript 4.1 Capabilities 4.2 Essential Terminology 4.3 Structure of JavaScript Code 4.4 Data and Objects 4.5 Tokens, Operators, Expressions, and Statements 4.6 The JavaScript math Object 4.7 Comparison Operators and Decision-Making Structures 4.8 Loop Structures 4.9 Using JavaScript to Change Values in Form Fields

4.1 Capabilities of JavaScript Manage input and output. Manage input and output. Permit information to be manipulated in a symbolic way, independent of how a particular computer stores that information internally. Permit information to be manipulated in a symbolic way, independent of how a particular computer stores that information internally. Perform arithmetic operations on numbers. Perform arithmetic operations on numbers. Perform operations on and with strings of characters. Perform operations on and with strings of characters. Make decisions based on comparing values. Make decisions based on comparing values. Perform repetitive calculations. Perform repetitive calculations.

4.2 Some Essential Terminology expression a group of tokens and operators identifier a name associated with a variable, object or function keyword a word with a specific meaning in a programming language literal an actual value embedded in a script operator a token that symbolizes a mathematical or other operation program a series of interpreted or compiled statements reserved word a word that might become part of a language script a series of JavaScript statements statement a command that changes outcomes in a program token an indivisible lexical unit in a programming language variable a place in memory that holds data, represented by a unique identifier

4.3 JavaScript Statements Usually embedded inside script tags. Built from tokens. Each statement should terminate with a semicolon. Statements are "free format" and can appear anywhere on a line. Multiple statements on a line are allowed if each is terminated with a semicolon. Statement blocks begin and end with curly brackets: { statements go here… } Comments: // single line comments /* Multiple line comments cannot be nested one inside another. */

Data and Objects All information in JavaScript is associated with a data type, either explicitly or implicitly. Each data type is stored differently and each is associated with a specific set of allowed operations. Variables serve as data "containers." Each "container" is given a symbolic name (its identifier). When done explicitly, this is called a "data declaration." JavaScript data declarations are done explicitly with the var keyword. JavaScript is a "weakly typed" language. You aren't required to do explicit variable declarations, and data types can be changed during a script, "on the fly." A JavaScript identifier reserves a place in memory for a variable, but does not dictate the type of the data. You can change not only the value of the contents whenever you want, but also the data type. That is, JavaScript will infer data type based on the contents of a variable "container." You cannot do this in languages such as Fortran and C!! JavaScript supports three basic data types ("primitives") – numbers, strings of characters, and boolean (true or false). JavaScript variable names are always case-sensitive.

Literals In the statements name="Professor Wonderful"; and pi=3.14; the values on the right are literals – a string literal in the first case and a numeric literal in the second.. You should avoid using literals in your code. It is better to assign literal values to a variable with an identifier.

Objects and Methods In plain language, an "object" is a "thing" that has properties and can do things. A ball is an object. It has a size and a color. It can roll and bounce, etc. Languages such as JavaScript have "objects." These objects have properties ( document.lastModifed ) and methods document.write() ) that define what can be done by and to them.

The and Methods The prompt() and alert() Methods We will use these two methods for now to minimize interactions with HTML. Document 4.1 (circle.htm) var radius=prompt("Give the radius of a circle: "); var area=Math.PI*radius*radius; alert("The area of the circle with radius="+radius+ " is "+area+".");

Some String Methods (just a few) (See Table 4.2.) charAt(n) "HTML".charAt(3); returns a value of L. concat( {two or more string arguments} ) var s="I".concat(" love"," HTML."); s has value I love HTML. substr(m[,len]) excel.substr(0,5); returns a value of excel. excel.substr(2); returns a value of cel. toLowerCase() var h="HTML"; h=h.toLowerCase(); replaces h with the new value html.

JavaScript's Operators Table 4.3. JavaScript’s arithmetic operators. OperatorSymbolExamplesPrecedence Addition Subtraction -z – 10 2 Multiplication *a*b 1 Division /z/ modulus (remainder) % 17%3 (= 2), 16.6%2.7 (=0.4) 1 var a=3,b=4,c=5; var x,y; x=a+b*c; // has a value of 23 y=(a+b)*c; // has as value of 35

The JavaScript Assignment Operator The JavaScript assignment operator is the "equals" sign ( = ). The assignment operator is NOT the same as the "equality" sign from mathematics. The meaning of the assignment operator is: “Evaluate the expression on the right side of the assignment operator and assign the result to the identifier on the left side of the assignment operator.” In algebra, x=a+b and a+b=x are equivalent. In JavaScript, only x=a+b; is allowed. In algebra, x=x+1 makes no sense at all, but in JavaScript, x=x+1; is a perfectly reasonable statement. Only an identifier can appear on the left side of the assignment operator in a JavaScript statement.

Shorthand Arithmetic/Assignment Operators Table 4.4. Shorthand arithmetic/assignment operators. OperatorImplementationInterpretation +=x+=y;x=x+y; -=x-=y;x=x-y; *=x*=y;x=x*y; /=x/=y;x=x/y; %=x%=y;x=x%y; ++x++; or ++x;x=x+1; --y--; or --y;x=x-1;

The Math Object – Properties PropertyDescription Math.E Base of the natural logarithm, e, Math.LN2 Natural logarithm of 2, Math.LN10 Natural logarithm of 10, Math.LOG2E Log to the base 2 of e, Math.LOG10E Log to the base 10 of e, Math.PI π, Math.SQRT1_2 Square root of ½, Math.SQRT2 Square root of 2,

The Math Object – Methods MethodReturns Math.abs(x) Absolute value of x Math.acos(x) Arc cosine of x, ±π, for -1≤x≤1 Math.asin(x) Arc sine of x, ±π/2, for -1≤x≤1 Math.atan(x) Arc tangent of x, ±π/2, for -∞<x<∞ (compare with Math.atan2(y,x) ) Math.atan2(y,x) Arc tangent of angle between x-axis and the point (x,y), measured counterclockwise (compare with Math.atan(x) ) Math.ceil(x) Smallest integer greater than or equal to x Math.cos(x) Cosine of x, ±1 Math.exp(x) e to the x power (e x ) Math.floor(x) Greatest integer less than or equal to x Math.log(x) Natural (base e) logarithm of x, x>0 Math.max(x,y) Greater of x or y Math.min(x,y) Lesser of x or y Math.pow(x,y) x to the y power (x y ) Math.random() Random real number in the range [0,1] Math.round(x) x rounded to the nearest integer Math.sin(x) Sine of x Math.sqrt(x) Square root of x Math.tan(x) Tangent of x, ±∞

Some Details… Base 10 log of x: Math.log(x)/Math.log(10); or, using the Math.LN10 property, Math.log(x)/Math.LN10; Math.round(Math.random()*(n-m))+m; with (Math) { {statements that refer to properties and/or methods of the Math object for example,} var x=sin(.197); }

Using the Object Using the Math Object Document 4.3 ( mathFunctions2.htm ) Demonstration of the Math object. for (var i=1; i<=10; i++) with (Math) { var x=floor(100*random()%1))+1; document.write(x+" "+sqrt(x)+" "+ pow(x,3)+" "); }

Relational and Logical Operators OperatorInterpretationMath Symbol PrecedenceExampleValue Relational < less than <2 -3.3<0true > greater than >2 17.7>17.5false >= greater than or equal to ≥2 17.7>=17.7true <= less than or equal to ≤2 17.6<=17.7true == equal to, allowing for type conversion =3 9=="9"true === equal to, with no type conversion =3 9=="9" "a"==="a" false true != not equal to, allowing for type conversion ≠3 9!="8" 9!="9" true false !== not equal to, with no type conversion ≠3 9!="9"true Logical && AND 4 (x==3)&&(y<0) || OR5 (x==3)||(z==4) ! NOT1*1* !(x==3)

Constructs if… then… else… Constructs if ( {an expression. If true, statements are executed}) { {statements here} } // optionally else if ( {an expression. If true, statements are executed}) { {statements here} } // optionally, more else if statements // optionally else { {statements here} }

Using an Statement Using an if… Statement Only the first branch of an if statement for which the expression evaluates as true will be taken. Document 4.4 ( grades.htm ) Get letter grade var grade=prompt("What is your numerical grade?"); document.write("For a numerical grade of "+grade+ ", your letter grade is "); if (grade >= 90) document.write("A"); else if (grade >= 80) document.write("B"); else if (grade >= 70) document.write("C"); else if (grade >= 60) document.write("D"); else document.write("F"); document.write(".");

Potential Problems with Statement Potential Problems with if… Statement “If today is Tuesday or Thursday, I should be in class.” if ((today == "Tuesday") || (today == "Thursday")) ??? What happens if this expression is rewritten as (today == "Tuesday" || "Thursday") // don't do it! An alternate version of the original expression, without the two inner sets of parentheses is: // poor style! (today == "Tuesday" || today == "Thursday") ??? What happens if… if ((today = "Tuesday") || (today = "Thursday"))

The Construct The switch() Construct From Document 4.6 (daysInMonth.htm) var month=prompt("Give month (1-12): "); switch (month) { case "1": case "3": case "5": case "7": case "8": case "10": case "12": alert("There are 31 days in this month."); break; case "4": case "6": case "9": case "11": alert("There are 30 days in this month."); break; case "2": alert("There are either 28 or 29 days in this month."); break; default: alert("I do not understand your month entry."); }