Basic Data Types Numbers (integer and floating point)‏ Strings (sequences of characters)‏ Boolean values (true/false)‏

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

1 COMM 1213 H1 COMP 4923 X1 JavaScript 1 (Readings: Ch. 10, 11 Knuckles)
1 CSC 551: Web Programming Spring 2004 client-side programming with JavaScript  scripts vs. programs  JavaScript vs. JScript vs. VBScript  common tasks.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
The Web Warrior Guide to Web Design Technologies
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
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.
Javascript Client-side scripting. Up to now  We've seen a little about how to control  content with HTML  presentation with CSS  Javascript is a language.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Functions Part I.
JavaScript, Fourth Edition
JavaScript, Third Edition
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.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
WEB DESIGN AND PROGRAMMING Introduction to Javascript.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
Database-Driven Web Sites, Second Edition1 Chapter 3 INTRODUCTION TO CLIENT-SIDE SCRIPTS.
1 CLIENT-SIDE SCRIPTS. Objectives 2 Learn how to reference objects in HTML documents using the HTML DOM and dot syntax Learn how to create client-side.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Data types, Literals (constants) and Variables Data types specify what kind of data, such as numbers and characters, can be stored and manipulated within.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
CHAPTER 4 Java Script อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Using Client-Side Scripts to Enhance Web Applications 1.
DHTML AND JAVASCRIPT Genetic Computer School LESSON 5 INTRODUCTION JAVASCRIPT G H E F.
Lecture 9 The Basics of JavaScript Boriana Koleva Room: C54
Introduction to PHP A user navigates in her browser to a page that ends with a.php extension The request is sent to a web server, which directs the request.
Keeping it Neat: Functions and JavaScript Source Files Chapter 7.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
4.4 JavaScript (JS) Deitel Ch. 7, 8, 9, JavaScript & Java: Similarities JS (JavaScript) is case-sensitive Operators –arithmetic: unary +, unary.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
Tharith Sriv.  JavaScript is a simple programming language that can be written directly into HTML documents to allow for increased interactivity with.
Variables and Data Types Data (information we're going to store) – Numbers – Text – Dates What types of data can JavaScript process? How do we store it?
 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
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
By Mr. Muhammad Pervez Akhtar
Chapter 2 Murach's JavaScript and jQuery, C2© 2012, Mike Murach & Associates, Inc.Slide 1.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
JavaScript and Ajax (JavaScript Data Types) Week 2 Web site:
Project 4: Working with Variables Essentials for Design JavaScript Level One Michael Brooks.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Expressions and Data Types Professor Robin Burke.
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.
JavaScript: A short introduction Joseph Lee Created by Joseph Lee.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
INTERNET APPLICATIONS CPIT405 JavaScript Instructor: Rasha AlOmari
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
>> Introduction to JavaScript
Chapter 10 - JavaScript: Functions
WEB PROGRAMMING JavaScript.
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
Web DB Programming: PHP
The Internet 11/22/11 Conditionals and Loops
Introducing JavaScript
JavaScript: Introduction to Scripting
CIS 136 Building Mobile Apps
Review of Java Fundamentals
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

Basic Data Types Numbers (integer and floating point)‏ Strings (sequences of characters)‏ Boolean values (true/false)‏

Integer literals decimal: any sequence of decimal digits not starting with 0 octal: a 0 followed by any sequence of octal digits hexadecimal: a 0x (or 0X) followed by any sequence of hexadecimal digits (0-9,a-f,A- F)‏

Floating point literals decimal integer followed by: optionally, the decimal point (.) and a sequence of decimal digits followed by: optionally, an exponent consisting of the letter ‘e’ or ‘E’, optionally, the sign of the exponent (+|-) and the decimal integer value of the exponent

String literals any sequence of characters enclosed in a matching pair of single quotes (') or double quotes (")‏ be careful with quote characters inside strings

Examples 'a string literal 1234' ' he said, "Stop!" ' " he said, 'Stop!' " ' what's that sound' - ERROR ' what\'s that sound' - OK

Implicit Type Conversions truestring as number non-empty string Context 1 / 0"true"/ "false" true/false 0 – false other - true number as string number false0empty string BooleanNumberString value

Variables an area of memory allocated to hold numeric, string, or boolean value The value of a variable can "vary" or change as a program executes In JavaScript, even the type of the variable can change – JavaScript is loosely typed In other languages, the type of a variable is not allowed to change – strongly typed

String operators the plus (+) operator can be used to concatenate two strings var x = " house "; var y = ' cat '; z = x + y; // ' house cat ' z = x + 'boat'; // ' house boat' z = "large" + y; // 'large cat'

Combining Strings and Numbers the plus (+) can be used to combine strings and numbers resulting in a string; but be careful! var x = 12; var y = 7; var z = x + " is the answer"; z = "the answer is " + y; z = x + y + " is the answer"; z = "The answer is " + x + y;

Compound Statement: if Composed of a condition and a sequence of statements if true and sequence of statements if false if ( income > ) then { tax = income * 0.25; } else { tax = income * 0.18; }

Compound Statement: while Composed of a condition and a sequence of statements that are executed repeatedly until the condition is false while ( X > 0 ) { Y = Y + X; X = X – 3; }

Other Selection/Looping Constructs switch do / while for break/continue

Switch switch (command)‏ { case "Fetch!": … break; case "Roll over!": … break; default: … }

do/while do { … } while ( a < b );

for for ( count = 0; count < 10; count++ )‏ { x = count*x; … }

break/continue break – used to exit a loop at an abnormal location continue – used to return to the top of the loop prematurely

Declaring functions function difference( a, b ) { var diff; if ( a < b ) { diff = b – a; } else { diff = a – b; } return diff; }

Variable Scoping every variable in a program either has global or local scope global scope means that the variable can be referred to anywhere within the entire program local scope means the the variable can be referred to only within the function it is declared local declarations of variables "hide" global variables with the same name

Variable Scoping var A = 3; function f() { var A = 7; } f(); alert( A ); function f() { var A = 7; } f(); alert( A ); // error! var A = 3; function f() { A = 7; } f(); alert( A ); Hidden ALocal AGlobal A

Variable Scoping Guidelines there should be as few global variables as possible in a program; none is ideal functions should depend on as few global variables as possible; none is ideal

Scoping Tragedy #1 function foo( a, b ) { foo = a * b; return foo; } alert( foo( 2, 2 ) ); alert( foo( 3, 5 ) );

Empty Statements var A = 7; while ( A > 0 ); { alert( A*A ); A = A – 1; }

Implicit Type Conversion while( first_guess – next_guess )‏ { … } be careful using floating point values where a boolean value is required

Object An object is a collection of named pieces of data These named values are usually referred to as properties of the object

Creating objects #1 var emp1 = {}; emp1.name = "Bob"; emp1.age = 55; emp1.yearsW = 20;

Creating objects #2 var emp2 = { name:"Carol", age:43, yearsW:15 };

Properties can be: primitive data values (Number, String, Boolean)‏ other objects functions; in this case, the property is called a method

Example var club = {}; club.name = "Heck's Angles"; club.secretary = emp1; club.treasurer = emp2; alert ( club.secretary.name ); alert ( club.treasurer.yearsW );

Object arguments to functions primitive data values are passed into functions by-value; a copy of the argument is made inside the function object data values are passed into functions by-reference; the function and the caller refer to the same data values

Call-by- Value vs. Reference var emp = { name:"john", age:27 }; function foo( x ) { x.name = "bob"; } foo( emp); var emp = { name:"john", age:27 }; function foo( x ) { x = "bob"; } foo( emp.name );

Arrays An array is a collection of data values similar to an object In this case, each value in the array is referred to by a number which is its index The indexes of an array start with 0 Usually the values within an array are homogeneous

Creating arrays #1 var foods = []; foods[0] = "apple"; foods[1] = "pear"; foods[2] = "banana";

Creating arrays #2 var cars = [ "Ford", "Honda", "BMW" ];

Array elements can be: primitive data values (Number, String, Boolean)‏ objects other arrays functions

Example var club_members = []; club_members[0] = emp1; club_members[1] = emp2; // "Bob" alert ( club_members[0].name ); // 15 alert ( club_members[1].yearsW );

Array length "property" every array has a "property" length which is the number of elements in the array e.g. var cars = [ "Ford", "Honda", "BMW" ]; cars.length; // 3

Value vs. Reference Types Numbers, Strings, and Booleans are value types; a copy is made when these values are assigned or passed to a function Objects and Arrays are reference types, only a copy of the reference is made when these values are assigned or passed to function; the entire object/array is not duplicated

Call-by- Value vs. Reference var food = [ "lemon", "orange" ]; function foo( x ) { x[0] = "bob"; } foo( food ); var food = [ "lemon", "orange" ]; function foo( x ) { x = "bob"; } foo( food[0] );

Running JavaScript Embed the script in an XHTML page: The file named by the src attribute contains your JavaScript program Reload the XHTML page in the browser

Built-in functions alert( message ) – displays the string message in a dialogue box prompt( message, default) – requests input from the user; message is displayed and default is the default value

Built-in functions parseInt( string, base ) – converts the string argument to an integer value assuming it is in base base parseFloat( string ) – converts the string argument to a floating point value

Math functions Math.abs(x) – absolute value of x Math.sqrt(x) – square root of x Math.min(a,b) – minimum of a, b Math.max(a,b) – maximum of a, b Math.floor( x ) – x rounded down to nearest int Math.ceil( x ) – x rounded up to nearest int Math.round( x) – usual rounding rules

Debugging JavaScript Use the Netscape browser if the program fails to run type: javascript: in the "Location:" field in the browser Correct the errors displayed reload the XHTML document repeat