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

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

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts

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

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

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

COMP284 Scripting Languages Lecture 2: Perl (Part 1) Handouts

COMP519 Web Programming Lecture 14: JavaScript (Part 5) Handouts

CSC Web Programming. Introduction to JavaScript

COMP519 Web Programming Lecture 20: Python (Part 4) Handouts

Chapter 6 Primitive types

Decaf Language Reference Manual

JavaScript: The Basics

Such JavaScript Very Wow

Client-Side Web Technologies. JavaScript Part I

HTML5 and CSS3 More JavaScript Page 1

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

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

COMP519 Web Programming Lecture 27: PHP (Part 3) Handouts

CMPT 125: Lecture 3 Data and Expressions

VLC : Language Reference Manual

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

Chapter 2 Working with Data Types and Operators

JavaScript I Language Basics

Language Fundamentals Summary

CA4003 Compiler Construction Assignment Language Definition

Lexical Considerations

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

COMP284 Scripting Languages Lecture 9: PHP (Part 1) Handouts

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

Basics of Java Programming

1 Lexical Considerations

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

CSCE 120: Learning To Code

Java Notes. 10th ICSE. Saravanan Ganesh

TED Language Reference Manual

COMP284 Scripting Languages Lecture 11: PHP (Part 3) Handouts

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

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

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

Language Reference Manual

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

Lexical Considerations

2 nd Week Lecture Notes

Decaf Language Reference

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Crayon (.cry) Language Reference Manual. Naman Agrawal (na2603) Vaidehi Dalmia (vd2302) Ganesh Ravichandran (gr2483) David Smart (ds3361)

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

Learning Language. Reference Manual. George Liao (gkl2104) Joseanibal Colon Ramos (jc2373) Stephen Robinson (sar2120) Huabiao Xu(hx2104)

CGS 3066: Spring 2015 JavaScript Reference

XQ: An XML Query Language Language Reference Manual

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

CHIL CSS HTML Integrated Language

Chapter 3 Data Types and Variables

Chapter 17. Fundamental Concepts Expressed in JavaScript

VENTURE. Section 1. Lexical Elements. 1.1 Identifiers. 1.2 Keywords. 1.3 Literals

4 Programming Fundamentals. Introduction to Programming 1 1

Lecture 3 Operators MIT AITI

Chapter 2: Using Data

JavaScript CS 4640 Programming Languages for Web Applications

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

COMS W4115 Programming Languages & Translators GIRAPHE. Language Reference Manual

Full file at

Visual C# Instructor s Manual Table of Contents

Built-in Types of Data

These are reserved words of the C language. For example int, float, if, else, for, while etc.

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Zheng-Liang Lu Java Programming 45 / 79

ARG! Language Reference Manual

JME Language Reference Manual

Typescript on LLVM Language Reference Manual

Ex: If you use a program to record sales, you will want to remember data:

Language Reference Manual

3. Java - Language Constructs I

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

Declaration and Memory

Values and Variables 1 / 30

Java Basic Datatypees

A variable is a name for a location in memory A variable must be declared

COMP Primitive and Class Types. Yi Hong May 14, 2015

1 CS580W-01 Quiz 1 Solution

egrapher Language Reference Manual

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

JAVASCRIPT. sarojpandey.com.np/iroz. JavaScript

Ruby: Introduction, Basics

FRAC: Language Reference Manual

CHAD Language Reference Manual

GridLang: Grid Based Game Development Language Language Reference Manual. Programming Language and Translators - Spring 2017 Prof.

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

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

CHAPTER 5: JavaScript Basics 99

The SPL Programming Language Reference Manual

COMS 469: Interactive Media II

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

Number Systems, Scalar Types, and Input and Output

Chapter 2 Elementary Programming

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

JavaScript CS 4640 Programming Languages for Web Applications

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

corgi Language Reference Manual COMS W4115

Transcription:

COMP519 Web Programming Lecture 11: JavaScript (Part 2) Handouts Ullrich Hustadt Department of Computer Science School of Electrical Engineering, Electronics, and Computer Science University of Liverpool

Contents 1 Primitive Data Types Booleans Numbers Strings 2 Arrays Literals 3 Storing Values Variables Assignments Constants Determining the Type of a Value or Variable 4 Further Reading Ullrich Hustadt COMP519 Web Programming L11 1

Types Data Type / Datatype / Type A set of computer represented values together with a set of operations that can be performed on those values JavaScript distinguished four main types: boolean number string object booleans integers and floating-point numbers strings objects (including functions and arrays) Every value is of a particular type (or none) Ullrich Hustadt COMP519 Web Programming L11 2

Booleans Primitive Types Booleans JavaScript has a boolean datatype with constants true and false (case sensitive) JavaScript offers the following boolean operators && (conjunction) (disjunction)! (negation) The truth tables for the boolean operators are as follows: A B (A && B) true true B (true) true false B (false) false true A (false) false false A (false) A (! A) true false false true A B (A B) true true A (true) true false A (true) false true B (true) false false B (false) Ullrich Hustadt COMP519 Web Programming L11 3

Boolean Operators Primitive Types Booleans As in almost every programming language the operators && (conjunction) (disjunction)! (negation) are so-called short-circuit boolean operators: A boolean expression is evaluated (using inorder traversal) only as far as is necessary to determine the overall truth value of the expression ( true ( false &&! (! ( true && ( false true ))))) ( true && ( false &&! (! ( true && ( false true ))))) This means that && and are not commutative, that is, (A && B) is not the same as (B && A) often taken advantage of in programs ( denom!= 0) && ( num / denom > 10) Ullrich Hustadt COMP519 Web Programming L11 4

Primitive Types Numbers Integers and Floating-point Numbers The JavaScript datatype number covers both integer numbers 0 2012-40 1263978 floating-point numbers 1.25 256.0-12e19 2.4e-10 all numbers are stored as 64-bit floating-point numbers There are also some pre-defined number constants including Math.PI (case sensitive) 3.14159265358979323846 NaN (case sensitive) not a number Infinity (case sensitive) infinity Ullrich Hustadt COMP519 Web Programming L11 5

Primitive Types Numbers Operators on Integers and Floating-point Numbers Arithmetic operators supported by JavaScript include +, -, * Addition, Subtraction, Multiplication / Division ** Exponentiation ++ Increment (+1) -- Decrement ( 1) The Math object provides a wide range of additional mathematical functions Math.abs(number) absolute value Math.ceil(number) round fractions up Math.floor(number) round fractions down Math.round(number) round fractions Math.log(number) natural logarithm Math.random() random number between 0 and 1 Math.sqrt(number) square root Ullrich Hustadt COMP519 Web Programming L11 6

Strings Primitive Types Strings JavaScript supports both single-quoted and double-quoted strings " This is a string " " true " This is also a string 519 The escape character \ can be used to include single quotes in single-quoted strings and double quotes in double-quoted strings: This isn \ t a " number " " We won t sing \" God Save the Queen.\" " The escape character \ also must be used to include \ in a string " This is a backslash \\" This is a backslash \\ Additional escape characters are available, but do not make much sense in the context of HTML \b (backspace) \f (form feed) \n (newline) \r (carriage return) \t (tab) Ullrich Hustadt COMP519 Web Programming L11 7

Strings Primitive Types Strings JavaScript uses + for string concatenation "519" + 123 // returns " 519123" the + end // returns " theend " JavaScript supports multi-line strings document. writeln ("\ Your name is " + name + "\ and you are studying " + degree + "\ at " + university \ "); Ullrich Hustadt COMP519 Web Programming L11 8

String Operators Primitive Types Strings There are a range of additional string operators, for example: string.substr(start, [length]) Returns (up to) length characters of string beginning at start "university".substr(3,2) // returns "ve" (count starts at 0) string.indexof(str, [start]) Returns the index number at which str starts in string after start "university".indexof("i",3) // returns 7 (count starts at 0) string.match(regexp) Returns an array of matching substrings for the regular expression regexp in string "0ab1".match(/[^0-9]/) // returns ["a"] string.replace(regexp, str) Replaces occurrences of regexp in string by str "0ab1".replace(/[^0-9]/g,"c") // returns "0cc1" Ullrich Hustadt COMP519 Web Programming L11 9

Array Literals Arrays An array literal is a comma-separated list of values enclosed in square brackets [2,3,5,7,11] Each element of that list has an index position given by the number of other elements preceding it the first index position is 0 [2,3,5,7,11] 0 1 2 3 4 The values in an array literal do not need to be of the same type [2, two,3," three ",1.1 e1, true ] The values in an array literal can include other array literals (nested array literal) [2,[3,5],[7,[11]]] Ullrich Hustadt COMP519 Web Programming L11 10

Arrays Operators on Array Literals It is possible to access an element of an array literal via its index position: arrayliteral[index] [2,3,5,7,11][1] // returns 3 For a nested array literal, it is possible to iterate this access operation [2,[3,5],[7,[11]]][1] // returns [3,5] 0 1 2 [2,[3,5],[7,[11]]][1][0] // returns 3 The length operation returns the number of elements in an array literal: arrayliteral.length [2,3,5,7,11]. length // returns 5 We will discuss more operators in the context of array variables Ullrich Hustadt COMP519 Web Programming L11 11

Variables Storing Values Variables A JavaScript variable name may consist of letters, digits, the $ symbol, and underscore, but cannot start with a digit JavaScript variable names are case sensitive Ullrich Hustadt COMP519 Web Programming L11 12

Variables Storing Values Variables Variables can be declared using one of the following statements: var variable1, variable2,... var variable1 = value1, variable2 = value2,... The second statement also initialises the variables Used inside a function definition, a declaration creates a local variable (only accessible within the function) Used outside a function definition, a declaration creates a global variable A variable can be inialised without a declaration by assigning a value to it: variable = value Both inside and outside a function definition, initialising an undeclared variable creates a global variable Note: A declaration does not specify the type of a variable only assigning a value of a certain type gives a variable a type Ullrich Hustadt COMP519 Web Programming L11 13

Variables Storing Values Variables In JavaScript, the use of the value of a variable that is neither declared nor initialised will result in a reference error A declared but uninitialised variable has the default value undefined and has no specific type myvar1 // reference error var myvar2 // myvar2 has value undefined var myvar3 = 5 // myvar3 has value 5 Ullrich Hustadt COMP519 Web Programming L11 14

Assignments Storing Values Assignments JavaScript uses the equality sign = for assignments student_ id = 200846369; As in Java, this is an assignment expression The value of an assignment expression is the value assigned b = ( a = 0) + 1; // a has value 0, b has value 1 JavaScript supports most of the standard binary assignment operators: Binary assignment Equivalent assignment var += expr var = var + expr var -= expr var = var - expr var *= expr var = var * expr var /= expr var = var / expr var %= expr var = var % expr var **= expr var = var ** expr (not in MS IE) Ullrich Hustadt COMP519 Web Programming L11 15

Constants Storing Values Constants Some JavaScript dialects allow the definition of constants using const variable1 = value1, variable2 = value2,... defines one or more constants constants follow the same scope rules as variables Attempts to change the value of a constant should result in a type error const columns = 10; columns ++; // type error However, this construct is not supported by MS Internet Explorer 6 10 and does not have the desired effect in Safari before version 5.1.7 nor Opera before version 12 Ullrich Hustadt COMP519 Web Programming L11 16

Storing Values Values, Variables and Types typeof string typeof value returns a string representation of the type of value Boolean "boolean" Number "number" String "string" Object "object" undefined "undefined" null "object" NaN "number" Infinity "number" Future versions of JavaScript may have an option to change typeof null to "null" (as in PHP) document. writeln (" Type of 23.0: " + typeof (23.0) + " < br / >" document. writeln (" Type of \"23\": " + typeof ("23") +" < br / >" var a document. writeln (" Type of a: " + typeof (a) + "<br / >" Type of 23.0: number < br / > Type of "23": string < br / > Type of a: undefined < br / > Ullrich Hustadt COMP519 Web Programming L11 17

Revision Revision and Further Reading Read Chapter 20: The JavaScript Language: Data Types and Variables Chapter 20: The JavaScript Language: Calculations and Operators of S. Schafer: Web Standards Programmer s Reference. Wiley Publishing, 2005. Harold Cohen Library 518.532.S29 or E-book http://library.liv.ac.uk/record=b2174141 Read Chapter 3: Language Basics: Variables, Data Types, Operators of N. C. Zakas: Professional JavaScript for Web developers. Wrox Press, 2009. Harold Cohen Library 518.59.Z21 or E-book http://library.liv.ac.uk/record=b2238913 Ullrich Hustadt COMP519 Web Programming L11 18