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

Similar documents
CSC Web Programming. Introduction to JavaScript

JavaScript CS 4640 Programming Languages for Web Applications

JavaScript CS 4640 Programming Languages for Web Applications

Client-Side Web Technologies. JavaScript Part I

Web Site Design and Development JavaScript

INF5750. Introduction to JavaScript and Node.js

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

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

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

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

JavaScript: The Basics

JavaScript I Language Basics

Javascript Arrays, Object & Functions

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

Working with JavaScript

JavaScript: Introduction, Types

Introduction to Programming Using Java (98-388)

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

Web Development. HCID 520 User Interface Software & Technology

PHP by Pearson Education, Inc. All Rights Reserved.

Lecture 14. Introduction to JavaScript. Mr. Mubashir Ali Lecturer (Dept. of Computer Science)

Lecture 8 (7.5?): Javascript

FALL 2017 CS 498RK JAVASCRIPT. Fashionable and Functional!

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

Variables and Typing

Javascript. Many examples from Kyle Simpson: Scope and Closures

CS312: Programming Languages. Lecture 21: JavaScript

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

COMS 469: Interactive Media II

JavaScript Introduction

Brief Summary of Java

JavaScript. What s wrong with JavaScript?

JavaScript: More Syntax

Boot Camp JavaScript Sioux, March 31, 2011

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

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

JavaScript: Getting Started

Princess Nourah bint Abdulrahman University. Computer Sciences Department

CGS 3066: Spring 2015 JavaScript Reference

Documents and computation. Introduction to JavaScript. JavaScript vs. Java Applet. Myths. JavaScript. Standard

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

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

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2017)

CHAD Language Reference Manual

BEFORE CLASS. If you haven t already installed the Firebug extension for Firefox, download it now from

JavaScript Basics. Mendel Rosenblum. CS142 Lecture Notes - JavaScript Basics

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

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

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

JavaScript Functions, Objects and Array

JavaScript: Sort of a Big Deal,

JAVASCRIPT. sarojpandey.com.np/iroz. JavaScript

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

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

JavaScript Lecture 1

Module 5 JavaScript, AJAX, and jquery. Module 5. Module 5 Contains an Individual and Group component

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

Quick-Start Guide: Applying for a Grant Using the Grants Ontario System

Pace University. Fundamental Concepts of CS121 1

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CS 130(0) JavaScript Lab

JAVASCRIPT BASICS. JavaScript String Functions. Here is the basic condition you have to follow. If you start a string with

Functional Programming. Pure Functional Programming

CISC 1600 Lecture 2.4 Introduction to JavaScript

COSC 122 Computer Fluency. Iteration and Arrays. Dr. Ramon Lawrence University of British Columbia Okanagan

Frontend II: Javascript and DOM Programming. Wednesday, January 7, 15

Compiler Errors. Flash CS4 Professional ActionScript 3.0 Language Reference. 1 of 18 9/6/2010 9:40 PM

UNIT 3 SECTION 1 Answer the following questions Q.1: What is an editor? editor editor Q.2: What do you understand by a web browser?

Test 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B.

Programming for the Web with PHP

Internet Explorer Script Error Invalid Character Code 0

SEEM4570 System Design and Implementation Lecture 03 JavaScript

COMS 469: Interactive Media II

JavaScript: Coercion, Functions, Arrays

JavaScript for PHP Developers

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

PHP. Interactive Web Systems

Such JavaScript Very Wow

Scripting for Multimedia LECTURE 3: INTRODUCING JAVASCRIPT

The JavaScript Language

COMP284 Scripting Languages Lecture 10: PHP (Part 2) Handouts

a Why JavaScript? jonkv interactivity on the web CGI JavaScript Java Applets Netscape LiveScript JavaScript 1: Example

Arrays Structured data Arrays What is an array?

Copyright 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Chapter 11 Introduction to PHP

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

Iteration and Arrays Dr. Abdallah Mohamed

(Refer Slide Time: 01:12)

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

5/19/2015. Objectives. JavaScript, Sixth Edition. Understanding Syntax Errors. Introduction to Debugging. Handling Run-Time Errors

User s Guide. This User Guide contains information that can help you navigate through your professional development session.

Web Application Development

JavaScript is described in detail in many books on the subject, and there is excellent tutorial material at

JavaScript Specialist v2.0 Exam 1D0-735

NOTE: Answer ANY FOUR of the following 6 sections:

PHP: The Basics CISC 282. October 18, Approach Thus Far

(Refer Slide Time: 01:40)

IDM 231. Functions, Objects and Methods

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

ITEC136 - Lab 2 Population

HTML5 MOCK TEST HTML5 MOCK TEST I

Transcription:

JavaScript I

Introduction JavaScript traditionally runs in an interpreter that is part of a browsers Often called a JavaScript engine Was originally designed to add interactive elements to HTML pages First released in 1995 as part of Netscape Navigator Forms the backbone of modern web development Uses multiple paradigms, including object oriented and functional aspects

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

Background (cont'd) Now Javascript is a standarized language that is overseen by European Computer Manufacturers Association The of cial term for the language is ECMAScript The 6th version of the standard was nalized in June, 2015 The 500+ page standard is available for browsing Javascript has moved beyond the web for use in databases and desktop programs.

JavaScript Capabilities Add text dynamically to an HTML page React to events that occur on an HTML page Basic validation (but not the only validation!) Detect browser and other data about the environment Asynchronous communication

JavaScript Restrictions (On the Web) You cannot read or write to the le system in general You cannot interact with other processes on the system You can't keep your code private

JavaScript Engines Each Major browser uses their own JavaScript engine (interpreter) There are differences not only in what is supported, but the speed of various functions The Engines Spider Monkey The original engine, tracing all the way back to Netscape Navigator Now developed by Mozilla Used in all Mozilla products, as well as Adobe Acrobat and Mongo DB

The Engines (Continued) V8 Developed by the Chromium team at Google Used in Chrome, Opera, and Vivaldi browsers Also used in Couchbase and node.js JavaScriptCore Developed by the WebKit team Used in Safari (known as Nitro) Chakra(Core) Developed by Microsoft Used in Internet Explorer and Edge Slightly different engines in each

Placing JavaScript in a Web Page JavaScript is embedded in a web page using the <script> tags You used to have to specify the type attribute, but HTML5 assumes JavaScript The JavaScript can be Placed between the script tags Stored in an external le, and speci ed using the src attribute

<script> JS GOES HERE </script> <script src="file_name.js"></script>

The noscript Tag Not all browsers will run JavaScript The user may have disabled it May be text-based or a screen reader Although screen readers are getting more advanced To handle these situtations gracefully, use the noscript tag Content in noscript is only displayed if running JavaScript is not possible

Developing Javascript Code NodeJS Web Browser Web Console (Firefox) Javascript Console (Chrome) Show Error Console (Safari, after enabling developer menu) Console Tool (IE 11+, Edge)

Variables & Scope Variables should always be declared using the keywords var, let, or const Not necessary always, but easier than trying to remember when to use it and when not to In strict mode is always necessary The scope of a variable is the function it was declared in Making a new function is used to be only way to make a new scope We can use the keyword let to de ne the scope inside a block (ES2015)

var a = 1 var _a = 1 var $a = 1 //var 1a = 1

var a = 5 if(true) { var b = 6 } a + b

var c = 6 function f() { var d = 11 } c + d

var e = 5 if(true) { let g = 6 } e + g

Data Types Javascript is a dynamically typed language Javascript provides 7 data types Unde ned Null Boolean String Symbol Number Object

Number Only one type for both integers and Also can hold one of 3 special values -In nity In nity NaN oats

Operators 4 + 1 4-1 4 * 1 4 / -2

4 % -2 4/0 Infinity / Infinity Math.sqrt(-1) 0/0

var a = 1 a += 1 a var b = 20 b++ b

String Strings in Javascript can be delimited by either single or dobule quotes A speci c character at position i in a string can be access through bracket notation [i] The concatenation operator is +

"abc"[0] "abc" + "abc" 'Someone said "this" '

String Methods charat(i) allows you to index using a method rather than [] concat(s1,s2...) allows multiple strings to be concatenated in one call indexof(string)/lastindexof(string) nds the rst or last occurrence of the argument in the string split(sep) returns an array, the result of splitting the string on the separator passed in length is a property that holds the number of characters in a string

"abc".charat(0) "abc".concat("def","ghi",'jkl') "abba".indexof('b') "abba".lastindexof('b')

"a,b,c,d".split(",") var x = "a,,b,c,,d".split(",,") x "abc".length

Boolean The boolean values in Javascript are true and false 0, NaN and "" are coerced to false The operators are && (and) (or)! (not) To test equality there are two operators == Tests the value only === Tests the value and the type

(1 > 0 ) && (1 < 10) '1' == 1 '1' === 1 '1'!= 1 '1'!== 1

Unde ned & Null A variable in Javascript that is uninitialized has a value of unde ned null is used in similar situations Testing a variable equal to (==) null actually test null or unde ned

var undeclared console.log(undeclared === undefined) console.log(undeclared === null) console.log(undeclared == null) console.log(null == false) console.log("abc"[200])

Arrays Arrays in Javascript are a special type of object They can be initialized by listing the elements between square brackets Calling the array constructor Array() with The length of the array The elements of the array They are indexed using []

var arr = [1,2,3,4,5,6] console.log(arr) console.log(arr[0]) var arr2 = Array(10) console.log(arr2) console.log(arr2[0]) var arr3 = Array(10,9,8,7,6) console.log(arr3) console.log(arr3[0]) console.log(arr3[-1])

Array Methods concat(a1,a2,a3) Appends several arrays together into one array join(string) Returns a string, with each element joined by a string pop/push(el) Remove or add an element at the end of the array shift/unshift(el) Remove or add an element at the front of the array reverse() Returns array in reverse order sort(function) Returns the array, sorted by a function

var my_array = Array(1,2,3,4) my_array.concat([1,2,3,4],[1,2,3,4]) my_array.join(",") my_array.join("...")

var my_array2 = Array(1,2,3,4,5) my_array2.pop() console.log(my_array2) my_array2.push("elephant") console.log(my_array2)

var my_array3 = Array(10,9,8,7,6,5) console.log(my_array3.shift()) console.log(my_array3) my_array3.unshift("t-minus") console.log(my_array3)

Type Coercion When dealing with two different data types, Javascript will prefer to attempt to cast one of the types rather than throw an error This is known as type coercion If type coercion fails, rather throw an error, NaN or unde ned are usually returned

2 - '20' 5 + Number('1') 5 + '1'

'1' + 2 '5' * 20 '5' * '5'

't' * 5 't' / null null == 0 '8' / null

Conditionals & Looping Javascript provides the following conditional statements if switch And the following looping mechanisms for while do-while for-in for-of

If The if statement in Javascript is pretty straightforward if (condition) { dosomething } The parentheses are not necesary for a single line, but should always be used if else looks like this: if (condition1){ } else if(condition2){ } else if(condition3){ } else{ }

var x = '0' if(x < 0){ console.log("negative"); } // /* Note the triple equals */ else if(x === 0){ console.log("zero"); } else{ console.log("positive"); }

Switch Statement The syntax and mechanics of the swtich statement borrow heavily from other languages Cases are marked with case and default provides a catch all case switch(totest){ case 1: case 2: dosomething break case "A": case "B": somethingelse break case "D": other break default: final break }

switch('0'){ case -1: console.log("negative") break case 0: //case '0': console.log("zero") break default: console.log("positive") break }

Looping The for loop construct is similar to other languages you know for(var i = 0; i < 10; i++){ } The while and do while syntax is also similar var i = 0 while(i < 10){ i++ }

for(let z = 0; z < 10; z++){ console.log(z * z) } console.log(z)

var q = 1 while(q < 10){ q++ }

For-In and For-Of The for in loop will loop over an objects keys Order is not guaranteed to be maintained The for of loop is new, and iterates directly over the values of an object Order is maintained

let to_loop = ['a','b','c',1,2,3] for (i in to_loop){ console.log(i, to_loop[i]) } for (j of to_loop){ console.log(j) }

Functions Functions in JavaScript are rst class objects They can be passed into and returned from other functions This means closures are possible To declare a function in JavaScript, the keyword is function function name(param1, param2,...){ }

Function Examples square(10) function square(x){ return x*x } function counter(){ var count = 0; return function(){ count++ return count } } var c = counter() c() + 1