Scripting for Multimedia LECTURE 3: INTRODUCING JAVASCRIPT

Similar documents
JavaScript Basics. The Big Picture

3 The Building Blocks: Data Types, Literals, and Variables

SEEM4570 System Design and Implementation Lecture 03 JavaScript

JavaScript CS 4640 Programming Languages for Web Applications

Working with JavaScript

JavaScript CS 4640 Programming Languages for Web Applications

COMP 110 Practice Exercises for Midterm Solutions

Client-Side Web Technologies. JavaScript Part I

COMS 469: Interactive Media II

Chapter 3 - Simple JavaScript - Programming Basics. Lesson 1 - JavaScript: What is it and what does it look like?

A.A. 2008/09. Why introduce JavaScript. G. Cecchetti Internet Software Technologies

CGS 3066: Spring 2015 JavaScript Reference

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

JavaScript: The Basics

Chapter 2 Working with Data Types and Operators

CSC Web Programming. Introduction to JavaScript

Princess Nourah bint Abdulrahman University. Computer Sciences Department

COMP284 Scripting Languages Lecture 14: JavaScript (Part 1) Handouts

Chapter 17. Fundamental Concepts Expressed in JavaScript

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

Variables and Typing

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

FALL 2017 CS 498RK JAVASCRIPT. Fashionable and Functional!

COMP284 Scripting Languages Lecture 15: JavaScript (Part 2) Handouts

Client vs Server Scripting

CISC 1600 Lecture 2.4 Introduction to JavaScript

INFS 2150 Introduction to Web Development and e-commerce Technology. Programming with JavaScript

Objectives. Introduction to JavaScript. Introduction to JavaScript INFS Peter Y. Wu, RMU 1

COMP519 Web Programming Lecture 11: JavaScript (Part 2) Handouts

Chapter 3 Data Types and Variables

PIC 40A. Lecture 10: JS: Wrapper objects, Input and Output, Control structures, random numbers. Copyright 2011 Jukka Virtanen UCLA 1 04/24/17

INF5750. Introduction to JavaScript and Node.js

JavaScript. The Bad Parts. Patrick Behr

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Programming language components

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

Introduction to JavaScript p. 1 JavaScript Myths p. 2 Versions of JavaScript p. 2 Client-Side JavaScript p. 3 JavaScript in Other Contexts p.

Introduction to Programming Using Java (98-388)

Relational & Logical Operators, if and switch Statements

JavaScript: Introduction, Types

Full file at

JavaScript: More Syntax

HTML5 and CSS3 More JavaScript Page 1

CS312: Programming Languages. Lecture 21: JavaScript

Why Discuss JavaScript? CS312: Programming Languages. Lecture 21: JavaScript. JavaScript Target. What s a Scripting Language?

Background. Javascript is not related to Java in anyway other than trying to get some free publicity

JavaScript. What s wrong with JavaScript?

JavaScript I Language Basics

JavaScript: More Syntax and Using Events

The course is supplemented by numerous hands-on labs that help attendees reinforce their theoretical knowledge of the learned material.

The JavaScript Language

JAVASCRIPT FOR THE C# DEVELOPER

Programming for the Web with PHP

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

Accelerating Information Technology Innovation

5. JavaScript Basics

Web Application Development

Computer Components. Software{ User Programs. Operating System. Hardware

Lecture 5 Tao Wang 1

MatchaScript: Language Reference Manual Programming Languages & Translators Spring 2017

Boot Camp JavaScript Sioux, March 31, 2011

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

What is Java Script? Writing to The HTML Document. What Can JavaScript do? CMPT 165: Java Script

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

CSC Software I: Utilities and Internals. Programming in Python

Language Reference Manual simplicity

ARG! Language Reference Manual

COMP519 Web Programming Lecture 12: JavaScript (Part 3) Handouts

JavaScript or: How I Learned to Stop Worrying and Love a Classless Loosely Typed Language

JAVA REVIEW cs2420 Introduction to Algorithms and Data Structures Spring 2015

What is PHP? [1] Figure 1 [1]

JavaScript Syntax. Web Authoring and Design. Benjamin Kenwright

JavaScript Lecture 1

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

Outcomes Week 2 Overview Describe the inputs, activities, and outputs of each step in the software development life cycle. Describe arithmetic, relati

<form>. input elements. </form>

isnan function returns true if the argument is not a number otherwise it is false.

Javascript. UNIVERSITY OF MASSACHUSETTS AMHERST CMPSCI 120 Fall 2010

Chapter 4 Statements. Slides Modified by Vicky Seno

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

JavaScript Introduction

JavaScript Pocket Reference, 2nd Edition By David Flanagan. Publisher: O'Reilly Pub Date: October 2002 ISBN: Pages: 136 Slots: 0.

CITS1231 Web Technologies. JavaScript

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Web Site Design and Development JavaScript

Variables, Constants, and Data Types

Lesson: Web Programming(4) Omid Jafarinezhad Sharif University of Technology

Computer Components. Software{ User Programs. Operating System. Hardware

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

FUNctions. Lecture 03 Spring 2018

More on JavaScript Functions

JavaScript Lecture 2

Chapter 7 Control I Expressions and Statements

JScript Reference. Contents

Transcription:

Scripting for Multimedia LECTURE 3: INTRODUCING JAVASCRIPT

Understanding Javascript Javascript is not related to Java but to ECMAScript It is widely used for client-side scripting on the web Javascript, Jscript, and ActionScript Javascript is untyped

Data Most data can be broken into smaller pieces called values. JS defines a value type as an object, a primitive value, or a function Types of primitive value: undefined, null, Boolean, number or string A function is a callable object A function that is a member of an object is called a method Build-in objects in JS the global object, the Object object, the Function object, the Array object, the String object, the Number object...

Data Number type in JavaScript double precision, 64-bit binary formation, IEEE 754 value 63 62 52 51 0 sign exponent fraction Special values NaN Infinity -Infinity

Data You can create a string by encoding in single or double quotes Examples "Every good boy deserves fudge" 'The quick brown fox jumps over the lazy dog' 'The doctor said "Today is your lucky day!"' "I'm going to be happy when you give the news!" Using backslash (\) Examples 'The doctor said "I\'m pleased to announce that it\'s a girl!" ' "The doctor said \"I'm pleased to announce that it's a girl!\" " \t and \n

Data String concatenation using plus sign Examples 'Hickory Dickory Dock.' + "The mouse ran up the clock." + 'The clock struck one' 'Hickory Dickory Dock.' + "The mouse ran up the clock." + 'The clock struck one'

Data Using unary operators There is only a single operand typeof 'Hello World' typeof 19.5 typeof true

Data Boolean 10 < 9; 20 > 3 5 <= 4; 7>=8 'Apples'!= 'Oranges' 10 == 13-3 Logical operators 'Apples'=='Oranges'&& 5 > 3 5 > 10 4 < 2!(7 > 5 1 > 2) Short-circuiting operators

Statement Variables Examples var totalcost = 2 * 21.15; var tax = totalcost *.05; or var totalcost; var tax; totalcost = 3 * 21.15; tax = totalcost * 0.05;

Statement Rules for naming variables A variable name can contain numbers but cannot begin with a number 4YourEyes, 2give, 1ForAll X Must not contain mathematical or logical operators monday-friday, cost*5 X Must not contain any punctuation marks other than _ and $ Must not contain any spaces Must not be JavaScript keywords Case-sensitive

Statement The name of the variable should be descriptive enough Some examples //bad examples var last; var current; var changed; //good examples var lastaccesseddata; var currentvehicle; var vehiclemakewaschanged //last accessed date //current vehicle //the vehicle make was changed Recommend to use camel casing

Function A function is a grouping of statements A function can be declared by using the function keyword and then providing a name (identifier) Example function Add(x, y) { return x + y; //call function var a = 5; var b = 10; var c = Add(a, b);

Function A function expression produces a value of type function Assign function expressions to variables or execute them directly Example var addfunction = function(x, y) { return x + y; ; var c = addfunction(5, 10); addfunction is called after the function expression assigned to addfunction variable addfunction variable is of type function An anonymous function

Function JavaScript is very loose when passing arguments to functions Too many arguments the extras is discarded Arguments are not enough parameter values for missing arguments will be undefined Advantage You can add parameters to method already been created and called Disadvantage You may inadvertently pass an incorrect quantity of arguments with no indication of a problem

Function Using the browser's built-in alert, prompt, and confirm functions alert alert('here is an alert');

Function Using the browser's built-in alert, prompt, and confirm functions prompt var prompresult = prompt('this is a prompt form information', 'default value');

Function Using the browser's built-in alert, prompt, and confirm functions confirm var confirmresult = confirm('do you confirm?');

Function These built-in functions can be overwritten because the function name is variable Example prompt = function(){ return 'hello again';

Scoping variables In JavaScript, there are essentially two scopes global local Local scope is function scope Variables declared anywhere inside the function will have a local function scope Declare all function variables at the top of the function

Scoping variables Do NOT create global variables implicitly! Example totalcost = 2 * 21.15; tax = totalcost *.05; Global? local?

Scoping variables Nesting functions Example function areaofpizzaslice(diameter, slicesperpizza) { return areaofpizza(diameter) / slicesperpizza; function areaofpizza(diameter) { var radius = diameter / 2; return 3.1415926 * radius * radius; Nested functions are private to the parent function Variables in the nested function are local A nested function can access variables in parent function and grandparent function

Converting to a different type Number function Example var age = prompt('enter age', ''); alert('you will soon be ' + age + 1 + ' years old!'); What will be displayed when running the code? var age = prompt('enter age', ''); alert('you will soon be ' + Number(age) + 1 + ' years old!'); var age = prompt('enter age', ''); alert('you will soon be ' + (Number(age) + 1) + ' years old!');

Converting to a different type String function Example var x = 10; var y = 20; alert(x + y); //----------------- var x = 10; var y = 20; alert(string(x) + String(y));

Conditional programming if/else Examples var age = prompt('enter age', ''); if(isnan(age)) alert('you need to enter a valid number'); else alert('you will soon be ' + (Number(age) + 1) + ' years old!');

Conditional programming if/else Examples var age = prompt('enter age', ''); if(isnan(age)) { alert('you need to enter a valid number'); else if(number(age) >= 50) { alert('you're old! You will soon be ' + (Number(age) + 1) + ' years old!'); else if(number(age) <= 20) { alert('you're a baby! You will soon be ' + (Number(age) + 1) + ' years old!'); else { alert('you will soon be ' + (Number(age) + 1) + ' years old!');

Conditional programming switch Example var carcolor = prompt('what color car would you like to buy?', 'white'); switch (carcolor) { case 'red': alert('red is a fancy choice!'); break; case 'white': alert('white is in stock and you get a discount!'); default: alert('the color: ' + carcolor + ' is not known.'); break; ;

Conditional programming switch Example var age = prompt('enter your age', ''); age = Number(age); switch (true) { case isnan(age): age = 0; alert('you need to enter a valid number'); break; case (age >= 50): age = Number(age) + 1; alert('you're old! You will soon be ' + age + ' years old!'); break; default: alert('you will soon be ' + (Number(age) + 1) + ' years old!'); break; ;

Conditional programming Determining whether a variable has a value using if keyword If the variable has a value true If the variable is undefined or null false Example if(myvar) { alert('myvar has a value'); else { alert('myvar does not has a value');

Conditional programming No value coalescing operators Example var customer = prompt('please enter your name'); alert('hello ' + (customer 'Valued Customer')); operatos can be chained var customer = prompt('please enter your name'); var companyname = prompt('please enter your company name'); alert('hello ' + (customer companyname 'Valued Customer')); && operator exhibits similar behavior but returns the first empty value

Conditional programming Determine two values have the same type and are equal True null == undefined; false == 0; '' == 0; '123' == 123; False null === undefined false === 0; '' === 0; '123' === 123;

Loops while Example var x = 10; while(x > 0) { x--; alert("the value of x is " + x);

Loops do Example var retries = 0; do { retries++; showloginscreen(); while(!authenticated() && retries < 3); if(retries==3) { alert('too many tries'); return;

Loops for Example for(var counter = 0; counter < 10; counter++) { alert('the counter is now set to ' + counter);

Loops Breaking out of a loop (break) break will exit only from the current loop Example var numbertotest = prompt('type number here.', ''); var index = 2; var isprime = true; while (index < numbertotest) { if (numbertotest % index == 0) { isprime = false; break; index++;

Handling errors try/catch/finally finally block is executed after the try block successfully completes or the catch block completes. Example try { undefinedfunction(); alert('made it, so undefinedfunction exists'); catch(ex) { alert('the following error occurred: ' + ex.message); finally { alert('finally block executed');