JavaScript CS 4640 Programming Languages for Web Applications

Similar documents
JavaScript CS 4640 Programming Languages for Web Applications

CSC Web Programming. Introduction to JavaScript

Client-Side Web Technologies. JavaScript Part I

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

JavaScript: The Basics

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

CGS 3066: Spring 2015 JavaScript Reference

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

COMS 469: Interactive Media II

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

JavaScript Functions, Objects and Array

Working with JavaScript

Outline. Introduction to JavaScript Resources What is JavaScript? JavaScript in web pages

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

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

Basics of JavaScript. Last Week. Needs for Programming Capability. Browser as Development Platform. Using Client-side JavaScript. Origin of JavaScript

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

Web Site Design and Development JavaScript

INF5750. Introduction to JavaScript and Node.js

5/19/2015. Objectives. JavaScript, Sixth Edition. Introduction to the World Wide Web (cont d.) Introduction to the World Wide Web

Lecture 3: The Basics of JavaScript. Background. Needs for Programming Capability. Origin of JavaScript. Using Client-side JavaScript

CHAD Language Reference Manual

JavaScript: Introduction, Types

This course is designed for web developers that want to learn HTML5, CSS3, JavaScript and jquery.

JavaScript. The Bad Parts. Patrick Behr

UNIT -II. Language-History and Versions Introduction JavaScript in Perspective-

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

JavaScript By: A. Mousavi & P. Broomhead SERG, School of Engineering Design, Brunel University, UK

JavaScript: the language of browser interactions. Claudia Hauff TI1506: Web and Database Technology

Such JavaScript Very Wow

JavaScript Basics. The Big Picture

JavaScript I Language Basics

JScript Reference. Contents

JavaScript. Training Offer for JavaScript Introduction JavaScript. JavaScript Objects

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Chapter 2 Working with Data Types and Operators

E ECMAScript, 21 elements collection, HTML, 30 31, 31. Index 161

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

c122mar413.notebook March 06, 2013

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

CSCE 120: Learning To Code

Varargs Training & Software Development Centre Private Limited, Module: HTML5, CSS3 & JavaScript

Client vs Server Scripting

FALL 2017 CS 498RK JAVASCRIPT. Fashionable and Functional!

Chapter 4 Basics of JavaScript

PHP by Pearson Education, Inc. All Rights Reserved.

Web Programming and Design. MPT Junior Cycle Tutor: Tamara Demonstrators: Aaron, Marion, Hugh

COMS 469: Interactive Media II

JavaScript Specialist v2.0 Exam 1D0-735

710 Index Attributes, 127 action attribute, 263 assigning, bottom attribute, domain name attribute, 481 expiration date attribute, 480 8

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

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

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

PHPoC vs PHP > Overview. Overview

JavaScript Lecture 1

JAVASCRIPT. sarojpandey.com.np/iroz. JavaScript

Part 1: jquery & History of DOM Scripting

Programming language components

EXERCISE: Introduction to client side JavaScript

Web Application Development

Java Primer 1: Types, Classes and Operators

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

The JavaScript Language

Visual C# Instructor s Manual Table of Contents

JavaScript: Sort of a Big Deal,

Java+- Language Reference Manual

Midterm Exam. 5. What is the character - (minus) used for in JavaScript? Give as many answers as you can.

Comprehensive AngularJS Programming (5 Days)

8/19/2018. Web Development & Design Foundations with HTML5. Learning Objectives (1 of 2) Learning Objectives (2 of 2) What is JavaScript?

CS312: Programming Languages. Lecture 21: JavaScript

Tutorial 10: Programming with JavaScript

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

Javascript Arrays, Object & Functions

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

Programming for the Web with PHP

JavaScript: Introductionto Scripting

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

HTML5 and CSS3 More JavaScript Page 1

PHPoC. PHPoC vs PHP. Version 1.1. Sollae Systems Co., Ttd. PHPoC Forum: Homepage:

Manju Muralidharan Priya. CS4PM Web Aesthetics and Development WEEK 11

5. Strict mode use strict ; 6. Statement without semicolon, with semicolon 7. Keywords 8. Variables var keyword and global scope variable 9.

Lecture 8 (7.5?): Javascript

CS558 Programming Languages

Produced by. App Development & Modelling. BSc in Applied Computing. Eamonn de Leastar

UNIT- 3 Introduction to C++

Chapter 17. Fundamental Concepts Expressed in JavaScript

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

MatchaScript: Language Reference Manual Programming Languages & Translators Spring 2017

JAVASCRIPT MOCK TEST JAVASCRIPT MOCK TEST I

Web Development. with Bootstrap, PHP & WordPress

The Warhol Language Reference Manual

Javascript. UNIVERSITY OF MASSACHUSETTS AMHERST CMPSCI 120 Fall 2010

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Web Application Development (WAD) V th Sem BBAITM(Unit-1) By: Binit Patel

Node.js Training JavaScript. Richard richardrodger.com

The course also includes an overview of some of the most popular frameworks that you will most likely encounter in your real work environments.

CS1520 Recitation Week 2

Chapter 12 Variables and Operators

Transcription:

JavaScript CS 4640 Programming Languages for Web Applications 1

How HTML, CSS, and JS Fit Together <html> {css} javascript() Content layer The HTML gives the page structure and adds semantics Presentation layer The CSS enhances the HTML page with rules that state how the HTML content is presented Behavior layer The JS controls how the page behaves, adding interactivity 2

JavaScript: Some History JavaScript was introduced as part of the Netscape 2.0 browser Microsoft soon released its own version called Jscript ECMA developed a standard language known as ECMAScript ECMAScript Edition 6 is widely supported and is what is commonly called JavaScript JavaScript = ECMAScript + DOM API JavaScript 1.0 ES1 ES2 ES3 AJAX JQuery ES5 ES6 3

Developing JavaScript Software Writing JavaScript code Any text editor (e.g., Vim, Notepad, Emacs) Specialized software (e.g., MS Visual InterDev) Executing JavaScript Load into browser (need HTML document) Browser detects syntax and run-time errors Firefox: JavaScript console lists errors Window (Ctrl-Shift-J) Mac (Command-Shift-J) IE: Exclamation icon and pop-up window (Bottom left) 4

JavaScript is not Java JavaScript JavaScript is a scripting language : Embedded in HTML Interpreted as the page is loaded Can manipulate the HTML page JavaScript Core Client side Server side Primary purpose is for client-end processing of HTML documents Netscape s Livewire, first server-side JS engine, allows JS to be used for form processing on the server We will not discuss server side JS No type checking 5

JavaScript Characteristics JavaScript does not need to be compiled JS is an interpreted language A JS interpreter is software that runs inside a browser that reads and executes JavaScript Interpreted vs. compiled languages : Advantage : simplicity Disadvantages : efficiency, maintainability, scalability, reliability 6

Why and Why Not JavaScript? What can be done with JS on the client and cannot be done with other techniques on the server? Monitor user events and take action Some dynamic effects What can be done on both client and server, but are better with JS? Build HTML dynamically when page is loaded Interactive web pages Communicate with the server asynchronously (Ajax) What are the drawbacks of JS? Platform dependent Can be turned off Performance Security Hard to write reliable and maintainable JS 7

Embedding JS in HTML Use the <script> tag to embed JS code in <head> or <body> Functions and code that may execute multiple times are typically placed in the <head> section These are only interpreted when the relevant function or eventhandler are called Code that needs to be executed only once, when the document is first loaded is placed in the <body> section 8

Embedding JS in HTML JS code may be written in a separate file. The external file can be included in an HTML file using the src attribute of a <script> tag JS code is visible in the client browser Do not hardcode anything that you don t want the client to see 9

Script Calls Some script calls may be embedded in the HTML tags or Script is evaluated once encountered by browser 10

First Example [see jex1.html] 11

Variables Variables are loosely typed Type is determined dynamically based on the value stored The typeof operator can be used to check type of a variable Declarations are made using the var keyword Variables declared but not initialized have the value undefined Variables used and not declared or initialized have value Null Names start with letters, $, or _ followed by any sequence of letters, $, _, or digits Case sensitive 12

Variables Variable is a container that hold things for later use String Number Undefined Null Objects (including arrays) Symbols Functions 13

Variables Loose typing means that JS figures out the type based on the value Variables have block scope. Declarations outside of any function are global Declarations within a function are local to that function 14

Expressions If operator is + and an operand is string, it treats the + as a string concatenation operator and coerce other operand to string If operator is arithmetic, and string value can be coerced to a number, it will do so If string is non-numeric, result is NaN (NotaNumber) String can be explicitly converted to a number using parseint and parsefloat [see jex2.html] 15

Using Arithmetic Operators Let s initialize, y = 4 Operator Description Example Resulting x + Addition x = y + 5 9 x = y + 5 45 x = Four + y + 4 Four44 - Subtraction x = y - 2 2 ++ Increment x = y++ 4 x = ++y 5 -- Decrement x = y-- 4 x = --y 3 * Multiplication x = y * 4 16 / Division x = 10 / y 2.5 % Modulo x = y % 3 1 16

Using Assignment Operators Let s initialize, x = 10 Operator Example Equivalent arithmetic operators Resulting x = x = 5 x = 5 5 += x += 5 x = x + 5 15 -= x -= 5 x = x - 5 5 *= x *= 5 x = x * 5 50 /= x /= 5 x = x / 5 2 %= x %= 5 x = x % 5 0 17

Applying Comparison and Conditional operators Let s initialize, x = 10 Operator Description Example Result == Equal to (value only) x == 8 false x == 10 true === Equal to (value and type) x === 10 true x === 10 false!= Not equal (value only) x!= 5 true!== Not equal (value and type) x!== 10 true x!== 10 false > Greater than x > 5 true >= Greater than or equal to x >= 10 true < Less than x < 5 false <= Less than or equal to x <= 10 true 18

Chaining Multiple Comparisons with Logical Operators Let s initialize, x = 10 and y = 5 Operator Description Example Result && And (x == 10 && y == 5) true (x == 10 && y > x) false Or (x >= 10 y > x) true (x < 10 && y > x) false! Not!(x == y) true!(x > y) false Mix (x >= 10 && y < x x == y) true ((x < y x >= 10) && y >= 5) true (!(x == y) && y >= 10) false 19

Control Structures (if Statement) 20

Control Structures (switch Statement) 21

Looping (while Loop) 22

Looping (do-while Loop) 23

Looping (for Loop) 24

Looping (for-in Loop) 25

Array Objects More relaxed version of Java arrays Size can be changed and data can be mixed Cannot use arbitrary keys as with PHP arrays - index Creating arrays Using the new operator and a constructor (Array) with multiple arguments Using the new operator and a constructor (Array) with a single numeric argument, assigned the size of array Using square brackets to make a literal 26

Array Predefined Operations Concat two arrays into one copies to end Join array items into a single string (commas between) Push, pop, shift, unshift easy implementation of stacks and queues Push and pop are a right stack add/remove from end Shift and unshift are a left stack add/remove from beginning queue Sort Sort by default compares using alphabetical order To sort using numbers we pass in a comparison function defining how the numbers will be compared Reverse the items in an array [see jex4.html] 27

Functions Similar to Java functions but header is somewhat different Return type not specified (like PHP, since JS has dynamic typing) Parameter types also not specified Functions execute when they are called, just as in any language 28

Anonymous Functions and Function Expressions Functions can be assigned to variables Variables declared in a function are local to the function Parameters are all value No parameter type-checking Function expression [see jex3.html] 29

Immediately Invoked Function Expressions Anonymous functions can be executed once as the interpreter comes across them Parentheses tell the interpreter to call the function immediately Grouping operators tell the interpreter to treat this as an expression 30

Functions and Default Values (ES6) 31

Global and Local Scopes Global scope Local scope (function-level scope) Global scope Local scope (function-level scope) Naming collision Two JavaScript files, both have a global variable with the same name 32