CGS 3066: Web Programming and Design Spring 2016

Slides:



Advertisements
Similar presentations
14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,
Advertisements

Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
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.
JavaScript, Fourth Edition
JavaScript, Third Edition
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
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.
Python quick start guide
WEB DESIGN AND PROGRAMMING Introduction to Javascript.
4. Python - Basic Operators
Javascript fundamentals (continue). Visual Web Developer wd/download/
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
CISC474 - JavaScript 03/02/2011. Some Background… Great JavaScript Guides: –
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
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.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
1 Session 3: Flow Control & Functions iNET Academy Open Source Web Programming.
Using Client-Side Scripts to Enhance Web Applications 1.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
CPS120: Introduction to Computer Science Decision Making in Programs.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - JavaScript: Control Structures I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
JavaScript, Fourth Edition
 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.
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.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
CGS 3066: Web Programming and Design Spring 2016 Programming in JavaScript.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
CGS 3066: Web Programming and Design Spring 2017
Chapter 6 JavaScript: Introduction to Scripting
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Scope, Objects, Strings, Numbers
JavaScript Syntax and Semantics
JavaScript: Functions.
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
The structure of computer programs
JavaScript and Ajax (Expression and Operators)
Arrays, For loop While loop Do while loop
ARRAYS MIT-AITI Copyright 2001.
Arithmetic operations, decisions and looping
Chapter 8 JavaScript: Control Statements, Part 2
WEB PROGRAMMING JavaScript.
Chapter 3: Introduction to Problem Solving and Control Statements
JavaScript Data Concepts
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
Object Oriented Programming in java
Logical Operations In Matlab.
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
M150: Data, Computing and Information
CSCI N207 Data Analysis Using Spreadsheet
Loops CGS3416 Spring 2019 Lecture 7.
CIS 136 Building Mobile Apps
Web Programming and Design
Lecture 9: JavaScript Syntax
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
CGS 3066: Web Programming and Design Fall 2019
Presentation transcript:

CGS 3066: Web Programming and Design Spring 2016 Programming in JavaScript

Expressions  An expression is any piece of code that resolves to a value(numeric/string/Boolean etc.) We can combine literal values, variables and operators to create complex expressions 5,”ABC”,true, x, 1+7, 3+x, 2*x+3, x>5, x=4 are all expressions (given that x is a variable) Expression that uses assignment(e.g. =,+= etc.) operator, resolves to the value that is finally assigned to left side variable Expression that compares two value/expressions using a relational operator, resolves to either true or false Expression that applies logical operator(&&, ||, !) over values/other expressions, resolves to either true or false

Expressions(contd.) var x,y; x=10 //expression value is 10 y=7 //expression value is 7 x+4 //expression value is 14, x is still 10 x+=y//equivalent to x=x+y; expression value is 17, x is 17 y=x-2 //expression value is 15, y is 15 Create your own expression and test its value using document.writeln() function

JavaScript Relational Operators Meaning Expression value Example < Value less than true if left-side operand has a value smaller than that of the right-hand operand. false otherwise 5<10 is true 10<9 is false 10<10 is false > Value greater than true if left-side operand has a value greater than that of the right-hand operand. false otherwise 5>10 is false 10>9 is true 10>10 is false <= Value less than or equal to true if left-side operand has a value smaller than or equal to that of the right-hand operand. false otherwise 5<=10 is true 10<=9 is false 10<=10 is true >= Value greater than or equal to true if left-side operand has a value greater than or equal to that of the right-hand operand. false otherwise 5>=10 is false 10>=9 is true 10>=10 is true == Value equal to true if left-side operand has the same value as the right-hand operand. false otherwise 1==2 is false 1==1 is true 1==“1” is true != Value not equal to true if left-side operand has a different value than that of the right-hand operand. false otherwise 1!=2 is true 1!=1 is false 1!=“1” is false === Value and type equal to true if both sides have same value and same data type. false otherwise 1===2 is false 1===1 is true 1===“1” is false !== Value or type not equal to true if both sides have mismatched value or data type or both. false otherwise 1!==2 is true 1!==1 is false 1!==“1” is true

JavaScript Logical Operators Usage Expression value Example && operand1 && operand2 true if both operands are,or can be converted to true. false otherwise true && true is true true && false is false false && true is false false && false is false || operand1 || operand2 true if either one of the two operands is true,or can be converted to true. false if both operands are false true || true is true true || false is true false || true is true false || false is false ! !operand true if left-side expression has a value smaller than or equal to that of the right-hand expression. false otherwise !(true) is false !(false) is true

JavaScript falsy values The following data values are considered ‘falsy’, equivalend to Boolean false 0 (numeric) false (Boolean) “” or ‘’ (empty string of characters) null undefined NaN (Numeric values indicating Not a Number)

Entering special characters in document.writeln() \’ - prints single quote \” - prints single quote \\ - prints backslash \n - new line \t – tab \r - carriage return \b - backspace

Conditional Statement Allows decision-making based on logically described criteria You choose to run one piece of code depending on some logical condition. Such piece of code and the condition that triggers it, is described in a conditional statement If statement: if some condition is true, run a block of code; otherwise, run another block of code.

Conditional Statement If statement: if ( 10 > 5 ){ //run the code here } If … else… statement: if( 10 > 5 ){ //run the code here } else{ //run a different piece of code here}

Loop Statement Loop is a way of repeating the same block of code over and over. While loop: repeats a block of code while a condition is true. var counter = 1; while(counter < 10){ alert(counter); counter++; //counter = counter + 1; } Do-while loop: specify looping condition after the statement block Executes at least once do{ counter++;} while(counter < 10);

For Statement For loop: it combines three semicolon-separated pieces information between the parentheses: initialization, condition and a final expression. for(var counter = 1; counter < 10; counter++) { alert(counter); }

Array An array is used to store multiple values in a single variable. An array is just one variable that contains a list of values. e.g., var numbers = new Array(); numbers[0] = 12; numbers[1] = 15; numbers[2] = 45; numbers[3] = 22; index value 12 15 1 2 45 3 22

Create and Access An Array var trees = new Array(“maple”, “ashley”, “oak”); var countries = [“America”, “Spain”, “China”]; Access an array: you refer to an element in an array using the index. var myCountry = countries[0];

Array Methods and Properties An array has predefined properties and methods Use dot(.) operator to access property/method of a given array Methods require appropriate input parameter in parantheses e.g: //the number of elements in an array named countries. alert(countries.length); //find the index of a particular element in an array. alert(countries.indexOf(“China”); //add a new item at the end of the array countries.push(“South Sudan”); //copy and remove last item from the array var lastitem = countries.pop(); //x is “South Sudan”

JavaScript Functions A function is a block of code that can be reused. It consists of a function name, a parameter list, and code that is executed when the function is called. When needed, we can call a javascript function by it’s name and provided a list of arguments(to be mapped to parameters) Functions may also be called automatically on web events(e.g. clicking a button) After the code inside the function are executed, execution of code resumes after the calling context. Function may return values back to the calling context

Function Example //declaration of function RectangleArea. Function name //declaration of function RectangleArea. function RectangleArea (height,width){ return height*width; }//end of function declaration //this is a function call var area = RectangleArea(100,40); alert(area); //code execution resumes here after function call Parameter list Function body Argument list